#macro #configuration

macro optional_struct

Crate defining a macro that will generate, from a structure, another structure with only Option fields

5 releases

Uses old Rust 2015

0.2.0 Aug 20, 2018
0.1.3 Oct 24, 2017
0.1.2 Oct 24, 2017
0.1.1 Oct 23, 2017
0.1.0 Oct 23, 2017

#157 in Configuration

Download history 49/week @ 2022-04-26 38/week @ 2022-05-03 89/week @ 2022-05-10 29/week @ 2022-05-17 36/week @ 2022-05-24 56/week @ 2022-05-31 19/week @ 2022-06-07 8/week @ 2022-06-14 20/week @ 2022-06-21 40/week @ 2022-06-28 108/week @ 2022-07-05 253/week @ 2022-07-12 110/week @ 2022-07-19 108/week @ 2022-07-26 128/week @ 2022-08-02 284/week @ 2022-08-09

681 downloads per month
Used in holium

Apache-2.0

12KB
209 lines

OptionalStruct

Build Status Crates.io

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 optional_struct, and for every Some(x) field, it assigns the original structure's value with the optional_struct one.

Now that's some confusing explanation (my English skills could use some help), but basically:

#[derive(OptionalStruct)]
struct Foo {
	meow: u32,
	woof: String,
}

will generate:

struct OptionalFoo {
	meow: Option<u32>,
	woof: Option<String>,
}

impl Foo {
	pub fn apply_options(&mut self, optional_struct: 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:

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

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

let mut conf = Config::get_default();
let user_conf = Config::get_user_conf();
conf.apply_options(user_conf);

Features

  • Option inside the original structs are handled. The generated struct will have the exact same field, not an Option<Option>
  • You can rename the generated struct:
#[derive(OptionalStruct)]
#[optional_name = "FoorBarMeowWoof"]
  • You can also add derives to the generated struct:
#[derive(OptionalStruct)]
#[optional_derive(Serialize, Copy, Display)]
  • You can also nest your generated struct by mapping the original types to their new names:
#[derive(OptionalStruct)]
#[opt_nested_original(LogConfig)]
#[opt_nested_generated(OptionalLogConfig)]
struct Config {
    timeout: Option<u32>,
    log_config: LogConfig,
}

#[derive(OptionalStruct)]
struct LogConfig {
    log_file: String,
    log_level: usize,
}

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

Dependencies

~1.5MB
~38K SLoC