From 0547fa49464a45064ee1823f2cb561b74c7937dd Mon Sep 17 00:00:00 2001 From: Guillaume Binet Date: Sat, 1 Jun 2024 14:34:00 -0400 Subject: [PATCH] Improvements from building a sinktask better type support cause instead of context, smoother cuerror building with cause. --- Cargo.toml | 2 +- copper/src/config.rs | 23 +++++++++++++++++- copper_datalogger/src/lib.rs | 2 +- copper_derive/src/lib.rs | 2 +- .../test/copper_log_index/lock.mdb | Bin 8192 -> 8192 bytes copper_traits/src/lib.rs | 19 ++++++++++----- 6 files changed, 38 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fdc3933ec..09b691cc2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,5 +11,5 @@ members = ["copper", "copper_log_runtime", "copper_datalogger", "copper_clock", - "copper_traits", "copper_log_reader"] + "copper_traits", "copper_log_reader", "examples/rp-gpio"] resolver = "2" diff --git a/copper/src/config.rs b/copper/src/config.rs index 5abf813b6..9e5df9fb5 100644 --- a/copper/src/config.rs +++ b/copper/src/config.rs @@ -25,12 +25,33 @@ impl From for Value { Value(RonValue::Number(value.into())) } } + +impl From for Value { + fn from(value: u8) -> Self { + Value(RonValue::Number((value as u64).into())) + } +} + impl From for Value { fn from(value: f64) -> Self { Value(RonValue::Number(value.into())) } } +impl From for u8 { + fn from(value: Value) -> Self { + if let RonValue::Number(num) = value.0 { + if let Some(i) = num.as_i64() { + i as u8 + } else { + panic!("Expected an integer value") + } + } else { + panic!("Expected a Number variant") + } + } +} + impl From for i32 { fn from(value: Value) -> Self { if let RonValue::Number(num) = value.0 { @@ -329,7 +350,7 @@ pub fn read_configuration(config_filename: &str) -> CuResult { "Failed to read configuration file: {:?}", &config_filename )) - .add_context(e.to_string().as_str()) + .add_cause(e.to_string().as_str()) })?; Ok(CuConfig::deserialize_ron(&config_content)) } diff --git a/copper_datalogger/src/lib.rs b/copper_datalogger/src/lib.rs index 607f961e4..0b5b9791c 100644 --- a/copper_datalogger/src/lib.rs +++ b/copper_datalogger/src/lib.rs @@ -91,7 +91,7 @@ impl Stream for MmapStream { _ => { let err = <&str as Into>::into("Unexpected error while encoding object.") - .add_context(e.to_string().as_str()); + .add_cause(e.to_string().as_str()); Err(err) } }, diff --git a/copper_derive/src/lib.rs b/copper_derive/src/lib.rs index 3eeb442b6..04301551e 100644 --- a/copper_derive/src/lib.rs +++ b/copper_derive/src/lib.rs @@ -99,7 +99,7 @@ pub fn copper_runtime(args: TokenStream, input: TokenStream) -> TokenStream { ty_name, index ); quote! { - #ty::new(all_instances_configs[#index]).map_err(|e| e.add_context(#additional_error_info))? + #ty::new(all_instances_configs[#index]).map_err(|e| e.add_cause(#additional_error_info))? } }) .collect(); diff --git a/copper_log_reader/test/copper_log_index/lock.mdb b/copper_log_reader/test/copper_log_index/lock.mdb index b2de4835a80372d1828c7aa8a38f3a40baf4eddc..e816159c3bf1b4ba9aa0f3dc3e6f3672466f04e8 100644 GIT binary patch delta 18 ZcmZp0XmFS?iACY0YSG4p@$wTBxB)~J2J-*_ delta 18 ZcmZp0XmFS?iRD2SU){!q@$wTBxB*Bo2T1?` diff --git a/copper_traits/src/lib.rs b/copper_traits/src/lib.rs index a1980a301..ef4cc6666 100644 --- a/copper_traits/src/lib.rs +++ b/copper_traits/src/lib.rs @@ -6,12 +6,12 @@ use std::fmt::{Display, Formatter}; #[derive(Debug)] pub struct CuError { message: String, - context: Option, + cause: Option, } impl Display for CuError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - let context_str = match &self.context { + let context_str = match &self.cause { Some(c) => format!("{}", c), None => "None".to_string(), }; @@ -26,7 +26,7 @@ impl From<&str> for CuError { fn from(s: &str) -> CuError { CuError { message: s.to_string(), - context: None, + cause: None, } } } @@ -35,14 +35,21 @@ impl From for CuError { fn from(s: String) -> CuError { CuError { message: s, - context: None, + cause: None, } } } impl CuError { - pub fn add_context(mut self, context: &str) -> CuError { - self.context = Some(context.into()); + pub fn new_with_cause(message: &str, cause: impl Error) -> CuError { + CuError { + message: message.to_string(), + cause: Some(cause.to_string()), + } + } + + pub fn add_cause(mut self, context: &str) -> CuError { + self.cause = Some(context.into()); self } }