Skip to main content

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