8 unstable releases (3 breaking)

0.4.1 Apr 20, 2023
0.4.0 Feb 22, 2023
0.3.0 Nov 19, 2022
0.2.2 Oct 14, 2022
0.1.0 Apr 11, 2022

#255 in Procedural macros

Download history 1274/week @ 2024-07-24 1252/week @ 2024-07-31 1278/week @ 2024-08-07 1250/week @ 2024-08-14 783/week @ 2024-08-21 1132/week @ 2024-08-28 1334/week @ 2024-09-04 924/week @ 2024-09-11 955/week @ 2024-09-18 1065/week @ 2024-09-25 804/week @ 2024-10-02 1067/week @ 2024-10-09 1001/week @ 2024-10-16 1217/week @ 2024-10-23 1324/week @ 2024-10-30 1027/week @ 2024-11-06

4,698 downloads per month
Used in 9 crates (6 directly)

MIT license

45KB
1K 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! {
    #[strikethrough[derive(Deserialize, Serialize, Clone, Debug, Validate, JsonSchema)]]
    #[strikethrough[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

~300KB