Closed Esgr0bar closed 5 months ago
@Esgr0bar I don't think there's any need to handle XML/JSON/YAML. I think it's better if we stick to one specific format.
@shard77 no problem that doesn't change a lot the code we can stick to one format. Here's the code that handle the different format :
fn load_config(file_path: &str) -> Result<MachineConfig, Box<dyn Error>> {
let config_str = fs::read_to_string(file_path)?;
let config: MachineConfig = match file_path.rsplit('.').next().ok_or("No file extension found")? {
"json" => serde_json::from_str(&config_str)?,
"yaml" => serde_yaml::from_str(&config_str)?,
"toml" => toml::from_str(&config_str)?,
"xml" => serde_xml_rs::from_str(&config_str)?,
_ => return Err("Unsupported file format".into()),
};
Ok(config)
}
@shard77 no problem that doesn't change a lot the code we can stick to one format. Here's the code that handle the different format :
fn load_config(file_path: &str) -> Result<MachineConfig, Box<dyn Error>> { let config_str = fs::read_to_string(file_path)?; let config: MachineConfig = match file_path.rsplit('.').next().ok_or("No file extension found")? { "json" => serde_json::from_str(&config_str)?, "yaml" => serde_yaml::from_str(&config_str)?, "toml" => toml::from_str(&config_str)?, "xml" => serde_xml_rs::from_str(&config_str)?, _ => return Err("Unsupported file format".into()), }; Ok(config) }
alright, perfect :+1: @Esgr0bar Did you use the derive feature from serde?
@shard77 Yeah, you can look my code it is push in the branch
we'll use this logic https://rust-unofficial.github.io/patterns/patterns/creational/builder.html
Maybe we finally use only in-line format or one format and stick with it