Memoscopy / libMMU

Rust crate that aims to simplify the process of rebuilding virtual address spaces from a memory dump
https://memoscopy.github.io/libMMU/
GNU General Public License v3.0
6 stars 0 forks source link

Loading base configuration from differents formats : TOML, YAML or from an in-line Rust builder pattern #4

Closed Esgr0bar closed 5 months ago

Esgr0bar commented 6 months ago

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

shard77 commented 6 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.

Esgr0bar commented 6 months ago

@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 commented 6 months ago

@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?

Esgr0bar commented 6 months ago

@shard77 Yeah, you can look my code it is push in the branch