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

Missing magic changed to be a unique error vs a parsing error #440

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion deku-derive/src/macros/deku_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@ fn emit_magic_read(input: &DekuData) -> TokenStream {
if *__deku_byte != __deku_read_byte {
extern crate alloc;
use alloc::borrow::Cow;
return Err(::#crate_::DekuError::Parse(Cow::from(format!("Missing magic value {:?}", #magic))));
return Err(::#crate_::DekuError::Framing(::#crate_::error::NeedMagic::new(&__deku_magic[0..])));
// return Err(::#crate_::DekuError::Parse(Cow::from(format!("Missing magic value {:?}", #magic))));
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,29 @@ impl NeedSize {
}
}

/// Magic needed to frame message
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NeedMagic {
magic: Vec<u8>,
}

impl NeedMagic where {
/// Create new [NeedSize] from bits
#[inline]
pub fn new(magic: &[u8]) -> Self {
Self { magic: magic.to_vec() }
}

}

/// Deku errors
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum DekuError {
/// Parsing error when reading
Incomplete(NeedSize),
/// Failed to find magic
Framing(NeedMagic),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would prefer the name relate to magic, unless we plan on expanding this to be an enum with different variants

/// Parsing error when reading
Parse(Cow<'static, str>),
/// Invalid parameter
Expand Down Expand Up @@ -80,6 +97,7 @@ impl core::fmt::Display for DekuError {
size.bit_size(),
size.byte_size()
),
DekuError::Framing(ref framing) => write!(f, "Missing magic framing bytes: {:?}", framing.magic),
DekuError::Parse(ref err) => write!(f, "Parse error: {err}"),
DekuError::InvalidParam(ref err) => write!(f, "Invalid param error: {err}"),
DekuError::Assertion(ref err) => write!(f, "Assertion error: {err}"),
Expand All @@ -103,6 +121,7 @@ impl From<DekuError> for std::io::Error {
use std::io;
match error {
DekuError::Incomplete(_) => io::Error::new(io::ErrorKind::UnexpectedEof, error),
DekuError::Framing(_) => io::Error::new(io::ErrorKind::InvalidData, error),
DekuError::Parse(_) => io::Error::new(io::ErrorKind::InvalidData, error),
DekuError::InvalidParam(_) => io::Error::new(io::ErrorKind::InvalidInput, error),
DekuError::Assertion(_) => io::Error::new(io::ErrorKind::InvalidData, error),
Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[What is a prelude?](std::prelude)
*/
pub use crate::error::{DekuError, NeedSize};
pub use crate::error::{DekuError, NeedSize, NeedMagic};
pub use crate::{
deku_derive, reader::Reader, writer::Writer, DekuContainerRead, DekuContainerWrite,
DekuEnumExt, DekuRead, DekuReader, DekuUpdate, DekuWrite, DekuWriter,
Expand Down
2 changes: 1 addition & 1 deletion src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::cmp::Ordering;
use bitvec::prelude::*;
use no_std_io::io::{ErrorKind, Read};

use crate::{prelude::NeedSize, DekuError};
use crate::{prelude::NeedSize, prelude::NeedMagic, DekuError};
use alloc::vec::Vec;

#[cfg(feature = "logging")]
Expand Down
10 changes: 5 additions & 5 deletions tests/test_compile/cases/attribute_token_stream.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ error[E0277]: can't compare `{integer}` with `bool`
|
= help: the trait `PartialEq<bool>` is not implemented for `{integer}`
= help: the following other types implement trait `PartialEq<Rhs>`:
isize
i8
f128
f16
f32
f64
i128
i16
i32
i64
i128
usize
u8
and $N others
18 changes: 9 additions & 9 deletions tests/test_magic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ use rstest::rstest;
#[rstest(input,
case(&hex!("64656b75")),

#[should_panic(expected = "Parse(\"Missing magic value [100, 101, 107, 117]\")")]
#[should_panic(expected = "Framing(NeedMagic { magic: [100, 101, 107, 117] })")]
case(&hex!("64656bde")),

#[should_panic(expected = "Parse(\"Missing magic value [100, 101, 107, 117]\")")]
#[should_panic(expected = "Framing(NeedMagic { magic: [100, 101, 107, 117] })")]
case(&hex!("6465ad75")),

#[should_panic(expected = "Parse(\"Missing magic value [100, 101, 107, 117]\")")]
#[should_panic(expected = "Framing(NeedMagic { magic: [100, 101, 107, 117] })")]
case(&hex!("64be6b75")),

#[should_panic(expected = "Parse(\"Missing magic value [100, 101, 107, 117]\")")]
#[should_panic(expected = "Framing(NeedMagic { magic: [100, 101, 107, 117] })")]
case(&hex!("ef656b75")),

#[should_panic(expected = "Incomplete(NeedSize { bits: 8 })")]
Expand All @@ -38,19 +38,19 @@ fn test_magic_struct(input: &[u8]) {
#[rstest(input,
case(&hex!("64656b7500")),

#[should_panic(expected = "Parse(\"Missing magic value [100, 101, 107, 117]\")")]
#[should_panic(expected = "Framing(NeedMagic { magic: [100, 101, 107, 117] })")]
case(&hex!("64656bde00")),

#[should_panic(expected = "Parse(\"Missing magic value [100, 101, 107, 117]\")")]
#[should_panic(expected = "Framing(NeedMagic { magic: [100, 101, 107, 117] })")]
case(&hex!("6465ad7500")),

#[should_panic(expected = "Parse(\"Missing magic value [100, 101, 107, 117]\")")]
#[should_panic(expected = "Framing(NeedMagic { magic: [100, 101, 107, 117] })")]
case(&hex!("64be6b7500")),

#[should_panic(expected = "Parse(\"Missing magic value [100, 101, 107, 117]\")")]
#[should_panic(expected = "Framing(NeedMagic { magic: [100, 101, 107, 117] })")]
case(&hex!("ef656b7500")),

#[should_panic(expected = "Parse(\"Missing magic value [100, 101, 107, 117]\")")]
#[should_panic(expected = "Framing(NeedMagic { magic: [100, 101, 107, 117] })")]
case(&hex!("64656b00")),

#[should_panic(expected = "Incomplete(NeedSize { bits: 8 })")]
Expand Down
Loading