This fork changes a few things:
#[opt_lenient]
to allow other attributes on a struct, e.g. #[model]
from wither.#[opt_skip_serializing_none]
to add #[serde(skip_serializing_if = "Option::is_none")]
to all fields.#[opt_some_priority]
make existing Option values take presence over None values in the optional struct.#[opt_passthrough]
on individual struct fields to include the next attribute on the field in the optional struct as well.This crate allows the user to generate a structure containing the same fields as the original struct but wrapped in Optionapply_options
. It consumes the generated optionalstruct, and for every Some(x) field, it assigns the original structure's value with the optionalstruct one.
Now that's some confusing explanation (my English skills could use some help), but basically:
```rust
struct Foo { meow: u32, woof: String, } ```
will generate:
```rust
struct OptionalFoo {
meow: Option
impl Foo { pub fn applyoptions(&mut self, optionalstruct: OptionalFoo) { if Some(field) = optional_struct.meow { self.meow = field; }
if Some(field) = optional_struct.woof {
self.woof = field;
}
}
} ```
You can use this to generate a configuration for you program more easily.
If you use toml-rs to parse your config file (using serde),
you'll need to wrap your values in Option
You can then easily handle default values for your config:
```rust
impl Config {
pub fn getuserconf() -> OptionalConfig {
toml::from_str
[keys]
github = 'xxxxxxxxxxxxxxxxx'
travis = 'yyyyyyyyyyyyyyyyy'
"#).unwrap()
}
}
let mut conf = Config::getdefault(); let userconf = Config::getuserconf(); conf.applyoptions(userconf); ```
```rust
```
```rust
```
```rust
struct Config {
timeout: Option
struct LogConfig { logfile: String, loglevel: usize, } ```
You'll find some examples in the tests folder (yes I know).