forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.rs
More file actions
66 lines (58 loc) · 1.64 KB
/
Copy pathconfig.rs
File metadata and controls
66 lines (58 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use clap::ArgMatches;
use std::env;
use std::error::Error;
use std::fs;
use std::io::Read;
use std::path::PathBuf;
use toml;
use super::find_project_root;
use print_schema;
#[derive(Deserialize, Default)]
#[serde(deny_unknown_fields)]
pub struct Config {
#[serde(default)]
pub print_schema: PrintSchema,
}
impl Config {
pub fn file_path(matches: &ArgMatches) -> PathBuf {
matches
.value_of("CONFIG_FILE")
.map(PathBuf::from)
.or_else(|| env::var_os("DIESEL_CONFIG_FILE").map(PathBuf::from))
.unwrap_or_else(|| find_project_root().unwrap_or_default().join("diesel.toml"))
}
pub fn read(matches: &ArgMatches) -> Result<Self, Box<dyn Error>> {
let path = Self::file_path(matches);
if path.exists() {
let mut bytes = Vec::new();
fs::File::open(path)?.read_to_end(&mut bytes)?;
toml::from_slice(&bytes).map_err(Into::into)
} else {
Ok(Self::default())
}
}
}
#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PrintSchema {
#[serde(default)]
pub file: Option<PathBuf>,
#[serde(default)]
pub with_docs: bool,
#[serde(default)]
pub filter: print_schema::Filtering,
#[serde(default)]
pub schema: Option<String>,
#[serde(default)]
pub patch_file: Option<PathBuf>,
#[serde(default)]
pub import_types: Option<Vec<String>>,
}
impl PrintSchema {
pub fn schema_name(&self) -> Option<&str> {
self.schema.as_ref().map(|s| &**s)
}
pub fn import_types(&self) -> Option<&[String]> {
self.import_types.as_ref().map(|v| &**v)
}
}