Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fountain/doctest: simplify xor function #226

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/fountain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
//! };
//! 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;
Expand Down Expand Up @@ -106,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"),
Expand Down Expand Up @@ -1106,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"
Expand Down
32 changes: 20 additions & 12 deletions src/ur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
}
Expand Down Expand Up @@ -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"
);
}
}
Loading