Skip to content

Commit

Permalink
Improvements from building a sinktask
Browse files Browse the repository at this point in the history
better type support
cause instead of context,
smoother cuerror building with cause.
  • Loading branch information
gbin committed Jun 1, 2024
1 parent a0a353b commit 0547fa4
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
23 changes: 22 additions & 1 deletion copper/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,33 @@ impl From<i32> for Value {
Value(RonValue::Number(value.into()))
}
}

impl From<u8> for Value {
fn from(value: u8) -> Self {
Value(RonValue::Number((value as u64).into()))
}
}

impl From<f64> for Value {
fn from(value: f64) -> Self {
Value(RonValue::Number(value.into()))
}
}

impl From<Value> 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<Value> for i32 {
fn from(value: Value) -> Self {
if let RonValue::Number(num) = value.0 {
Expand Down Expand Up @@ -329,7 +350,7 @@ pub fn read_configuration(config_filename: &str) -> CuResult<CuConfig> {
"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))
}
Expand Down
2 changes: 1 addition & 1 deletion copper_datalogger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl Stream for MmapStream {
_ => {
let err =
<&str as Into<CuError>>::into("Unexpected error while encoding object.")
.add_context(e.to_string().as_str());
.add_cause(e.to_string().as_str());
Err(err)
}
},
Expand Down
2 changes: 1 addition & 1 deletion copper_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Binary file modified copper_log_reader/test/copper_log_index/lock.mdb
Binary file not shown.
19 changes: 13 additions & 6 deletions copper_traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use std::fmt::{Display, Formatter};
#[derive(Debug)]
pub struct CuError {
message: String,
context: Option<String>,
cause: Option<String>,
}

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(),
};
Expand All @@ -26,7 +26,7 @@ impl From<&str> for CuError {
fn from(s: &str) -> CuError {
CuError {
message: s.to_string(),
context: None,
cause: None,
}
}
}
Expand All @@ -35,14 +35,21 @@ impl From<String> 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
}
}
Expand Down

0 comments on commit 0547fa4

Please sign in to comment.