Code Monkey home page Code Monkey logo

serde_default_utils's Introduction

serde_default_utils

v

Overview

This is a simple set of functions to make your life easier while working with defaults in serde. Based on const generic parameters. Heavily inspired by discussions on issues about serde defaults, but mostly this one

Kudos

  • JohnTheCoolingFan posted this solution, I just made it available as crate and a macro that helps to generate another const generic function for any const generic type.
  • bytedream made a more powerful version of it, and although I still see const generic approach as more readable, I have to admit that for strings it's superior, hence - included under the feature

Example

    use serde_default_utils::*;
    use serde::{Deserialize, Serialize};

        const JSON: &str =
            r#"{
                "yes_or_no":false,
                "max":60,
                "delta":-77,
                "delimeter":"☀",
                "motto":"I matter"
            }"#;
        const INLINE_JSON: &str =
            r#"{
                "inline_motto":"I matter even more",
                "slice":["I", "matter", "even", "more"],
                "slice_u64":[9000, 8000, 10000, 7000]
            }"#;
    const EMPTY_JSON: &str = r#"{}"#;
    const MAX: u32 = 7;

    serde_default!(motto, "You matter");

    #[derive(Serialize, Deserialize, Default)]
    struct Config {
        #[serde(default = "default_bool::<true>")]
        yes_or_no: bool,
        #[serde(default = "default_i16::<-3>")]
        delta: i16,
        // you can even use consts right here
        #[serde(default = "default_u32::<MAX>")]
        max: u32,
        #[serde(default = "default_char::<'☀'>")]
        delimeter: char,
        #[serde(default = "default_motto")]
        motto: &'static str,
    }

    #[cfg(feature = "inline")]
    #[serde_inline_default]
    #[derive(Serialize, Deserialize, Default)]
    struct InlineConfig {
        #[serde_inline_default("You matter even more".to_string())]
        inline_motto: String,
        #[serde_inline_default(vec!["You", "matter", "even", "more"])]
        slice: Vec<&'static str>,
        #[serde_inline_default(vec![98712, 12346, 129389, 102937])]
        slice_u64: Vec<u64>,
    }

    fn main() {
        // existing json fields are not changed
        let config: Config = serde_json::from_str(JSON).unwrap();
        let s = serde_json::to_string(&config).unwrap();
        assert_eq!(r#"{"yes_or_no":false,"delta":-77,"max":60,"delimeter":"☀","motto":"I matter"}"#, &s);
        // if the field is not present - it is substituted with defaults
        let config: Config = serde_json::from_str(EMPTY_JSON).unwrap();
        let s = serde_json::to_string(&config).unwrap();
        assert_eq!(r#"{"yes_or_no":true,"delta":-3,"max":7,"delimeter":"☀","motto":"You matter"}"#, &s);
        // the default impl is just calling underlying type defaults unless you have a custom impl Default
        let config = Config::default();
        let s = serde_json::to_string(&config).unwrap();
        assert_eq!(r#"{"yes_or_no":false,"delta":0,"max":0,"delimeter":"\u0000","motto":""}"#, &s);

        // Inline
        #[cfg(feature = "inline")]
        {
        // existing json fields are not changed
        let config: InlineConfig = serde_json::from_str(INLINE_JSON).unwrap();
        let s = serde_json::to_string(&config).unwrap();
        assert_eq!(r#"{"inline_motto":"I matter even more","slice":["I","matter","even","more"],"slice_u64":[9000,8000,10000,7000]}"#, &s);
        // if the field is not present - it is substituted with defaults
        let config: InlineConfig = serde_json::from_str(EMPTY_JSON).unwrap();
        let s = serde_json::to_string(&config).unwrap();
        assert_eq!(r#"{"inline_motto":"You matter even more","slice":["You","matter","even","more"],"slice_u64":[98712,12346,129389,102937]}"#, &s);
        // the default impl is just calling underlying type defaults unless you have a custom impl Default
        let config = Config::default();
        let s = serde_json::to_string(&config).unwrap();
        assert_eq!(r#"{"inline_motto":"","slice":[],"slice_u64":[]}"#, &s);
        }
    }

License: MIT OR Apache-2.0

serde_default_utils's People

Contributors

alekspickle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

serde_default_utils's Issues

[FEATURE] String support

struct PlatformSettings {
    #[serde(default = "default_string::'some_String'")]
    minimum_client_version: String,
    max_sms_count: i64,
    max_user_count: i64,
}

Would be nice.

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.