Skip to content
Open
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
2 changes: 1 addition & 1 deletion deku-derive/src/macros/deku_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ 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..])));
}
}
}
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 magic bytes
#[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),
/// 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
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