Skip to main content

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