I am creating a configuration parser and I am using the toml
crate to parse the config file (which is in toml) to a string. I want to create certain checks to ensure the user does not include fields not supported or not included in the config struct. This is what the config struct looks like
#[derive(Deserialize, Debug)]
pub struct Config {
add: Option<Add>,
sub: Option<Sub>,
mul: Option<Mul>,
}
And the add
, sub
and mul
structs look something like this
#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct add {
width: u32,
granularity: Option<u32>,
}
Since all the 3 structs within Config
struct are optional, how do I create a vector of Some
structs if they happen to be specified in the toml configuration?
For further context – here is an example config file
[add]
width = 16
[sub]
width = 32
[mul]
width = 32
granularity = 2
Would a schema check on the toml config be a better solution than trying to check for valid entries in the configuration file? Please drop some suggestions, thanks!
I tried to create a function for the Config
struct to return a vector of structs if they are specified, but I do not know how to do this for arbitrary entries (i.e. without explicitly checking each sub-struct of Config
struct).
Suhas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.