Skip to content

Rust nested structs

License

Notifications You must be signed in to change notification settings

jcaesar/structstruck

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

73 Commits

Repository files navigation

StructStruck

Ever had a deeply nested JSON struct

{"outer":{"middle":{"inner":{"foo": "bar" } } } }

and wanted to write the Rust structs to handle that data just in the same nested way?

structParent{outer:struct{middle:struct{inner:struct{foo:String,}}}}

This proc macro crate allows exactly that. Check the docs on how exaclty.

For illustration, some more usecases:

  • an enum where every variant has its own struct, named exactly the same as the variant.
structstruck::strike! {enumToken{Identifier(struct{ name:String,}),Punctuation(struct{ character:char,}),}}
  • my original use case: conveniently write kubernetes custom resources with kube.
structstruck::strike! { #[structstruck::each[derive(Deserialize,Serialize,Clone,Debug,Validate,JsonSchema)]] #[structstruck::each[serde(rename_all = "camelCase")]] #[derive(CustomResource)] #[kube( group = "kafka.strimzi.io", version = "v1beta2", kind = "Kafka", namespaced )]structKafkaSpec{ kafka:structKafkaCluster{ #[validate(length(min = 1))] version:String, #[validate(range(min = 1))] replicas:u32, listeners:Vec<structKafkaListener{ name:String, port:u16, r#type:String, tls:bool,}>, config:HashMap<String,JsonValue>, storage:struct{ r#type:String, volumes:Vec<structVolume{ id:Option<u64>, r#type:String, size:String, delete_claim:bool,}>,},}, zookeeper:struct{ #[validate(range(min = 1))] replicas:u32, storage:Volume,}, entity_operator:struct{ topic_operator:Option<HashMap<String,JsonValue>>, user_operator:Option<HashMap<String,JsonValue>>,},}}

About

Rust nested structs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 5

Languages