https://github.com/jcaesar/structstruck
Rust nested structs
https://github.com/jcaesar/structstruck
Last synced: 10 months ago
JSON representation
Rust nested structs
- Host: GitHub
- URL: https://github.com/jcaesar/structstruck
- Owner: jcaesar
- License: mit
- Created: 2022-04-10T08:02:17.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2025-03-23T14:43:08.000Z (10 months ago)
- Last Synced: 2025-03-28T18:14:14.284Z (10 months ago)
- Language: Rust
- Size: 69.3 KB
- Stars: 55
- Watchers: 3
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# StructStruck
Ever had a deeply nested JSON struct
```json
{
"outer": {
"middle": {
"inner": {
"foo": "bar"
}
}
}
}
```
and wanted to write the Rust structs to handle that data just in the same nested way?
```rust
struct Parent {
outer: struct {
middle: struct {
inner: struct {
foo: String,
}
}
}
}
```
This proc macro crate allows exactly that.
Check the [docs](https://docs.rs/structstruck) on how exaclty.
For illustration, some more usecases:
* an enum where every variant has its own struct, named exactly the same as the variant.
```rust
structstruck::strike! {
enum Token {
Identifier(struct {
name: String,
}),
Punctuation(struct {
character: char,
}),
}
}
```
* my original use case: conveniently write kubernetes custom resources with `kube`.
```rust
structstruck::strike! {
#[strctstruck::each[derive(Deserialize, Serialize, Clone, Debug, Validate, JsonSchema)]]
#[strctstruck::each[serde(rename_all = "camelCase")]]
#[derive(CustomResource)]
#[kube(
group = "kafka.strimzi.io",
version = "v1beta2",
kind = "Kafka",
namespaced
)]
struct KafkaSpec {
kafka: struct KafkaCluster {
#[validate(length(min = 1))]
version: String,
#[validate(range(min = 1))]
replicas: u32,
listeners: Vec,
config: HashMap,
storage: struct {
r#type: String,
volumes: Vec,
r#type: String,
size: String,
delete_claim: bool,
}>,
},
},
zookeeper: struct {
#[validate(range(min = 1))]
replicas: u32,
storage: Volume,
},
entity_operator: struct {
topic_operator: Option>,
user_operator: Option>,
},
}
}
```