Skip to main content

leodos_libcfs/
status.rs

1//! CFE informational status code handling.
2//!
3//! While error conditions are represented by the `Error` enum, cFE APIs can also
4//! return a variety of non-error "informational" status codes. This module
5//! provides the `Status` enum to represent these successful-but-noteworthy
6//! outcomes, and a `check` function to triage a raw `CFE_Status_t` into either
7//! a `Result<Status, Error>`.
8
9use crate::error::CfsError;
10use crate::ffi;
11
12/// Represents non-error, informational status codes from cFE APIs.
13pub enum Status {
14    /// Command was processed successfully.
15    Success,
16
17    // --- Informational Status Codes ---
18    /// Command was processed successfully, but command counter should not be incremented.
19    StatusNoCounterIncrement,
20    /// The application is receiving a pointer to a CDS that was already present.
21    EsCdsAlreadyExists,
22    /// CFE_ES_LoadLibrary detected that the requested library name is already loaded.
23    EsLibAlreadyLoaded,
24    /// The last syslog message was truncated.
25    EsErrSysLogTruncated,
26    /// The table has a load pending.
27    TblInfoUpdatePending,
28    /// A registration is trying to replace an existing table with the same name.
29    TblWarnDuplicate,
30    /// The table has been updated since the last time the address was obtained.
31    TblInfoUpdated,
32    /// A table file contained less data than the size of the table.
33    TblWarnShortFile,
34    /// An attempt was made to update a table without a pending load.
35    TblInfoNoUpdatePending,
36    /// An attempt was made to update a table locked by another user.
37    TblInfoTableLocked,
38    /// The application should call CFE_TBL_Validate for the specified table.
39    TblInfoValidationPending,
40    /// An attempt was made to validate a table that did not have a validation request pending.
41    TblInfoNoValidationPending,
42    /// A table file load did not start with the first byte.
43    TblWarnPartialLoad,
44    /// A dump of the Dump-Only table has been requested.
45    TblInfoDumpPending,
46    /// A table registered as "Critical" failed to create a CDS.
47    TblWarnNotCritical,
48    /// A critical table's contents were recovered from the CDS.
49    TblInfoRecoveredTbl,
50}
51
52impl core::fmt::Display for Status {
53    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
54        let description = match self {
55            Status::Success => "Success",
56            // --- Informational Status Codes ---
57            Status::StatusNoCounterIncrement => "Info: No Counter Increment",
58            Status::EsCdsAlreadyExists => "Info ES: CDS Already Exists",
59            Status::EsLibAlreadyLoaded => "Info ES: Library Already Loaded",
60            Status::EsErrSysLogTruncated => "Info ES: System Log Message Truncated",
61            Status::TblInfoUpdatePending => "Info TBL: Update Pending",
62            Status::TblWarnDuplicate => "Warning TBL: Duplicate Table",
63            Status::TblInfoUpdated => "Info TBL: Table Updated",
64            Status::TblWarnShortFile => "Warning TBL: Short File",
65            Status::TblInfoNoUpdatePending => "Info TBL: No Update Pending",
66            Status::TblInfoTableLocked => "Info TBL: Table Locked",
67            Status::TblInfoValidationPending => "Info TBL: Validation Pending",
68            Status::TblInfoNoValidationPending => "Info TBL: No Validation Pending",
69            Status::TblWarnPartialLoad => "Warning TBL: Partial Load",
70            Status::TblInfoDumpPending => "Info TBL: Dump Pending",
71            Status::TblWarnNotCritical => "Warning TBL: Table Not Critical",
72            Status::TblInfoRecoveredTbl => "Info TBL: Recovered Table",
73        };
74        write!(f, "{}", description)
75    }
76}
77/// Converts a raw CFE status code into a `Result<Status, Error>` for idiomatic error handling.
78pub fn check(code: ffi::CFE_Status_t) -> Result<Status, CfsError> {
79    Status::try_from(code)
80}
81
82impl TryFrom<ffi::CFE_Status_t> for Status {
83    type Error = CfsError;
84    fn try_from(status: ffi::CFE_Status_t) -> Result<Self, Self::Error> {
85        let ok = match status {
86            ffi::CFE_SUCCESS => Status::Success,
87            ffi::CFE_STATUS_NO_COUNTER_INCREMENT => Status::StatusNoCounterIncrement,
88            ffi::CFE_ES_CDS_ALREADY_EXISTS => Status::EsCdsAlreadyExists,
89            ffi::CFE_ES_LIB_ALREADY_LOADED => Status::EsLibAlreadyLoaded,
90            ffi::CFE_ES_ERR_SYS_LOG_TRUNCATED => Status::EsErrSysLogTruncated,
91            ffi::CFE_TBL_INFO_UPDATE_PENDING => Status::TblInfoUpdatePending,
92            ffi::CFE_TBL_WARN_DUPLICATE => Status::TblWarnDuplicate,
93            ffi::CFE_TBL_INFO_UPDATED => Status::TblInfoUpdated,
94            ffi::CFE_TBL_WARN_SHORT_FILE => Status::TblWarnShortFile,
95            ffi::CFE_TBL_INFO_NO_UPDATE_PENDING => Status::TblInfoNoUpdatePending,
96            ffi::CFE_TBL_INFO_TABLE_LOCKED => Status::TblInfoTableLocked,
97            ffi::CFE_TBL_INFO_VALIDATION_PENDING => Status::TblInfoValidationPending,
98            ffi::CFE_TBL_INFO_NO_VALIDATION_PENDING => Status::TblInfoNoValidationPending,
99            ffi::CFE_TBL_WARN_PARTIAL_LOAD => Status::TblWarnPartialLoad,
100            ffi::CFE_TBL_INFO_DUMP_PENDING => Status::TblInfoDumpPending,
101            ffi::CFE_TBL_WARN_NOT_CRITICAL => Status::TblWarnNotCritical,
102            ffi::CFE_TBL_INFO_RECOVERED_TBL => Status::TblInfoRecoveredTbl,
103
104            other => return Err(CfsError::from(other)),
105        };
106        Ok(ok)
107    }
108}