diff options
author | Keuin <[email protected]> | 2024-02-24 02:46:28 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2024-02-24 03:06:16 +0800 |
commit | e670f5389171a7a124d990fe2093e1f9290eccfc (patch) | |
tree | 9c9b313db57159f019b2d4f5770ab7a2e1085fe5 /src/config.rs |
Initial commit
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 |