diff options
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..a1c4a66 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,55 @@ +use std::fs; +use std::path::Path; + +use anyhow::anyhow; +use serde_derive::Deserialize; +use serde_derive::Serialize; + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub struct Root { + pub listen: String, + pub fields: Vec<Field>, + pub relations: Vec<Relation>, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub struct Field { + pub id: String, + #[serde(default = "distinct_default")] + pub distinct: bool, +} + +fn distinct_default() -> bool { + false +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub struct Relation { + pub name: String, + pub connect: String, + pub table_name: String, + pub fields: Vec<RelationField>, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub struct RelationField { + pub id: String, + pub query: String, +} + +pub fn read_file<P: AsRef<Path>>(path: P) -> Result<Root, anyhow::Error> { + let s = match fs::read_to_string(path) { + Ok(s) => s, + Err(why) => { + return Err(anyhow!("failed to read toml file: {}", why)); + } + }; + match toml::from_str(&*s) { + Ok(r) => Ok(r), + Err(why) => Err(anyhow!("failed to decode toml file: {}", why)), + } +}
\ No newline at end of file |