4 releases

0.1.3 Aug 8, 2024
0.1.2 Jan 5, 2024
0.1.1 Dec 1, 2023
0.1.0 Dec 1, 2023

#1167 in Parser implementations

45 downloads per month
Used in swc_sdk

Apache-2.0

14KB
365 lines

default-from-serde

Derive Default using serde::Deserialize! No mismatch between Default and Deserialize anymore!

Usage

Cargo.toml:

default-from-serde = "0.1"

See docs.rs for the Rust code.

License

APACHE-2.0.

Some source code are copied from serde_json. This library is practically deserialize using serde_json with {}.


lib.rs:

This crate provides a derive macro named SerdeDefault which derives Default from serde::Deserialize.

Usage

use default_from_serde::SerdeDefault;

#[derive(SerdeDefault, Deserialize)]
pub struct ComplexTypewithDefault {
    #[serde(default)]
    pub a: i32,
    #[serde(default = "default_b")]
    pub b: String,
    #[serde(default)]
    pub c: Vec<i32>,
}

fn default_b() -> String {
    "default".to_string()
}

fn main() {
    let x = ComplexTypewithDefault::default();

    assert_eq!(x.b, "default");
}

Dependencies

~0.3–1MB
~21K SLoC