-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clafrica): add support for config file
- Loading branch information
1 parent
4ec88ef
commit 003b2a9
Showing
9 changed files
with
307 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Main clafrica config file | ||
|
||
[core] | ||
buffer_size = 12 | ||
|
||
[data] | ||
sample = { path = "./data_sample.toml" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Sample data file | ||
|
||
[info] | ||
name = "Sample code" | ||
authors = [ | ||
"Foo Bar <foo@example.com>", | ||
{ name = "Baz Qux", email = "bazqux@example.com", url = "https://example.com/bazqux" } | ||
] | ||
website = "sample@example.com" | ||
description = "Sample code for testing purpose." | ||
|
||
[data] | ||
2a_ = "á̠" | ||
2af_ = "ɑ̠́" | ||
2ai_ = "έ̠" | ||
2ia = "íá" | ||
2iaf = "íɑ́" | ||
2ie = "íé" | ||
2ua = "úá" | ||
2uaf = "úɑ́" | ||
2uuaf = "ʉ́ɑ́" | ||
sample2 = { path = "./data_sample2.toml" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Sample data file | ||
|
||
[info] | ||
name = "Sample code 2" | ||
authors = ["Foo Bar <foo@example.com>"] | ||
website = "sample@example.com" | ||
description = "Sample code for testing purpose." | ||
|
||
[data] | ||
2aa = { value = "áá", alias = ["a22"]} | ||
2ee = { value = "éé", alias = ["e22"]} | ||
2aff = { value = "ɑ́ɑ́", alias = ["aff22"]} | ||
2ii = { value = "íí", alias = ["i22"]} | ||
2oo = { value = "óó", alias = ["o22"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use serde::Deserialize; | ||
use std::{collections::HashMap, error, fs, path::Path}; | ||
use toml::{self}; | ||
|
||
#[derive(Deserialize, Debug, Clone)] | ||
pub struct Config { | ||
pub core: Option<CoreConfig>, | ||
pub data: HashMap<String, Data>, | ||
} | ||
|
||
#[derive(Deserialize, Debug, Clone)] | ||
pub struct CoreConfig { | ||
pub buffer_size: usize, | ||
} | ||
|
||
#[derive(Deserialize, Debug, Clone)] | ||
#[serde(untagged)] | ||
pub enum Data { | ||
Simple(String), | ||
File(DataFile), | ||
Detailed(DetailedData), | ||
} | ||
|
||
#[derive(Deserialize, Debug, Clone)] | ||
pub struct DataFile { | ||
path: String, | ||
} | ||
|
||
#[derive(Deserialize, Debug, Clone)] | ||
pub struct DetailedData { | ||
value: String, | ||
alias: Vec<String>, | ||
} | ||
|
||
impl Config { | ||
pub fn from_file(filepath: &Path) -> Result<Self, Box<dyn error::Error>> { | ||
let content = fs::read_to_string(filepath)?; | ||
let mut config: Self = toml::from_str(&content)?; | ||
|
||
let config_path = filepath.parent().unwrap(); | ||
|
||
let mut data = HashMap::new(); | ||
|
||
config | ||
.data | ||
.iter() | ||
.try_for_each(|(key, value)| -> Result<(), Box<dyn error::Error>> { | ||
match value { | ||
Data::File(DataFile { path }) => { | ||
let filepath = config_path.join(path); | ||
let conf = Config::from_file(&filepath)?; | ||
data.extend(conf.data); | ||
} | ||
Data::Simple(_) => { | ||
data.insert(key.to_owned(), value.clone()); | ||
} | ||
Data::Detailed(DetailedData { value, alias }) => { | ||
alias.iter().chain([key.to_owned()].iter()).for_each(|e| { | ||
data.insert(e.to_owned(), Data::Simple(value.to_owned())); | ||
}); | ||
} | ||
}; | ||
Ok(()) | ||
})?; | ||
|
||
config.data = data; | ||
|
||
Ok(config) | ||
} | ||
|
||
pub fn extract_data(&self) -> HashMap<&String, &String> { | ||
self.data | ||
.iter() | ||
.filter_map(|(k, v)| { | ||
let v = match v { | ||
Data::Simple(value) => Some(value), | ||
_ => None, | ||
}; | ||
v.map(|v| (k, v)) | ||
}) | ||
.collect() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn from_file() { | ||
use crate::config::Config; | ||
use std::path::Path; | ||
|
||
let conf = Config::from_file(&Path::new("./data/config_sample.toml")); | ||
|
||
let conf = conf.unwrap(); | ||
assert_eq!(conf.core.unwrap().buffer_size, 12); | ||
assert_eq!(conf.data.len(), 19); | ||
|
||
let conf = Config::from_file(&Path::new("./not_found")); | ||
assert!(conf.is_err()); | ||
|
||
let conf = Config::from_file(&Path::new("./data/blank_sample.toml")); | ||
assert!(conf.is_err()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.