MCPcopy Index your code
hub / github.com/bytedream/serde-inline-default

github.com/bytedream/serde-inline-default @v1.0.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.0.1 ↗ · + Follow
18 symbols 24 edges 7 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

serde-inline-default ci crates.io crates.io downloads docs

A tiny crate to set default values for serde struct fields via inline attribute declaration.

Overview

This crate is an approach to do what serde-rs/serde#368 purposes: Defining default values for struct fields via inline declaration instead of creating a separate function for it.

So instead of writing something like this, which can get very verbose quickly with many fields:

#[derive(Deserialize)]
struct Test {
    #[serde(default = "value_default")]
    value: u32
}

fn value_default() -> u32 { 42 }

you can just do this:

#[serde_inline_default]
#[derive(Deserialize)]
struct Test {
    #[serde_inline_default(42)]
    value: u32
}

[!IMPORTANT] #[serde_inline_default] must be set before #[derive(Deserialize)]/#[derive(Serialize)]!

Internally, #[serde_inline_default(...)] gets expanded to a function which returns the set value and the attribute is replaced with #[serde(default = "<function name>")]. So this macro is just some syntax sugar for you, but can get quite handy if you want to keep your code clean or write declarative macros / macro_rules!.

Alternatives

This crate isn't perfect. Thus, you might be more satisfied with alternatives serde provides.

With #[serde(default)] + impl Default on a struct, serde uses the default implementation of the struct to get default values for each field (docs):

#[derive(Deserialize)]
#[serde(default)]
struct Test {
    value: u32
}

impl Default for Test {
    fn default() -> Self {
        Self {
            value: 42
        }
    }
}

If you still need/want serde-inline-default features, you also can combine them with #[serde(default)) and impl Default:

#[serde_inline_default]
#[derive(Deserialize)]
#[serde(default)]
struct Test {
    value: u32,
    #[serde_inline_default(0)]
    other_value: u32,
}

impl Default for Test {
    fn default() -> Self {
        Self {
            value: 42,
            other_value: 42
        }
    }
}

License

This project is licensed under either of the following licenses, at your option:

Core symbols most depended-on inside this repo

check_field_for_default_expr
called by 2
src/utils.rs
type_lifetimes_to_static
called by 1
src/utils.rs
expand_struct
called by 1
src/expand.rs
expand_enum
called by 1
src/expand.rs
serde_inline_default
called by 0
src/lib.rs
native_default
called by 0
tests/struct.rs
conditional_compilation
called by 0
tests/struct.rs
main
called by 0
examples/basic.rs

Shape

Function 12
Class 5
Enum 1

Languages

Rust100%

Modules by API surface

tests/struct.rs8 symbols
tests/enum.rs2 symbols
src/utils.rs2 symbols
src/expand.rs2 symbols
examples/basic.rs2 symbols
src/lib.rs1 symbols
examples/macro_rules.rs1 symbols

For agents

$ claude mcp add serde-inline-default \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact