From 1d01fc7f81032e01c08cb7ba03ac0f65245c7922 Mon Sep 17 00:00:00 2001 From: Dominik Spicher Date: Thu, 6 Feb 2025 21:56:54 +0100 Subject: [PATCH 1/2] fountain/doctest: simplify xor function --- src/fountain.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/fountain.rs b/src/fountain.rs index 85d7453..c9ddc0d 100644 --- a/src/fountain.rs +++ b/src/fountain.rs @@ -8,12 +8,8 @@ //! A seeded `Xoshiro` RNG ensures that the receiver can reconstruct which segments //! were combined into the part. //! ``` -//! let xor = |a: &[u8], b: &[u8]| { -//! a.iter() -//! .zip(b.iter()) -//! .map(|(&x1, &x2)| x1 ^ x2) -//! .collect::>() -//! }; +//! let xor = +//! |a: &[u8], b: &[u8]| -> Vec<_> { a.iter().zip(b.iter()).map(|(x1, x2)| x1 ^ x2).collect() }; //! //! let data = String::from("Ten chars!"); //! let max_length = 4; From dda3c7f20be2b316c10096bf995663d195d26bcc Mon Sep 17 00:00:00 2001 From: Dominik Spicher Date: Thu, 6 Feb 2025 22:06:15 +0100 Subject: [PATCH 2/2] fountain/ur: tweak error messages - Prepend with context if applicable. - Consistently start lower-cased. - Test more formattings. --- src/fountain.rs | 12 ++++++++++-- src/ur.rs | 32 ++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/fountain.rs b/src/fountain.rs index c9ddc0d..1506419 100644 --- a/src/fountain.rs +++ b/src/fountain.rs @@ -102,8 +102,8 @@ pub enum Error { impl core::fmt::Display for Error { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { - Self::CborDecode(e) => write!(f, "{e}"), - Self::CborEncode(e) => write!(f, "{e}"), + Self::CborDecode(e) => write!(f, "minicbor decoding error: {e}"), + Self::CborEncode(e) => write!(f, "minicbor encoding error: {e}"), Self::EmptyMessage => write!(f, "expected non-empty message"), Self::EmptyPart => write!(f, "expected non-empty part"), Self::InvalidFragmentLen => write!(f, "expected positive maximum fragment length"), @@ -1102,6 +1102,14 @@ mod tests { #[test] fn test_error_formatting() { + assert_eq!( + super::Error::from(minicbor::decode::Error::end_of_input()).to_string(), + "minicbor decoding error: end of input bytes" + ); + assert_eq!( + super::Error::from(minicbor::encode::Error::message("error")).to_string(), + "minicbor encoding error: error" + ); assert_eq!( super::Error::EmptyMessage.to_string(), "expected non-empty message" diff --git a/src/ur.rs b/src/ur.rs index 929a22a..cccd976 100644 --- a/src/ur.rs +++ b/src/ur.rs @@ -50,13 +50,13 @@ pub enum Error { impl core::fmt::Display for Error { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { - Self::Bytewords(e) => write!(f, "{e}"), - Self::Fountain(e) => write!(f, "{e}"), - Self::InvalidScheme => write!(f, "Invalid scheme"), - Self::TypeUnspecified => write!(f, "No type specified"), - Self::InvalidCharacters => write!(f, "Type contains invalid characters"), - Self::InvalidIndices => write!(f, "Invalid indices"), - Self::NotMultiPart => write!(f, "Can't decode single-part UR as multi-part"), + Self::Bytewords(e) => write!(f, "bytewords: {e}"), + Self::Fountain(e) => write!(f, "fountain: {e}"), + Self::InvalidScheme => write!(f, "invalid scheme"), + Self::TypeUnspecified => write!(f, "no type specified"), + Self::InvalidCharacters => write!(f, "type contains invalid characters"), + Self::InvalidIndices => write!(f, "invalid indices"), + Self::NotMultiPart => write!(f, "can't decode single-part UR as multi-part"), } } } @@ -468,19 +468,27 @@ mod tests { #[test] fn test_error_formatting() { - assert_eq!(super::Error::InvalidScheme.to_string(), "Invalid scheme"); + assert_eq!( + super::Error::from(crate::bytewords::Error::InvalidChecksum).to_string(), + "bytewords: invalid checksum" + ); + assert_eq!( + super::Error::from(crate::fountain::Error::EmptyPart).to_string(), + "fountain: expected non-empty part" + ); + assert_eq!(super::Error::InvalidScheme.to_string(), "invalid scheme"); assert_eq!( super::Error::TypeUnspecified.to_string(), - "No type specified" + "no type specified" ); assert_eq!( super::Error::InvalidCharacters.to_string(), - "Type contains invalid characters" + "type contains invalid characters" ); - assert_eq!(super::Error::InvalidIndices.to_string(), "Invalid indices"); + assert_eq!(super::Error::InvalidIndices.to_string(), "invalid indices"); assert_eq!( super::Error::NotMultiPart.to_string(), - "Can't decode single-part UR as multi-part" + "can't decode single-part UR as multi-part" ); } }