#serde #flatten #path #long #macro #field #structure

macro serde_flat_path

A macro for expanding long structure chains within serde

4 releases

0.2.0 Jun 16, 2024
0.1.2 Apr 12, 2023
0.1.1 Apr 10, 2023
0.1.0 Apr 10, 2023

#2054 in Procedural macros

Download history 7/week @ 2024-07-22 6/week @ 2024-07-29 2/week @ 2024-08-05 16/week @ 2024-09-23 13/week @ 2024-09-30 7/week @ 2024-10-07

279 downloads per month
Used in cargo-leet

MIT license

22KB
398 lines

serde_flat_path

build_status crates.io

This crate was designed to solve the problem of needing to manually create placeholder structs or untyped values (such as serde_json::Value). For example, take this json file:

{
  "title": "Foo",
  "foo": true,
  "config": {
    "bar": {
      "name": "foo bar"
    }
  }
}

Using serde derive, you would likely arrive at something similar to the code shown below. As you can see, you may need to create separate structures for config and bar despite only containing a single field of interest each.

#[derive(Deserialize)]
struct Foo {
    title: String,
    foo: bool,
    config: FooConfig,
}

#[derive(Deserialize)]
struct FooConfig {
    bar: Bar,
}

#[derive(Deserialize)]
struct Bar {
    name: String,
}

To solve this problem, this crate provides a macro for shortening a long path to a single field.

#[flat_path]
#[derive(Deserialize)]
struct Foo {
    title: String,
    foo: bool,
    #[flat_path("config.bar.name")]
    bar_name: String,
}

Usage

flat_path can be applied to any named field within a struct or enum. The #[flat_path] attribute must be applied before the serialize/deserialize #[derive(...)] so that it can apply the necessary serde attributes before serde performs macro expansion for its derive macros. Similar to derive macros, the original type is not altered.

For cases where field names contain . or additional verbosity is desired, #[flat_path("a.b.c")] may also be written as #[flat_path(path = ["a", "b", "c"])]. These two forms are equivalent and no distinction is made between regarding macro expansion.

#[serde(...)] attributes are also moved from #[flat_path(...)] fields to the final field within the path. During this process, the order of attributes remains the same.

Dependencies

~1.5MB
~37K SLoC