-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.rs
More file actions
51 lines (41 loc) · 1.39 KB
/
config.rs
File metadata and controls
51 lines (41 loc) · 1.39 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
//! Loading `.sqlshield.toml` from the current working directory.
use std::path::{Path, PathBuf};
use std::str::FromStr;
use serde::Deserialize;
use sqlshield::Dialect;
pub const CONFIG_FILE_NAME: &str = ".sqlshield.toml";
#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RawConfig {
pub schema: Option<PathBuf>,
pub directory: Option<PathBuf>,
pub dialect: Option<String>,
pub db_url: Option<String>,
}
#[derive(Debug, Default)]
pub struct Config {
pub schema: Option<PathBuf>,
pub directory: Option<PathBuf>,
pub dialect: Option<Dialect>,
pub db_url: Option<String>,
}
/// Load `.sqlshield.toml` from `dir`. Returns an empty config (`Ok(None)`)
/// if no file is present. Only malformed files error; missing ones are
/// just defaults.
pub fn load_from(dir: &Path) -> Result<Option<Config>, String> {
let path = dir.join(CONFIG_FILE_NAME);
if !path.is_file() {
return Ok(None);
}
let source = std::fs::read_to_string(&path)
.map_err(|e| format!("failed to read {}: {e}", path.display()))?;
let raw: RawConfig =
toml::from_str(&source).map_err(|e| format!("invalid {}: {e}", path.display()))?;
let dialect = raw.dialect.as_deref().map(Dialect::from_str).transpose()?;
Ok(Some(Config {
schema: raw.schema,
directory: raw.directory,
dialect,
db_url: raw.db_url,
}))
}