cu29_traits/
lib.rs

1//! Common copper traits and types for robotics systems.
2//!
3//! This crate is no_std compatible by default. Enable the "std" feature for additional
4//! functionality like implementing `std::error::Error` for `CuError` and the
5//! `new_with_cause` method that accepts types implementing `std::error::Error`.
6//!
7//! # Features
8//!
9//! - `std` (default): Enables standard library support
10//!   - Implements `std::error::Error` for `CuError`
11//!   - Adds `CuError::new_with_cause()` method for interop with std error types
12//!
13//! # no_std Usage
14//!
15//! To use without the standard library:
16//!
17//! ```toml
18//! [dependencies]
19//! cu29-traits = { version = "0.9", default-features = false }
20//! ```
21
22#![cfg_attr(not(feature = "std"), no_std)]
23extern crate alloc;
24
25use bincode::de::{BorrowDecoder, Decoder};
26use bincode::enc::Encoder;
27use bincode::error::{DecodeError, EncodeError};
28use bincode::{BorrowDecode, Decode as dDecode, Decode, Encode, Encode as dEncode};
29use compact_str::CompactString;
30use cu29_clock::{PartialCuTimeRange, Tov};
31use serde::{Deserialize, Serialize};
32
33use alloc::boxed::Box;
34use alloc::format;
35use alloc::string::{String, ToString};
36use alloc::vec::Vec;
37#[cfg(not(feature = "std"))]
38use core::error::Error as CoreError;
39use core::fmt::{Debug, Display, Formatter};
40#[cfg(feature = "std")]
41use std::error::Error;
42
43// Type alias for the boxed error type to simplify conditional compilation
44#[cfg(feature = "std")]
45type DynError = dyn std::error::Error + Send + Sync + 'static;
46#[cfg(not(feature = "std"))]
47type DynError = dyn core::error::Error + Send + Sync + 'static;
48
49/// A simple wrapper around String that implements Error trait.
50/// Used for cloning and deserializing CuError causes.
51#[derive(Debug)]
52struct StringError(String);
53
54impl Display for StringError {
55    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
56        write!(f, "{}", self.0)
57    }
58}
59
60#[cfg(feature = "std")]
61impl std::error::Error for StringError {}
62
63#[cfg(not(feature = "std"))]
64impl core::error::Error for StringError {}
65
66/// Common copper Error type.
67///
68/// This error type stores an optional cause as a boxed dynamic error,
69/// allowing for proper error chaining while maintaining Clone and
70/// Serialize/Deserialize support through custom implementations.
71pub struct CuError {
72    message: String,
73    cause: Option<Box<DynError>>,
74}
75
76// Custom Debug implementation that formats cause as string
77impl Debug for CuError {
78    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
79        f.debug_struct("CuError")
80            .field("message", &self.message)
81            .field("cause", &self.cause.as_ref().map(|e| e.to_string()))
82            .finish()
83    }
84}
85
86// Custom Clone implementation - clones cause as StringError wrapper
87impl Clone for CuError {
88    fn clone(&self) -> Self {
89        CuError {
90            message: self.message.clone(),
91            cause: self
92                .cause
93                .as_ref()
94                .map(|e| Box::new(StringError(e.to_string())) as Box<DynError>),
95        }
96    }
97}
98
99// Custom Serialize - serializes cause as Option<String>
100impl Serialize for CuError {
101    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
102    where
103        S: serde::Serializer,
104    {
105        use serde::ser::SerializeStruct;
106        let mut state = serializer.serialize_struct("CuError", 2)?;
107        state.serialize_field("message", &self.message)?;
108        state.serialize_field("cause", &self.cause.as_ref().map(|e| e.to_string()))?;
109        state.end()
110    }
111}
112
113// Custom Deserialize - deserializes cause as StringError wrapper
114impl<'de> Deserialize<'de> for CuError {
115    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
116    where
117        D: serde::Deserializer<'de>,
118    {
119        #[derive(Deserialize)]
120        struct CuErrorHelper {
121            message: String,
122            cause: Option<String>,
123        }
124
125        let helper = CuErrorHelper::deserialize(deserializer)?;
126        Ok(CuError {
127            message: helper.message,
128            cause: helper
129                .cause
130                .map(|s| Box::new(StringError(s)) as Box<DynError>),
131        })
132    }
133}
134
135impl Display for CuError {
136    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
137        let context_str = match &self.cause {
138            Some(c) => c.to_string(),
139            None => "None".to_string(),
140        };
141        write!(f, "{}\n   context:{}", self.message, context_str)?;
142        Ok(())
143    }
144}
145
146#[cfg(not(feature = "std"))]
147impl CoreError for CuError {
148    fn source(&self) -> Option<&(dyn CoreError + 'static)> {
149        self.cause
150            .as_deref()
151            .map(|e| e as &(dyn CoreError + 'static))
152    }
153}
154
155#[cfg(feature = "std")]
156impl Error for CuError {
157    fn source(&self) -> Option<&(dyn Error + 'static)> {
158        self.cause.as_deref().map(|e| e as &(dyn Error + 'static))
159    }
160}
161
162impl From<&str> for CuError {
163    fn from(s: &str) -> CuError {
164        CuError {
165            message: s.to_string(),
166            cause: None,
167        }
168    }
169}
170
171impl From<String> for CuError {
172    fn from(s: String) -> CuError {
173        CuError {
174            message: s,
175            cause: None,
176        }
177    }
178}
179
180impl CuError {
181    /// Creates a new CuError from an interned string index.
182    /// Used by the cu_error! macro.
183    ///
184    /// The index is stored as a placeholder string `[interned:{index}]`.
185    /// Actual string resolution happens at logging time via the unified logger.
186    pub fn new(message_index: usize) -> CuError {
187        CuError {
188            message: format!("[interned:{}]", message_index),
189            cause: None,
190        }
191    }
192
193    /// Creates a new CuError with a message and an underlying cause.
194    ///
195    /// # Example
196    /// ```
197    /// use cu29_traits::CuError;
198    ///
199    /// let io_err = std::io::Error::other("io error");
200    /// let err = CuError::new_with_cause("Failed to read file", io_err);
201    /// ```
202    #[cfg(feature = "std")]
203    pub fn new_with_cause<E>(message: &str, cause: E) -> CuError
204    where
205        E: std::error::Error + Send + Sync + 'static,
206    {
207        CuError {
208            message: message.to_string(),
209            cause: Some(Box::new(cause)),
210        }
211    }
212
213    /// Creates a new CuError with a message and an underlying cause.
214    #[cfg(not(feature = "std"))]
215    pub fn new_with_cause<E>(message: &str, cause: E) -> CuError
216    where
217        E: core::error::Error + Send + Sync + 'static,
218    {
219        CuError {
220            message: message.to_string(),
221            cause: Some(Box::new(cause)),
222        }
223    }
224
225    /// Adds or replaces the cause with a context string.
226    ///
227    /// This is useful for adding context to errors during propagation.
228    ///
229    /// # Example
230    /// ```
231    /// use cu29_traits::CuError;
232    ///
233    /// let err = CuError::from("base error").add_cause("additional context");
234    /// ```
235    pub fn add_cause(mut self, context: &str) -> CuError {
236        self.cause = Some(Box::new(StringError(context.to_string())));
237        self
238    }
239
240    /// Adds a cause error to this CuError (builder pattern).
241    ///
242    /// # Example
243    /// ```
244    /// use cu29_traits::CuError;
245    ///
246    /// let io_err = std::io::Error::other("io error");
247    /// let err = CuError::from("Operation failed").with_cause(io_err);
248    /// ```
249    #[cfg(feature = "std")]
250    pub fn with_cause<E>(mut self, cause: E) -> CuError
251    where
252        E: std::error::Error + Send + Sync + 'static,
253    {
254        self.cause = Some(Box::new(cause));
255        self
256    }
257
258    /// Adds a cause error to this CuError (builder pattern).
259    #[cfg(not(feature = "std"))]
260    pub fn with_cause<E>(mut self, cause: E) -> CuError
261    where
262        E: core::error::Error + Send + Sync + 'static,
263    {
264        self.cause = Some(Box::new(cause));
265        self
266    }
267
268    /// Returns a reference to the underlying cause, if any.
269    pub fn cause(&self) -> Option<&(dyn core::error::Error + Send + Sync + 'static)> {
270        self.cause.as_deref()
271    }
272
273    /// Returns the error message.
274    pub fn message(&self) -> &str {
275        &self.message
276    }
277}
278
279/// Creates a CuError with a message and cause in a single call.
280///
281/// This is a convenience function for use with `.map_err()`.
282///
283/// # Example
284/// ```
285/// use cu29_traits::with_cause;
286///
287/// let result: Result<(), std::io::Error> = Err(std::io::Error::other("io error"));
288/// let cu_result = result.map_err(|e| with_cause("Failed to read file", e));
289/// ```
290#[cfg(feature = "std")]
291pub fn with_cause<E>(message: &str, cause: E) -> CuError
292where
293    E: std::error::Error + Send + Sync + 'static,
294{
295    CuError::new_with_cause(message, cause)
296}
297
298/// Creates a CuError with a message and cause in a single call.
299#[cfg(not(feature = "std"))]
300pub fn with_cause<E>(message: &str, cause: E) -> CuError
301where
302    E: core::error::Error + Send + Sync + 'static,
303{
304    CuError::new_with_cause(message, cause)
305}
306
307// Generic Result type for copper.
308pub type CuResult<T> = Result<T, CuError>;
309
310/// Defines a basic write, append only stream trait to be able to log or send serializable objects.
311pub trait WriteStream<E: Encode>: Debug + Send + Sync {
312    fn log(&mut self, obj: &E) -> CuResult<()>;
313    fn flush(&mut self) -> CuResult<()> {
314        Ok(())
315    }
316}
317
318/// Defines the types of what can be logged in the unified logger.
319#[derive(dEncode, dDecode, Copy, Clone, Debug, PartialEq)]
320pub enum UnifiedLogType {
321    Empty,             // Dummy default used as a debug marker
322    StructuredLogLine, // This is for the structured logs (ie. debug! etc..)
323    CopperList,        // This is the actual data log storing activities between tasks.
324    FrozenTasks,       // Log of all frozen state of the tasks.
325    LastEntry,         // This is a special entry that is used to signal the end of the log.
326}
327/// Represent the minimum set of traits to be usable as Metadata in Copper.
328pub trait Metadata: Default + Debug + Clone + Encode + Decode<()> + Serialize {}
329
330impl Metadata for () {}
331
332/// Key metadata piece attached to every message in Copper.
333pub trait CuMsgMetadataTrait {
334    /// The time range used for the processing of this message
335    fn process_time(&self) -> PartialCuTimeRange;
336
337    /// Small status text for user UI to get the realtime state of task (max 24 chrs)
338    fn status_txt(&self) -> &CuCompactString;
339}
340
341/// A generic trait to expose the generated CuStampedDataSet from the task graph.
342pub trait ErasedCuStampedData {
343    fn payload(&self) -> Option<&dyn erased_serde::Serialize>;
344    fn tov(&self) -> Tov;
345    fn metadata(&self) -> &dyn CuMsgMetadataTrait;
346}
347
348/// Trait to get a vector of type-erased CuStampedDataSet
349/// This is used for generic serialization of the copperlists
350pub trait ErasedCuStampedDataSet {
351    fn cumsgs(&self) -> Vec<&dyn ErasedCuStampedData>;
352}
353
354/// Trait to trace back from the CopperList the origin of the messages
355pub trait MatchingTasks {
356    fn get_all_task_ids() -> &'static [&'static str];
357}
358
359/// A CopperListTuple needs to be encodable, decodable and fixed size in memory.
360pub trait CopperListTuple:
361    bincode::Encode
362    + bincode::Decode<()>
363    + Debug
364    + Serialize
365    + ErasedCuStampedDataSet
366    + MatchingTasks
367    + Default
368{
369} // Decode forces Sized already
370
371// Also anything that follows this contract can be a payload (blanket implementation)
372impl<T> CopperListTuple for T where
373    T: bincode::Encode
374        + bincode::Decode<()>
375        + Debug
376        + Serialize
377        + ErasedCuStampedDataSet
378        + MatchingTasks
379        + Default
380{
381}
382
383// We use this type to convey very small status messages.
384// MAX_SIZE from their repr module is not accessible so we need to copy paste their definition for 24
385// which is the maximum size for inline allocation (no heap)
386pub const COMPACT_STRING_CAPACITY: usize = size_of::<String>();
387
388#[derive(Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
389pub struct CuCompactString(pub CompactString);
390
391impl Encode for CuCompactString {
392    fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
393        let CuCompactString(compact_string) = self;
394        let bytes = &compact_string.as_bytes();
395        bytes.encode(encoder)
396    }
397}
398
399impl Debug for CuCompactString {
400    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
401        if self.0.is_empty() {
402            return write!(f, "CuCompactString(Empty)");
403        }
404        write!(f, "CuCompactString({})", self.0)
405    }
406}
407
408impl<Context> Decode<Context> for CuCompactString {
409    fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
410        let bytes = <Vec<u8> as Decode<D::Context>>::decode(decoder)?; // Decode into a byte buffer
411        let compact_string =
412            CompactString::from_utf8(bytes).map_err(|e| DecodeError::Utf8 { inner: e })?;
413        Ok(CuCompactString(compact_string))
414    }
415}
416
417impl<'de, Context> BorrowDecode<'de, Context> for CuCompactString {
418    fn borrow_decode<D: BorrowDecoder<'de>>(decoder: &mut D) -> Result<Self, DecodeError> {
419        CuCompactString::decode(decoder)
420    }
421}
422
423#[cfg(feature = "defmt")]
424impl defmt::Format for CuError {
425    fn format(&self, f: defmt::Formatter) {
426        match &self.cause {
427            Some(c) => {
428                let cause_str = c.to_string();
429                defmt::write!(
430                    f,
431                    "CuError {{ message: {}, cause: {} }}",
432                    defmt::Display2Format(&self.message),
433                    defmt::Display2Format(&cause_str),
434                )
435            }
436            None => defmt::write!(
437                f,
438                "CuError {{ message: {}, cause: None }}",
439                defmt::Display2Format(&self.message),
440            ),
441        }
442    }
443}
444
445#[cfg(feature = "defmt")]
446impl defmt::Format for CuCompactString {
447    fn format(&self, f: defmt::Formatter) {
448        if self.0.is_empty() {
449            defmt::write!(f, "CuCompactString(Empty)");
450        } else {
451            defmt::write!(f, "CuCompactString({})", defmt::Display2Format(&self.0));
452        }
453    }
454}
455
456#[cfg(test)]
457mod tests {
458    use crate::CuCompactString;
459    use bincode::{config, decode_from_slice, encode_to_vec};
460    use compact_str::CompactString;
461
462    #[test]
463    fn test_cucompactstr_encode_decode_empty() {
464        let cstr = CuCompactString(CompactString::from(""));
465        let config = config::standard();
466        let encoded = encode_to_vec(&cstr, config).expect("Encoding failed");
467        assert_eq!(encoded.len(), 1); // This encodes the usize 0 in variable encoding so 1 byte which is 0.
468        let (decoded, _): (CuCompactString, usize) =
469            decode_from_slice(&encoded, config).expect("Decoding failed");
470        assert_eq!(cstr.0, decoded.0);
471    }
472
473    #[test]
474    fn test_cucompactstr_encode_decode_small() {
475        let cstr = CuCompactString(CompactString::from("test"));
476        let config = config::standard();
477        let encoded = encode_to_vec(&cstr, config).expect("Encoding failed");
478        assert_eq!(encoded.len(), 5); // This encodes a 4-byte string "test" plus 1 byte for the length prefix.
479        let (decoded, _): (CuCompactString, usize) =
480            decode_from_slice(&encoded, config).expect("Decoding failed");
481        assert_eq!(cstr.0, decoded.0);
482    }
483}
484
485// Tests that require std feature
486#[cfg(all(test, feature = "std"))]
487mod std_tests {
488    use crate::{CuError, with_cause};
489
490    #[test]
491    fn test_cuerror_from_str() {
492        let err = CuError::from("test error");
493        assert_eq!(err.message(), "test error");
494        assert!(err.cause().is_none());
495    }
496
497    #[test]
498    fn test_cuerror_from_string() {
499        let err = CuError::from(String::from("test error"));
500        assert_eq!(err.message(), "test error");
501        assert!(err.cause().is_none());
502    }
503
504    #[test]
505    fn test_cuerror_new_index() {
506        let err = CuError::new(42);
507        assert_eq!(err.message(), "[interned:42]");
508        assert!(err.cause().is_none());
509    }
510
511    #[test]
512    fn test_cuerror_new_with_cause() {
513        let io_err = std::io::Error::other("io error");
514        let err = CuError::new_with_cause("wrapped error", io_err);
515        assert_eq!(err.message(), "wrapped error");
516        assert!(err.cause().is_some());
517        assert!(err.cause().unwrap().to_string().contains("io error"));
518    }
519
520    #[test]
521    fn test_cuerror_add_cause() {
522        let err = CuError::from("base error").add_cause("additional context");
523        assert_eq!(err.message(), "base error");
524        assert!(err.cause().is_some());
525        assert_eq!(err.cause().unwrap().to_string(), "additional context");
526    }
527
528    #[test]
529    fn test_cuerror_with_cause_method() {
530        let io_err = std::io::Error::other("io error");
531        let err = CuError::from("base error").with_cause(io_err);
532        assert_eq!(err.message(), "base error");
533        assert!(err.cause().is_some());
534    }
535
536    #[test]
537    fn test_cuerror_with_cause_free_function() {
538        let io_err = std::io::Error::other("io error");
539        let err = with_cause("wrapped", io_err);
540        assert_eq!(err.message(), "wrapped");
541        assert!(err.cause().is_some());
542    }
543
544    #[test]
545    fn test_cuerror_clone() {
546        let io_err = std::io::Error::other("io error");
547        let err = CuError::new_with_cause("test", io_err);
548        let cloned = err.clone();
549        assert_eq!(err.message(), cloned.message());
550        // Cause string representation should match
551        assert_eq!(
552            err.cause().map(|c| c.to_string()),
553            cloned.cause().map(|c| c.to_string())
554        );
555    }
556
557    #[test]
558    fn test_cuerror_serialize_deserialize_json() {
559        let io_err = std::io::Error::other("io error");
560        let err = CuError::new_with_cause("test", io_err);
561
562        let serialized = serde_json::to_string(&err).unwrap();
563        let deserialized: CuError = serde_json::from_str(&serialized).unwrap();
564
565        assert_eq!(err.message(), deserialized.message());
566        // Cause should be preserved as string
567        assert!(deserialized.cause().is_some());
568    }
569
570    #[test]
571    fn test_cuerror_serialize_deserialize_no_cause() {
572        let err = CuError::from("simple error");
573
574        let serialized = serde_json::to_string(&err).unwrap();
575        let deserialized: CuError = serde_json::from_str(&serialized).unwrap();
576
577        assert_eq!(err.message(), deserialized.message());
578        assert!(deserialized.cause().is_none());
579    }
580
581    #[test]
582    fn test_cuerror_display() {
583        let err = CuError::from("test error").add_cause("some context");
584        let display = format!("{}", err);
585        assert!(display.contains("test error"));
586        assert!(display.contains("some context"));
587    }
588
589    #[test]
590    fn test_cuerror_debug() {
591        let err = CuError::from("test error").add_cause("some context");
592        let debug = format!("{:?}", err);
593        assert!(debug.contains("test error"));
594        assert!(debug.contains("some context"));
595    }
596}