Skip to content

Commit

Permalink
address custom error feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Nov 5, 2023
1 parent 3346ab3 commit 631a360
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
5 changes: 1 addition & 4 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand All @@ -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",
}
}
Expand All @@ -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(),
}
}
Expand Down Expand Up @@ -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")]
Expand Down
15 changes: 5 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 631a360

Please sign in to comment.