Skip to main content

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