Skip to main content

zerodds_rtc/
return_code.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 ZeroDDS Contributors
3
4//! `ReturnCode_t` — Spec §5.2.1.
5
6use core::fmt;
7
8/// `ReturnCode_t` (Spec §5.2.1.1-§5.2.1.6, S. 9-10) — Status-Codes
9/// fuer alle RTC-Operationen, die nicht von Natur aus einen anderen
10/// Wert zurueckgeben.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub enum ReturnCode {
13    /// `OK` (§5.2.1.1) — Operation completed successfully.
14    Ok,
15    /// `ERROR` (§5.2.1.2) — Generic, unspecified failure.
16    Error,
17    /// `BAD_PARAMETER` (§5.2.1.3) — Illegal argument.
18    BadParameter,
19    /// `UNSUPPORTED` (§5.2.1.4) — Compliance-Point not implemented.
20    Unsupported,
21    /// `OUT_OF_RESOURCES` (§5.2.1.5) — Resource exhausted.
22    OutOfResources,
23    /// `PRECONDITION_NOT_MET` (§5.2.1.6) — State-Machine-Violation
24    /// (z.B. `initialize` waehrend nicht in `Created`-State).
25    PreconditionNotMet,
26}
27
28impl ReturnCode {
29    /// Spec §5.2.1.1 — `true`, wenn `Ok`.
30    #[must_use]
31    pub const fn is_ok(self) -> bool {
32        matches!(self, Self::Ok)
33    }
34
35    /// Konvertiert zu `Result<(), ReturnCode>` fuer ergonomischen
36    /// `?`-Operator-Einsatz im Caller.
37    ///
38    /// # Errors
39    /// Liefert `Err(self)` wenn nicht `Ok`.
40    pub const fn into_result(self) -> Result<(), Self> {
41        match self {
42            Self::Ok => Ok(()),
43            other => Err(other),
44        }
45    }
46}
47
48impl fmt::Display for ReturnCode {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        f.write_str(match self {
51            Self::Ok => "OK",
52            Self::Error => "ERROR",
53            Self::BadParameter => "BAD_PARAMETER",
54            Self::Unsupported => "UNSUPPORTED",
55            Self::OutOfResources => "OUT_OF_RESOURCES",
56            Self::PreconditionNotMet => "PRECONDITION_NOT_MET",
57        })
58    }
59}
60
61#[cfg(feature = "std")]
62impl std::error::Error for ReturnCode {}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn ok_is_ok_and_others_are_not() {
70        // Spec §5.2.1.1.
71        assert!(ReturnCode::Ok.is_ok());
72        for rc in [
73            ReturnCode::Error,
74            ReturnCode::BadParameter,
75            ReturnCode::Unsupported,
76            ReturnCode::OutOfResources,
77            ReturnCode::PreconditionNotMet,
78        ] {
79            assert!(!rc.is_ok());
80        }
81    }
82
83    #[test]
84    fn into_result_maps_ok_to_unit_and_others_to_err() {
85        assert_eq!(ReturnCode::Ok.into_result(), Ok(()));
86        assert_eq!(
87            ReturnCode::PreconditionNotMet.into_result(),
88            Err(ReturnCode::PreconditionNotMet)
89        );
90    }
91
92    #[test]
93    fn display_reports_spec_token_names() {
94        assert_eq!(alloc::format!("{}", ReturnCode::Ok), "OK");
95        assert_eq!(
96            alloc::format!("{}", ReturnCode::PreconditionNotMet),
97            "PRECONDITION_NOT_MET"
98        );
99        assert_eq!(
100            alloc::format!("{}", ReturnCode::BadParameter),
101            "BAD_PARAMETER"
102        );
103    }
104}