From 631a36035cf426bf83a3dd40575eeedfaa56c3aa Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Sun, 5 Nov 2023 18:23:44 -0500 Subject: [PATCH] address custom error feedback --- src/ctx.rs | 5 +---- src/error.rs | 12 ++++++------ src/lib.rs | 15 +++++---------- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/ctx.rs b/src/ctx.rs index 555385a..b1ab78d 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -866,13 +866,11 @@ impl TryIntoCtx for CString { // } // } -#[cfg(test)] +#[cfg(all(test, feature = "std"))] mod tests { - #[cfg(feature = "std")] use super::*; #[test] - #[cfg(feature = "std")] fn parse_a_cstr() { let src = CString::new("Hello World").unwrap(); let as_bytes = src.as_bytes_with_nul(); @@ -884,7 +882,6 @@ mod tests { } #[test] - #[cfg(feature = "std")] fn round_trip_a_c_str() { let src = CString::new("Hello World").unwrap(); let src = src.as_c_str(); diff --git a/src/error.rs b/src/error.rs index 8f92b85..8e7811a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -20,14 +20,13 @@ pub enum Error { size: usize, msg: &'static str, }, - #[cfg(feature = "std")] /// A custom Scroll error for reporting messages to clients + #[cfg(feature = "std")] Custom(String), - #[cfg(not(feature = "std"))] /// A custom static Scroll error for reporting messages to clients - Custom(&'static str), - #[cfg(feature = "std")] + CustomStatic(&'static str), /// Returned when IO based errors are encountered + #[cfg(feature = "std")] IO(io::Error), } @@ -39,6 +38,7 @@ impl error::Error for Error { Error::BadOffset(_) => "BadOffset", Error::BadInput { .. } => "BadInput", Error::Custom(_) => "Custom", + Error::CustomStatic(_) => "Custom", // Both Custom and CustomStatic are custom errors Error::IO(_) => "IO", } } @@ -48,6 +48,7 @@ impl error::Error for Error { Error::BadOffset(_) => None, Error::BadInput { .. } => None, Error::Custom(_) => None, + Error::CustomStatic(_) => None, Error::IO(ref io) => io.source(), } } @@ -76,8 +77,7 @@ impl Display for Error { Error::Custom(ref msg) => { write!(fmt, "{}", msg) } - #[cfg(not(feature = "std"))] - Error::Custom(msg) => { + Error::CustomStatic(msg) => { write!(fmt, "{msg}") } #[cfg(feature = "std")] diff --git a/src/lib.rs b/src/lib.rs index 399bd4f..2b29d23 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -623,22 +623,17 @@ mod tests { let res = b.gread_with::<&str>(offset, StrCtx::Length(3)); assert!(res.is_err()); *offset = 0; - // FIXME: 0x2A used to be 042 -- a weird way to declare a number, probably a bug - let astring: [u8; 3] = [0x45, 0x2A, 0x44]; + let astring: [u8; 3] = [0x45, 0x42, 0x44]; let string = astring.gread_with::<&str>(offset, StrCtx::Length(2)); match &string { Ok(_) => {} - #[cfg(feature = "std")] - Err(err) => { - println!("{err}"); - panic!(); - } - #[cfg(not(feature = "std"))] - Err(_) => { + Err(_err) => { + #[cfg(feature = "std")] + println!("{_err}"); panic!(); } } - assert_eq!(string.unwrap(), "E*"); + assert_eq!(string.unwrap(), "EB"); *offset = 0; let bytes2: &[u8] = b.gread_with(offset, 2).unwrap(); assert_eq!(*offset, 2);