OptionalStruct

Goal

This crate allows the user to generate a structure containing the same fields as the original struct but wrapped in Option. A method is also implemented for the original struct, apply_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

[derive(OptionalStruct)]

struct Foo { meow: u32, woof: String, } ```

will generate:

```rust struct OptionalFoo { meow: Option, woof: 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;
    }

}

} ```

Usage

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, or you need them present in the config file. With this crate, you can easily generate your whole Config struct with an Option wrap for each field. This means that if a config is missing in the file, you'll get a None.

You can then easily handle default values for your config:

```rust impl Config { pub fn getuserconf() -> OptionalConfig { toml::from_str(r#" ip = '127.0.0.1'

        [keys]
        github = 'xxxxxxxxxxxxxxxxx'
        travis = 'yyyyyyyyyyyyyyyyy'
        "#).unwrap()
}

}

let mut conf = Config::getdefault(); let userconf = Config::getuserconf(); conf.applyoptions(userconf); ```

Features

[optional_name = "FoorBarMeowWoof"]

* You can also add derives to the generated struct: rust

[derive(OptionalStruct)]

[optional_derive(Serialize, Copy, Display)]

* You can also nest your generated struct by mapping the original types to their new names: rust

[derive(OptionalStruct)]

[optnestedoriginal(LogConfig)]

[optnestedgenerated(OptionalLogConfig)]

struct Config { timeout: Option, log_config: LogConfig, }

[derive(OptionalStruct)]

struct LogConfig { logfile: String, loglevel: usize, } ```

You'll find some examples in the tests folder (yes I know).