Git Product home page Git Product logo

optionalstruct's Introduction

Changes in Fork

This fork changes a few things:

  • Allows docstrings for struct values.
  • Allow setting #[opt_lenient] to allow other attributes on a struct, e.g. #[model] from wither.
  • Allow setting #[opt_skip_serializing_none] to add #[serde(skip_serializing_if = "Option::is_none")] to all fields.
  • Allow setting #[opt_some_priority] make existing Option values take presence over None values in the optional struct.
  • Allow setting #[opt_passthrough] on individual struct fields to include the next attribute on the field in the optional struct as well.

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).

optionalstruct's People

Contributors

insertish avatar plesur avatar

Stargazers

AGATHOS avatar

Watchers

James Cloos avatar  avatar

Forkers

revoltchat

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.