Skip to main content

rubo4e/generated/v202607/
rollencodetyp.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
2#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
3#[cfg_attr(
4    feature = "strum",
5    derive(strum::EnumString, strum::EnumIter, strum::IntoStaticStr)
6)]
7#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
8#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
9/// Gibt den Codetyp einer Rolle, beispielsweise einer Marktrolle, an.
10#[non_exhaustive]
11pub enum Rollencodetyp {
12    #[cfg_attr(feature = "serde", serde(rename = "BDEW"))]
13    #[cfg_attr(feature = "strum", strum(serialize = "BDEW"))]
14    Bdew,
15    #[cfg_attr(feature = "serde", serde(rename = "DVGW"))]
16    #[cfg_attr(feature = "strum", strum(serialize = "DVGW"))]
17    Dvgw,
18    #[cfg_attr(feature = "serde", serde(rename = "GLN"))]
19    #[cfg_attr(feature = "strum", strum(serialize = "GLN"))]
20    Gln,
21    /// Unknown or future variant — produced when deserializing a value
22    /// that is not yet known to this version of the library.
23    #[cfg_attr(feature = "serde", serde(other, rename = "UNKNOWN"))]
24    #[cfg_attr(feature = "strum", strum(serialize = "UNKNOWN"))]
25    Unknown,
26}
27impl Rollencodetyp {
28    /// All variants defined by the BO4E schema, in declaration order.
29    ///
30    /// Excludes the forward-compatibility [`Rollencodetyp::Unknown`] catch-all, so this
31    /// is exactly the set of values that appear on the wire.  Available **without**
32    /// the `strum` feature — use it to drift-guard SQL `CHECK` lists and mappings.
33    pub const VARIANTS: &'static [Self] = &[Self::Bdew, Self::Dvgw, Self::Gln];
34    /// Number of schema-defined variants (equal to `VARIANTS.len()`), excluding the
35    /// [`Rollencodetyp::Unknown`] catch-all.  Stable for this schema version.
36    pub const COUNT: usize = Self::VARIANTS.len();
37    /// Returns an iterator over all **known** variants of `Rollencodetyp`.
38    ///
39    /// Yields only variants that correspond to values defined in the BO4E schema
40    /// (i.e. [`Self::VARIANTS`]), never the [`Rollencodetyp::Unknown`] catch-all.
41    /// Available **without** the `strum` feature.
42    ///
43    /// # Example
44    /// ```
45    /// # use rubo4e::current::Rollencodetyp;
46    /// // Never yields the `Unknown` catch-all, so the count matches `COUNT`.
47    /// assert_eq!(Rollencodetyp::iter_known().count(), Rollencodetyp::COUNT);
48    /// assert!(Rollencodetyp::iter_known().all(|v| v.is_known()));
49    /// ```
50    pub fn iter_known() -> impl Iterator<Item = Self> + Clone {
51        Self::VARIANTS.iter().copied()
52    }
53    /// Returns the canonical BO4E wire string (SCREAMING_SNAKE_CASE) for this value.
54    ///
55    /// [`Rollencodetyp::Unknown`] renders as `"UNKNOWN"`, matching its serialized form.
56    pub const fn as_wire(&self) -> &'static str {
57        match self {
58            Self::Bdew => "BDEW",
59            Self::Dvgw => "DVGW",
60            Self::Gln => "GLN",
61            Self::Unknown => "UNKNOWN",
62        }
63    }
64    /// **Strictly** parses a BO4E wire string into a known variant.
65    ///
66    /// Unlike the lenient `serde` / [`FromStr`](std::str::FromStr) path — which maps
67    /// any unrecognized value (a typo, a legacy code, or a value from a newer schema)
68    /// to [`Rollencodetyp::Unknown`] — this returns
69    /// [`Err`](crate::error::UnknownVariant) for values not defined in this schema
70    /// version, including the literal `"UNKNOWN"`.  Use it at the ingest boundary to
71    /// reject bad values instead of silently degrading them.
72    ///
73    /// # Example
74    /// ```
75    /// # use rubo4e::current::Rollencodetyp;
76    /// assert_eq!(Rollencodetyp::from_wire("BDEW"), Ok(Rollencodetyp::Bdew));
77    /// // Out-of-schema values are rejected rather than degraded:
78    /// assert!(Rollencodetyp::from_wire("NOT_A_REAL_VALUE").is_err());
79    /// // …including the `Unknown` catch-all's own wire spelling:
80    /// assert!(Rollencodetyp::from_wire("UNKNOWN").is_err());
81    /// ```
82    pub fn from_wire(s: &str) -> Result<Self, crate::error::UnknownVariant> {
83        match s {
84            "BDEW" => Ok(Self::Bdew),
85            "DVGW" => Ok(Self::Dvgw),
86            "GLN" => Ok(Self::Gln),
87            other => Err(crate::error::UnknownVariant::new(other)),
88        }
89    }
90    /// Returns `true` if this value is the forward-compatibility
91    /// [`Rollencodetyp::Unknown`] catch-all (an out-of-schema value).
92    pub const fn is_unknown(&self) -> bool {
93        matches!(self, Self::Unknown)
94    }
95    /// Returns `true` if this value is a known, schema-defined variant.
96    pub const fn is_known(&self) -> bool {
97        !self.is_unknown()
98    }
99}
100impl std::fmt::Display for Rollencodetyp {
101    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102        f.write_str(self.as_wire())
103    }
104}
105impl AsRef<str> for Rollencodetyp {
106    fn as_ref(&self) -> &str {
107        self.as_wire()
108    }
109}
110#[cfg(feature = "versioned")]
111impl crate::bo4e_enum_sealed::Sealed for Rollencodetyp {}
112#[cfg(feature = "versioned")]
113impl crate::Bo4eEnum for Rollencodetyp {
114    const VARIANTS: &'static [Self] = Self::VARIANTS;
115    const COUNT: usize = Self::COUNT;
116    fn as_wire(&self) -> &'static str {
117        Self::as_wire(self)
118    }
119    fn from_wire(s: &str) -> Result<Self, crate::error::UnknownVariant> {
120        Self::from_wire(s)
121    }
122    fn is_unknown(&self) -> bool {
123        Self::is_unknown(self)
124    }
125}
126#[cfg(feature = "versioned")]
127impl crate::Bo4eStrict for Rollencodetyp {
128    fn collect_unknown_enums(&self, path: &str, out: &mut Vec<String>) {
129        if self.is_unknown() {
130            out.push(path.to_owned());
131        }
132    }
133}
134#[cfg(feature = "sqlx")]
135impl sqlx::Type<sqlx::Postgres> for Rollencodetyp {
136    fn type_info() -> sqlx::postgres::PgTypeInfo {
137        <String as sqlx::Type<sqlx::Postgres>>::type_info()
138    }
139}
140/// Encodes as the canonical BO4E wire string, borrowed from `as_wire` — no
141/// intermediate `String` or `serde_json::Value` is allocated.
142#[cfg(feature = "sqlx")]
143impl<'q> sqlx::Encode<'q, sqlx::Postgres> for Rollencodetyp {
144    fn encode_by_ref(
145        &self,
146        buf: &mut <sqlx::Postgres as sqlx::Database>::ArgumentBuffer<'q>,
147    ) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
148        let s: &str = self.as_wire();
149        <&str as sqlx::Encode<'q, sqlx::Postgres>>::encode_by_ref(&s, buf)
150    }
151}
152/// Decodes leniently, matching the `serde` path: a value the schema does not
153/// define becomes [`Rollencodetyp::Unknown`] rather than a decode error, so a
154/// database row written by a newer schema version still reads back.
155///
156/// Use [`Rollencodetyp::from_wire`] on a `String` column, or check
157/// [`Rollencodetyp::is_known`], where out-of-schema values must be rejected.
158#[cfg(feature = "sqlx")]
159impl<'r> sqlx::Decode<'r, sqlx::Postgres> for Rollencodetyp {
160    fn decode(
161        value: <sqlx::Postgres as sqlx::Database>::ValueRef<'r>,
162    ) -> Result<Self, sqlx::error::BoxDynError> {
163        let s = <&str as sqlx::Decode<sqlx::Postgres>>::decode(value)?;
164        Ok(Self::from_wire(s).unwrap_or(Self::Unknown))
165    }
166}
167/// Lets `Vec<Rollencodetyp>` bind to a `TEXT[]` column.  Only this crate can
168/// provide it: the trait and the enum are both foreign to any consumer, so the
169/// orphan rule rules out a downstream impl.
170#[cfg(feature = "sqlx")]
171impl sqlx::postgres::PgHasArrayType for Rollencodetyp {
172    fn array_type_info() -> sqlx::postgres::PgTypeInfo {
173        <String as sqlx::postgres::PgHasArrayType>::array_type_info()
174    }
175}
176#[cfg(test)]
177impl proptest::arbitrary::Arbitrary for Rollencodetyp {
178    type Parameters = ();
179    type Strategy = proptest::strategy::BoxedStrategy<Self>;
180    fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
181        use proptest::prelude::*;
182        proptest::sample::select(Self::VARIANTS.to_vec()).boxed()
183    }
184}