8 releases (4 breaking)
0.5.2 | Nov 21, 2024 |
---|---|
0.4.2 | Nov 18, 2024 |
0.3.3 | Nov 17, 2024 |
0.2.0 | Nov 16, 2024 |
0.1.6 | Nov 15, 2024 |
#1313 in Rust patterns
1,381 downloads per month
Used in kuliya
28KB
339 lines
nest_struct
Nest struct and enum definitions with minimal syntax changes in Rust
Example
use nest_struct::nest_struct;
#[nest_struct]
struct Post {
title: String,
summary: String,
author: nest! {
name: String,
handle: String,
},
}
See expanded code
struct Post {
title: String,
summary: String,
author: PostAuthor,
}
struct PostAuthor {
name: String,
handle: String,
}
You can also overwrite inner struct name, by passing the name itself as macro instead of nest!
:
use nest_struct::nest_struct;
#[nest_struct]
struct Post {
title: String,
summary: String,
author: Author! {
name: String,
handle: String,
},
}
See expanded code
struct Post {
title: String,
summary: String,
author: Author,
}
struct Author {
name: String,
handle: String,
}
Or, you can open a block and define struct like normal Rust code for full flexibility:
use nest_struct::nest_struct;
#[nest_struct]
struct Post {
title: String,
summary: String,
author: nest! {
/// doc comment for Author struct
#[derive(Debug)]
struct Author {
name: String,
handle: String,
}
},
}
See expanded code
struct Post {
title: String,
summary: String,
author: Author,
}
/// doc comment for Author struct
#[derive(Debug)]
struct Author {
name: String,
handle: String,
}
Another example calling Pokemon API
use nest_struct::nest_struct;
// Define a struct with nested struct definitions all in one place
// with minimal syntax changes.
#[nest_struct]
#[derive(serde::Deserialize)]
struct APIResponse {
id: u32,
name: String,
abilities: Vec<nest! {
ability: nest! { name: String, url: String },
is_hidden: bool,
slot: u32,
},
>,
}
let body = reqwest::blocking::get("https://pokeapi.co/api/v2/pokemon/ditto").unwrap().text().unwrap();
let api_response: APIResponse = serde_json::from_str(&body).unwrap();
assert_eq!(api_response.name, "ditto");
// Access nested struct fields
assert_eq!(api_response.abilities.first().unwrap().ability.name, "limber");
For more examples, see the ./tests/cases
directory.
Features
- deep nesting (no theoretical limit).
- nest
struct
inside anotherstruct
. - nest
enum
inside anotherenum
. - nest
enum
inside astruct
and vice-versa. - inherit
derive
and other attribute macros from rootstruct
. - auto-generate inner
struct
names. - overwrite the auto-generated inner struct name.
Feature parity with native Rust code:
-
impl
block on innerstruct
s. - define
derive
and other attribute macros individually per innerstruct
. - define doc comments individually per inner
struct
. - useful compiler error messages.
- support generic types.
- support lifetimes.
Contributing
Contributions are welcome, please read CONTRIBUTING.md
to get started.
License
Licensed under MIT (twitter: @zibanpirate).
Dependencies
~0.6–1.1MB
~21K SLoC