Skip to main content

zerodds_types/dynamic/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 ZeroDDS Contributors
3//! Fehlertypen fuer DynamicType / DynamicData (Spec §7.5.6 ReturnCode).
4
5use alloc::string::String;
6use core::fmt;
7
8/// Spec-genannter Return-Code (§7.5.6.4) — nicht alle Codes sind in
9/// Phase 4 belegt; die wichtigsten sechs sind hier abgebildet.
10#[derive(Debug, Clone, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum DynamicError {
13    /// `BadParameter` — illegales Argument (Type-Mismatch, falsche ID).
14    BadParameter(String),
15    /// `PreconditionNotMet` — Operation aktuell nicht erlaubt.
16    PreconditionNotMet(String),
17    /// `IllegalOperation` — auf diesen Kind nicht definiert.
18    IllegalOperation(String),
19    /// `Unsupported` — Spec-Konstrukt noch nicht implementiert.
20    Unsupported(String),
21    /// Type/Member-Inkonsistenz (gefunden via `is_consistent`).
22    Inconsistent(String),
23    /// Builder-Konflikt: dup name/id, fehlende Required-Felder etc.
24    BuilderConflict(String),
25    /// Loan-Lifecycle-Verletzung.
26    LoanError(String),
27}
28
29impl DynamicError {
30    /// Helper.
31    pub(crate) fn bad_parameter(msg: impl Into<String>) -> Self {
32        Self::BadParameter(msg.into())
33    }
34
35    /// Helper.
36    pub(crate) fn inconsistent(msg: impl Into<String>) -> Self {
37        Self::Inconsistent(msg.into())
38    }
39
40    /// Helper.
41    pub(crate) fn builder(msg: impl Into<String>) -> Self {
42        Self::BuilderConflict(msg.into())
43    }
44
45    /// Helper.
46    pub(crate) fn unsupported(msg: impl Into<String>) -> Self {
47        Self::Unsupported(msg.into())
48    }
49
50    /// Helper.
51    pub(crate) fn loan(msg: impl Into<String>) -> Self {
52        Self::LoanError(msg.into())
53    }
54}
55
56impl fmt::Display for DynamicError {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        match self {
59            Self::BadParameter(m) => write!(f, "bad parameter: {m}"),
60            Self::PreconditionNotMet(m) => write!(f, "precondition not met: {m}"),
61            Self::IllegalOperation(m) => write!(f, "illegal operation: {m}"),
62            Self::Unsupported(m) => write!(f, "unsupported: {m}"),
63            Self::Inconsistent(m) => write!(f, "inconsistent: {m}"),
64            Self::BuilderConflict(m) => write!(f, "builder conflict: {m}"),
65            Self::LoanError(m) => write!(f, "loan error: {m}"),
66        }
67    }
68}
69
70#[cfg(feature = "std")]
71extern crate std;
72
73#[cfg(feature = "std")]
74impl std::error::Error for DynamicError {}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79    use alloc::string::ToString;
80
81    #[test]
82    fn dynamic_error_display_emits_helpful_text() {
83        let e = DynamicError::bad_parameter("x");
84        assert!(e.to_string().contains("bad parameter"));
85        assert!(e.to_string().contains('x'));
86    }
87}