Skip to content

Create derive macro for variants (enums) #1846

Open
@heaths

Description

@heaths

Because Azure services define a lot of enums and, despite generating most of our clients, hand-authored enums could benefit from consistency with our guidelines, we should create a derive macro for enums called, tentatively, Variant e.g.,

#[derive(Variant)]
#[variant(fixed, rename_all = "camelCase")] // optional; default is extensible per <https://aka.ms/azapi/guidelines>
pub enum DaysOfWeek {
    Monday,
    Tuesday,
    // ...
}

#[derive(Variant)]
#[variant(rename_all = "camelCase")]
pub enum Metasyntactic {
    Foo,
    Bar,
    #[variant(other)] // UnknownValue(String) is used by default, actually
    UnknownValue(String),
}

The Variant derive macro would:

  • Derive Clone, Debug, PartialEq, and Eq
  • Implement serde::Serialize and serde::Deserialize
  • Implement FromStr. Extensible enums will define FromStr::Err as std::convert::Infallible but fixed enums using azure_core::ErrorKind::DataConversion. This will be used in serde::Deserialize.
  • Implement Display. This will be used in serde::Serialize.

See #1690

Question(s) on top of mind:

  • Should we try to duplicate necessary serde attributes like rename_all? Off hand, that may be all we need.
  • Should we define a Variant trait for which we can add blanket implementations that are easier to update than a derive macro? Is that even practical? Seems we'd need a Variant trait to return tuples of tokens to values which might make the whole thing inefficient.
  • #[variant(other)] is meant to be equivalent to #[serde(untagged)] but I feel is more intuitive; or, should we make these the same for familiarity?

Metadata

Metadata

Assignees

Labels

Azure.CoreThe azure_core crateClientThis issue points to a problem in the data-plane of the library.help wantedThis issue is tracking work for which community contributions would be welcomed and appreciated

Type

Projects

Status

Not Started

Relationships

None yet

Development

No branches or pull requests

Issue actions