Autoformat

This commit is contained in:
2025-07-20 10:51:03 -05:00
parent 64215cefcc
commit 6ca279de49

View File

@@ -75,7 +75,7 @@ pub fn default_paths() -> impl Iterator<Item = PathBuf> {
/// TODO: Check for, and warn or error when given a dir. /// TODO: Check for, and warn or error when given a dir.
pub fn get_config( pub fn get_config(
project: &str, project: &str,
search_files: impl Iterator<Item=PathBuf> search_files: impl Iterator<Item = PathBuf>,
) -> Result<PartialConfig> { ) -> Result<PartialConfig> {
/* /*
1. Get conf search (from fn input) 1. Get conf search (from fn input)
@@ -87,7 +87,6 @@ pub fn get_config (
7. (merge, again) Fold the PartialConfigs into a finished one 7. (merge, again) Fold the PartialConfigs into a finished one
*/ */
let file_iter = search_files; let file_iter = search_files;
let toml_iter = file_iter let toml_iter = file_iter
@@ -117,15 +116,11 @@ pub fn get_config (
)) ))
}) })
// 5. Merge the PartialConfigs together, project-specific has higher priority // 5. Merge the PartialConfigs together, project-specific has higher priority
.and_then(|pair| { .and_then(|pair| Ok(pair.0.merge(pair.1)));
Ok(pair.0.merge(pair.1))
});
maybe_proj maybe_proj
}) })
.filter_map(|res| res.ok()) .filter_map(|res| res.ok())
.fold(PartialConfig::default(), |acc, inc|{ .fold(PartialConfig::default(), |acc, inc| acc.merge(inc));
acc.merge(inc)
});
return Ok(config_iter); return Ok(config_iter);
} }
@@ -177,7 +172,6 @@ impl TryFrom<&Table> for PartialConfig {
token: get_maybe_property(&value, "token")?.cloned(), token: get_maybe_property(&value, "token")?.cloned(),
}) })
} }
} }
/// The outer value must be a Table so we can get the sub-table from it. /// The outer value must be a Table so we can get the sub-table from it.
@@ -189,10 +183,12 @@ fn get_table<'outer>(outer: &'outer Table, table_name: impl ToString) -> Result<
.ok_or(Error::BadFormat)?) .ok_or(Error::BadFormat)?)
} }
/// Similar to `get_property()` but maps the "Error::NoSuchProperty" result to /// Similar to `get_property()` but maps the "Error::NoSuchProperty" result to
/// Option::None. Some properties aren't specified, and that's okay... sometimes. /// Option::None. Some properties aren't specified, and that's okay... sometimes.
fn get_maybe_property<'outer> (outer: &'outer Table, property: impl ToString) -> Result<Option<&'outer String>> { fn get_maybe_property<'outer>(
outer: &'outer Table,
property: impl ToString,
) -> Result<Option<&'outer String>> {
let maybe_prop = get_property(outer, property); let maybe_prop = get_property(outer, property);
match maybe_prop { match maybe_prop {
Ok(value) => Ok(Some(value)), Ok(value) => Ok(Some(value)),
@@ -209,7 +205,9 @@ fn get_maybe_property<'outer> (outer: &'outer Table, property: impl ToString) ->
/// The config properties are individual strings. This gets the named property, /// The config properties are individual strings. This gets the named property,
/// or an error explaining why it couldn't be fetched. /// or an error explaining why it couldn't be fetched.
fn get_property<'outer>(outer: &'outer Table, property: impl ToString) -> Result<&'outer String> { fn get_property<'outer>(outer: &'outer Table, property: impl ToString) -> Result<&'outer String> {
let maybe_prop = outer.get(&property.to_string()).ok_or(Error::NoSuchProperty)?; let maybe_prop = outer
.get(&property.to_string())
.ok_or(Error::NoSuchProperty)?;
if let Value::String(text) = maybe_prop { if let Value::String(text) = maybe_prop {
Ok(text) Ok(text)
} else { } else {
@@ -254,7 +252,10 @@ mod tests {
let fx_value = fx_input_str.parse::<Value>()?; let fx_value = fx_input_str.parse::<Value>()?;
let fx_value = fx_value.as_table().ok_or(Error::BadFormat)?; let fx_value = fx_value.as_table().ok_or(Error::BadFormat)?;
let mut expected: Map<String, Value> = Map::new(); let mut expected: Map<String, Value> = Map::new();
expected.insert(String::from("with_a_garbage"), Value::String(String::from("value"))); expected.insert(
String::from("with_a_garbage"),
Value::String(String::from("value")),
);
let res = get_table(&fx_value, String::from("tab"))?; let res = get_table(&fx_value, String::from("tab"))?;
assert_eq!(res, &expected); assert_eq!(res, &expected);
@@ -271,10 +272,7 @@ mod tests {
let search_paths = ["./test_data/sample_config.toml"] let search_paths = ["./test_data/sample_config.toml"]
.into_iter() .into_iter()
.map(PathBuf::from); .map(PathBuf::from);
let load_result = get_config( let load_result = get_config("/home/robert/projects/gt-tool", search_paths)?;
"/home/robert/projects/gt-tool",
search_paths
)?;
let expected = PartialConfig { let expected = PartialConfig {
project_path: Some(String::from("/home/robert/projects/gt-tool")), project_path: Some(String::from("/home/robert/projects/gt-tool")),
owner: Some(String::from("robert")), owner: Some(String::from("robert")),