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
```rust pub struct MyStruct { pub data: MyStructData, }
pub struct MyStructData { pub data: u32 } ```
Into this
```rust use nested_struct::*;
nested_struct! { pub struct MyStruct { pub data: MyStructData { pub data: u32 } } } ```
Creates a new struct that may feature nested fields.
```rust use nested_struct::*;
nestedstruct! { pub struct MyStruct { pub regularfield: u32, pub nestedfield: NestedField { pub innerfield: bool } } }
let _ = MyStruct { regularfield: 123, nestedfield: NestedField { inner_field: true, }, }; ```
Nesting is not limited to a single level. You can generate structs with multi-nested fields:
```rust use nested_struct::*;
nestedstruct! { pub struct MyStruct { pub nestedfield: NestedField { pub nestedfield: DeeperNestedField { pub innerfield: bool } } } }
let _ = MyStruct { nestedfield: NestedField { nestedfield: DeeperNestedField { inner_field: true, } }, }; ```
Like with a normal struct, nested fields can have attributes placed on them:
```rust use nested_struct::*;
nestedstruct! { pub struct MyStruct { pub regularfield: u32,
#[doc = "my nested field"]
pub nested_field: NestedField {
pub inner_field: bool
}
}
} ```
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:
```rust use nested_struct::*;
nestedstruct! { pub struct MyStruct { pub regularfield: u32,
#[doc = "my nested field"]
@nested(#[derive(Clone)])
pub nested_field: NestedField {
pub inner_field: bool
}
}
}
let nestedfield = NestedField { innerfield: true }; let _ = MyStruct { regularfield: 123, nestedfield: nested_field.clone(), }; ```
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.