Optionable

Derive Macro Optionable 

Source
#[derive(Optionable)]
{
    // Attributes available to this derive:
    #[optionable]
    #[optionable_attr]
}
Available on crate feature derive only.
Expand description

Derive macro to derive the Optionable trait for structs/enums recursively by generating a type with all fields recursively replaced with Option versions. All non-required fields have to implement the Optionable trait. This trait is already implemented by this library for many primitive types, wrapper and container types.

§Type-level attributes (on the struct/enum level)

  • optionable_attr: Helper for the derive type-level attribute, for details see the derive attribute.
  • derive: Allows to specify derive attributes that should be attached to the generate optioned struct/enum. If you need to forward additional helper attributes to the generated type use attr_copy or optionable_attr with the attribute to forward as content (works for type and field attributes).
    #[derive(optionable)]
    #[optionable(derive(Deserialize, Serialize))]
    #[optionable_attr(serde(rename_all = "camelCase"))]
    struct MyStruct{
      #[optionable_attr(serde(rename = "firstName"))]
      name: String,
      surname: String,
    }
  • attr_copy: Attributes that should be copied over the optioned type. If no key filter is set all attributes with the given path are copied over.
    #[derive(optionable)]
    #[optionable(derive(Deserialize, Serialize))]
    #[optionable(attr_copy(path=serde,key=rename))]
    struct MyStruct{
      name: String,
      surname: String,
    }
  • no_convert: Does not derive the OptionableConvert implementation. Might be required if the target type involves smart pointers or unsized fields.
    #[derive(optionable)]
    #[optionable(no_convert)]
    struct MyStruct{}
  • suffix: The name of the generated optioned struct/enum will be <original><suffix> with suffix defaulting to "Opt". The suffix value can be adjusted via e.g. #[optionable(suffix="Ac")].
    #[derive(optionable)]
    #[optionable(suffix="Ac")]
    struct MyStruct{}

§Field-level attributes (for structs and struct-typed enum variants)

  • optionable_attr: Helper for the derive type-level attribute, for details see the derive attribute.
  • required: The annotated field will be kept as is and won’t be transformed into some optional variant for the derived optioned Struct.
    #[derive(optionable)]
    struct MyStruct{
      street: String; // will be an `Option<String>` in the derived `MyStructOpt`.
      #[optionable(required)]
      number: u32; // will also be a u32 in the derived `MyStructOpt`.
    }

§Kubernetes type-level attributes (on the struct/enum level)

Specialized Kubernetes specific type attributes to help deriving optioned types (in go called ApplyConfiguration) for Kubernetes resources/subfields.

  • kube: Intended to be placed on types implementing subfields of a kube CRD spec. Add derives for the optioned type Clone, Debug, PartialEq, Serialize, Deserialize and additionally for Structs Default. Also copies over any #[(serde(rename="...")] and #[(serde(rename_all="...")] attributes over to the optioned types.

    #[optionable(kube())]
    #[derive(Optionable, Clone, Debug, Deserialize, Serialize, JsonSchema)]
    pub struct MyCrdSpecTemplate {
      pub replicas: u32,
    }

    If you need to configure optionable explicitly setting this is e.g. for a Struct equivalent to:

    #[optionable(
      derive(Clone, Debug, PartialEq, Serialize, Deserialize),
      attr_copy(path=serde,key=rename)
    )]