Add YAML as a config format option

@mustafa89 ;)
This commit is contained in:
2022-01-14 17:27:04 +01:00
parent e45de3b498
commit 9b64de7991
10 changed files with 394 additions and 295 deletions

View File

@@ -36,6 +36,10 @@ impl Config {
Err(error) => Err(error.to_string()),
}
}
pub fn as_yaml(&self) -> Result<String, String> {
serde_yaml::to_string(self).map_err(|e| e.to_string())
}
}
#[derive(Debug, Serialize, Deserialize)]
@@ -62,12 +66,15 @@ pub fn read_config(path: &str) -> Result<Config, String> {
let config: Config = match toml::from_str(&content) {
Ok(c) => c,
Err(e) => {
return Err(format!(
"Error parsing configuration file \"{}\": {}",
path, e
))
}
Err(_) => match serde_yaml::from_str(&content) {
Ok(c) => c,
Err(e) => {
return Err(format!(
"Error parsing configuration file \"{}\": {}",
path, e
))
}
},
};
Ok(config)

View File

@@ -61,10 +61,25 @@ pub struct OptionalConfig {
pub config: Option<String>,
}
#[derive(clap::ArgEnum, Clone)]
pub enum ConfigFormat {
Yaml,
Toml,
}
#[derive(Parser)]
pub struct Find {
#[clap(help = "The path to search through")]
pub path: String,
#[clap(
arg_enum,
short,
long,
help = "Format to produce",
default_value_t = ConfigFormat::Toml,
)]
pub format: ConfigFormat,
}
#[derive(Parser)]

View File

@@ -119,15 +119,34 @@ fn main() {
} else {
let config = trees.to_config();
let toml = match config.as_toml() {
Ok(toml) => toml,
Err(error) => {
print_error(&format!("Failed converting config to TOML: {}", &error));
process::exit(1);
match find.format {
cmd::ConfigFormat::Toml => {
let toml = match config.as_toml() {
Ok(toml) => toml,
Err(error) => {
print_error(&format!(
"Failed converting config to TOML: {}",
&error
));
process::exit(1);
}
};
print!("{}", toml);
}
};
print!("{}", toml);
cmd::ConfigFormat::Yaml => {
let yaml = match config.as_yaml() {
Ok(yaml) => yaml,
Err(error) => {
print_error(&format!(
"Failed converting config to YAML: {}",
&error
));
process::exit(1);
}
};
print!("{}", yaml);
}
}
}
for warning in warnings {
print_warning(&warning);