10 releases

0.5.0 Mar 23, 2025
0.5.0-beta.1 Dec 15, 2024
0.4.1 Apr 20, 2023
0.4.0 Feb 22, 2023
0.1.0 Apr 11, 2022

#163 in Procedural macros

Download history 805/week @ 2025-02-02 635/week @ 2025-02-09 658/week @ 2025-02-16 738/week @ 2025-02-23 937/week @ 2025-03-02 853/week @ 2025-03-09 947/week @ 2025-03-16 1084/week @ 2025-03-23 582/week @ 2025-03-30 770/week @ 2025-04-06 555/week @ 2025-04-13 779/week @ 2025-04-20 719/week @ 2025-04-27 565/week @ 2025-05-04 440/week @ 2025-05-11 349/week @ 2025-05-18

2,124 downloads per month
Used in 16 crates (10 directly)

MIT license

62KB
2K SLoC

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?

struct Parent {
    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! {
    enum Token {
        Identifier(struct {
            name: String,
        }),
        Punctuation(struct {
            character: char,
        }),
    }
}
  • my original use case: conveniently write kubernetes custom resources with kube.
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<struct KafkaListener {
                name: String,
                port: u16,
                r#type: String,
                tls: bool,
            }>,
            config: HashMap<String, JsonValue>,
            storage: struct {
                r#type: String,
                volumes: Vec<struct Volume {
                    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>>,
        },
    }
}

Dependencies

~270KB