wick_interface_types/types/
enum_def.rs

1use serde::{Deserialize, Serialize};
2
3use crate::is_false;
4
5/// Signatures of enum type definitions.
6#[derive(Debug, Clone, Default, Serialize, Deserialize, Eq)]
7#[must_use]
8#[non_exhaustive]
9pub struct EnumDefinition {
10  /// The name of the enum.
11  pub name: String,
12  /// The variants in the enum.
13  #[serde(default, skip_serializing_if = "Vec::is_empty")]
14  pub variants: Vec<EnumVariant>,
15  /// The optional description of the enum.
16  #[serde(default, skip_serializing_if = "Option::is_none")]
17  pub description: Option<String>,
18  /// Whether this type is imported.
19  #[serde(default, skip_serializing_if = "is_false")]
20  pub imported: bool,
21}
22
23impl EnumDefinition {
24  /// Constructor for [EnumDefinition]
25  pub fn new<T: Into<String>>(name: T, variants: Vec<EnumVariant>, description: Option<String>) -> Self {
26    Self {
27      name: name.into(),
28      variants,
29      imported: false,
30      description,
31    }
32  }
33}
34
35impl PartialEq for EnumDefinition {
36  fn eq(&self, other: &Self) -> bool {
37    self.name == other.name && self.variants == other.variants
38  }
39}
40
41#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
42#[must_use]
43#[non_exhaustive]
44/// An enum variant definition
45pub struct EnumVariant {
46  /// The name of the variant.
47  pub name: String,
48  /// The index of the variant.
49  #[serde(default, skip_serializing_if = "Option::is_none")]
50  pub index: Option<u32>,
51  /// The optional value of the variant.
52  #[serde(default, skip_serializing_if = "Option::is_none")]
53  pub value: Option<String>,
54  /// The optional description of the variant.
55  #[serde(default, skip_serializing_if = "Option::is_none")]
56  pub description: Option<String>,
57}
58
59impl EnumVariant {
60  /// Constructor for [EnumVariant]
61  pub fn new<T: Into<String>>(name: T, index: Option<u32>, value: Option<String>, description: Option<String>) -> Self {
62    Self {
63      name: name.into(),
64      index,
65      value,
66      description,
67    }
68  }
69}