Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
6 tasks done
Esgr0bar opened this issue May 11, 2024 · 4 comments
Assignees
Labels
enhancement New feature or request ready for review feature or request is ready for review
Milestone

Comments

@Esgr0bar
Copy link
Contributor

Esgr0bar commented May 11, 2024

we'll use this logic https://rust-unofficial.github.io/patterns/patterns/creational/builder.html

  • handle YAML (done ->need to be tested)
  • handle TOML (done ->need to be tested)
  • handle in-line
  • handle JSON
  • handle XML (done ->need to be tested)
  • find which type of file is passed

Maybe we finally use only in-line format or one format and stick with it

@Esgr0bar Esgr0bar added this to the v0.1.0 milestone May 11, 2024
@Esgr0bar Esgr0bar self-assigned this May 11, 2024
@shard77
Copy link
Member

shard77 commented May 12, 2024

@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
Copy link
Contributor Author

Esgr0bar commented May 12, 2024

@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)
}

@Esgr0bar Esgr0bar added the enhancement New feature or request label May 12, 2024
@shard77
Copy link
Member

shard77 commented May 12, 2024

@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 👍
@Esgr0bar Did you use the derive feature from serde?

@Esgr0bar
Copy link
Contributor Author

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

@Esgr0bar Esgr0bar added ready for review feature or request is ready for review ready for merge feature or request has been reviewed and is ready to merge and removed ready for merge feature or request has been reviewed and is ready to merge labels May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ready for review feature or request is ready for review
Projects
None yet
Development

No branches or pull requests

2 participants