Skip to main content

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