#structs #flatten #enums #macro #nest

macro nestruct

A Rust library that provides macros to easily flatten or nest structs and enums in your code

1 unstable release

0.1.0 Mar 19, 2023

#1255 in Procedural macros

Download history 27/week @ 2023-12-25 5/week @ 2024-01-22 5/week @ 2024-01-29 21/week @ 2024-02-12 36/week @ 2024-02-19 33/week @ 2024-02-26 9/week @ 2024-03-04 34/week @ 2024-03-11 24/week @ 2024-03-18

106 downloads per month

MIT license

15KB
242 lines

Nestruct: A Rust Library for Flattening and Nesting Structs

Nestruct is a Rust library that provides macros to easily flatten or nest structs and enums in your code. It can help simplify the representation of complex data structures, such as web API response types. Nestruct allows you to write concise code and automatically generates the equivalent standard Rust structs and enums.

Features

  • nestruct::flatten!: Flatten nested structs and enums into a single namespace.
  • nestruct::nest!: Nest flattened structs and enums into their original hierarchical structure.

Example

Using the flatten! macro, you can transform the following nested structure:

mod foo_bar {
    nestruct::flatten! {
        FooBar {
            /// foo
            foo: String?,
            /// bar
            bar: [[usize]?]?,
            baz: {
                qux: [{
                    quux: (usize, usize)?,
                    quuz: String?,
                }],
                corge: String,
            },
            grault: {
                garply,
                waldo { wubble: String },
                wubble(usize?, usize?)
            }?,
        }
    }
}

Into the following flattened structure:

mod foo_bar {
    pub struct Qux {
        pub quux: Option<(usize, usize)>,
        pub quuz: Option<String>,
    }
    pub struct Baz {
        pub qux: Vec<Qux>,
        pub corge: String,
    }
    pub enum Grault {
        Garply,
        Waldo { wubble: String },
        Wubble(Option<usize>, Option<usize>),
    }
    pub struct FooBar {
        /// foo
        pub foo: Option<String>,
        /// bar
        pub bar: Option<Vec<Option<Vec<usize>>>>,
        pub baz: Baz,
        pub grault: Option<Grault>,
    }
}

Similarly, using the nest! macro, you can nest a flattened structure like this:

nestruct::nest! {
    FooBar {
        /// foo
        foo: String?,
        /// bar
        bar: [[usize]?]?,
        baz: {
            qux: [{
                quux: (usize, usize)?,
                quuz: String?,
            }],
            corge: String,
        },
        grault: {
            garply,
            waldo { wubble: String },
            wubble(usize?, usize?)
        }?,
    }
}

Into the following nested structure:

pub mod foo_bar {
    pub mod baz {
        pub mod qux {
            pub struct Qux {
                pub quux: Option<(usize, usize)>,
                pub quuz: Option<String>,
            }
        }
        pub struct Baz {
            pub qux: Vec<qux::Qux>,
            pub corge: String,
        }
    }
    pub mod grault {
        pub enum Grault {
            Garply,
            Waldo { wubble: String },
            Wubble(Option<usize>, Option<usize>),
        }
    }
    pub struct FooBar {
        /// foo
        pub foo: Option<String>,
        /// bar
        pub bar: Option<Vec<Option<Vec<usize>>>>,
        pub baz: baz::Baz,
        pub grault: Option<grault::Grault>,
    }
}

Installation

Add the following dependency to your Cargo.toml:

[dependencies]
nestruct = "0.1.0"

License

Nestruct is distributed under the MIT License. See the LICENSE file for more information.

Dependencies

~2MB
~42K SLoC