Coder Social home page Coder Social logo

merge-rs's Introduction

merge-rs

The merge crate provides the Merge trait that can be used to merge multiple values into one:

trait Merge {
    fn merge(&mut self, other: Self);
}

Merge can be derived for structs:

use merge::Merge;

#[derive(Merge)]
struct User {
    // Fields with the skip attribute are skipped by Merge
    #[merge(skip)]
    pub name: &'static str,

    // The strategy attribute is used to select the merge behavior
    #[merge(strategy = merge::option::overwrite_none)]
    pub location: Option<&'static str>,

    #[merge(strategy = merge::vec::append)]
    pub groups: Vec<&'static str>,
}

let defaults = User {
    name: "",
    location: Some("Internet"),
    groups: vec!["rust"],
};
let mut ferris = User {
    name: "Ferris",
    location: None,
    groups: vec!["mascot"],
};
ferris.merge(defaults);

assert_eq!("Ferris", ferris.name);
assert_eq!(Some("Internet"), ferris.location);
assert_eq!(vec!["mascot", "rust"], ferris.groups);

A merge strategy is a function with the signature fn merge<T>(left: &mut T, right: T) that merges right into left. The merge crate provides strategies for the most common types, but you can also define your own strategies.

The trait can be used to merge configuration from different sources, for example environment variables, multiple configuration files and command-line arguments, see the args.rs example.

Features

This crate has the following features:

  • derive (default): Enables the derive macro for the Merge trait using the merge_derive crate.
  • num (default): Enables the merge strategies in the num module that require the num_traits crate.
  • std (default): Enables the merge strategies in the hashmap and vec modules that require the standard library. If this feature is not set, merge is a no_std library.

Minimum Supported Rust Version

This crate supports Rust 1.36.0 or later.

Contact

For bug reports, patches, feature requests and other messages, please send a mail to ~ireas/[email protected] (archive) using the [merge-rs] prefix in the subject.

You can submit patches using git send-email, for example:

git send-email --to=~ireas/[email protected] --subject-prefix="PATCH merge-rs"

Please prefix the subject with PATCH merge-rs so that CI will be automatically run.

License

This project is dual-licensed under the Apache-2.0 and MIT licenses. The documentation and configuration files contained in this repository are licensed under the Creative Commons Zero license. You can find a copy of the license texts in the LICENSES directory.

merge-rs complies with version 3.0 of the REUSE specification.

merge-rs's People

Contributors

robinkrahl avatar gabrielhamel avatar jyn514 avatar

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.