nested-struct

Crates.ionested-struct
lib.rsnested-struct
version0.1.0
sourcesrc
created_at2023-02-04 07:12:00.833985
updated_at2023-02-04 07:12:00.833985
descriptionCreate nested structs using a macro
homepagehttps://github.com/chipsenkbeil/nested-struct
repositoryhttps://github.com/chipsenkbeil/nested-struct
max_upload_size
id776237
size20,516
Chip Senkbeil (chipsenkbeil)

documentation

README

Nested Struct

Ever wanted to be able to create a data structure that contains nested data? Do you find yourself creating many individual structs simply to combine them together? Well, this is the library for you!

Transform this

pub struct MyStruct {
    pub data: MyStructData,
}

pub struct MyStructData {
    pub data: u32
}

Into this

use nested_struct::*;

nested_struct! {
    pub struct MyStruct {
        pub data: MyStructData {
            pub data: u32
        }
    }
}

Basic usage

Creates a new struct that may feature nested fields.

use nested_struct::*;

nested_struct! {
    pub struct MyStruct {
        pub regular_field: u32,
        pub nested_field: NestedField {
            pub inner_field: bool
        }
    }
}

let _ = MyStruct {
    regular_field: 123,
    nested_field: NestedField {
        inner_field: true,
    },
};

Deeply-nested structs

Nesting is not limited to a single level. You can generate structs with multi-nested fields:

use nested_struct::*;

nested_struct! {
    pub struct MyStruct {
        pub nested_field: NestedField {
            pub nested_field: DeeperNestedField {
                pub inner_field: bool
            }
        }
    }
}

let _ = MyStruct {
    nested_field: NestedField {
        nested_field: DeeperNestedField {
            inner_field: true,
        }
    },
};

Applying field attributes to fields that are nested structs

Like with a normal struct, nested fields can have attributes placed on them:

use nested_struct::*;

nested_struct! {
    pub struct MyStruct {
        pub regular_field: u32,

        #[doc = "my nested field"]
        pub nested_field: NestedField {
            pub inner_field: bool
        }
    }
}

Applying struct-level attributes to nested structs

If you want to apply attributes on the generated, nested struct, you need to use the @nested marker. This can be used multiple times, but must occur AFTER any field-specific attributes:

use nested_struct::*;

nested_struct! {
    pub struct MyStruct {
        pub regular_field: u32,

        #[doc = "my nested field"]
        @nested(#[derive(Clone)])
        pub nested_field: NestedField {
            pub inner_field: bool
        }
    }
}

let nested_field = NestedField { inner_field: true };
let _ = MyStruct {
    regular_field: 123,
    nested_field: nested_field.clone(),
};

License

This project is licensed under either of

Apache License, Version 2.0, (LICENSE-APACHE or apache-license) MIT license (LICENSE-MIT or mit-license) at your option.

Commit count: 6

cargo fmt