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

Add "catch all" error types. #60

Merged
merged 3 commits into from
Apr 3, 2023
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
770 changes: 770 additions & 0 deletions etherparse/src/err/from_slice_error.rs

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions etherparse/src/err/ip/header_read_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use super::HeaderError;
#[cfg(feature = "std")]
#[derive(Debug)]
pub enum HeaderReadError {
/// Error when an unexpected end of a slice is reached
/// even though more data was expected to be present.
/// IO error was encoutered while reading header.
Io(std::io::Error),

/// Error caused by the contents of the header.
Expand Down
3 changes: 1 addition & 2 deletions etherparse/src/err/ip_auth/header_read_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use super::HeaderError;
#[cfg(feature = "std")]
#[derive(Debug)]
pub enum HeaderReadError {
/// Error when an unexpected end of a slice is reached
/// even though more data was expected to be present.
/// IO error was encoutered while reading header.
Io(std::io::Error),

/// Error caused by the contents of the header.
Expand Down
7 changes: 3 additions & 4 deletions etherparse/src/err/ipv4/header_read_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use super::HeaderError;
#[cfg(feature = "std")]
#[derive(Debug)]
pub enum HeaderReadError {
/// Error when an unexpected end of a slice is reached
/// even though more data was expected to be present.
/// IO error was encoutered while reading header.
Io(std::io::Error),

/// Error caused by the contents of the header.
Expand All @@ -15,7 +14,7 @@ pub enum HeaderReadError {
#[cfg(feature = "std")]
impl HeaderReadError {
/// Returns the `std::io::Error` value if the `HeaderReadError` is `Io`.
/// Otherwise `None is returned.
/// Otherwise `None` is returned.
#[inline]
pub fn io_error(self) -> Option<std::io::Error> {
use HeaderReadError::*;
Expand All @@ -26,7 +25,7 @@ impl HeaderReadError {
}

/// Returns the `err::ipv4::HeaderError` value if the `HeaderReadError` is `Content`.
/// Otherwise `None is returned.
/// Otherwise `None` is returned.
#[inline]
pub fn content_error(self) -> Option<HeaderError> {
use HeaderReadError::*;
Expand Down
10 changes: 5 additions & 5 deletions etherparse/src/err/ipv4/slice_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub enum SliceError {
Header(ipv4::HeaderError),

/// Error while slicing an ipv4 extension header.
Extensions(ip_auth::HeaderError),
Exts(ip_auth::HeaderError),
}

impl core::fmt::Display for SliceError {
Expand All @@ -19,7 +19,7 @@ impl core::fmt::Display for SliceError {
match self {
Len(value) => value.fmt(f),
Header(err) => err.fmt(f),
Extensions(value) => value.fmt(f),
Exts(value) => value.fmt(f),
}
}
}
Expand All @@ -31,7 +31,7 @@ impl std::error::Error for SliceError {
match self {
Len(err) => Some(err),
Header(err) => Some(err),
Extensions(err) => Some(err),
Exts(err) => Some(err),
}
}
}
Expand Down Expand Up @@ -94,7 +94,7 @@ mod tests {
// extensions
{
let err = ip_auth::HeaderError::ZeroPayloadLen;
assert_eq!(format!("{}", &err), format!("{}", Extensions(err.clone())));
assert_eq!(format!("{}", &err), format!("{}", Exts(err.clone())));
}
}

Expand All @@ -113,7 +113,7 @@ mod tests {
assert!(Header(HeaderError::UnexpectedVersion { version_number: 6 })
.source()
.is_some());
assert!(Extensions(ip_auth::HeaderError::ZeroPayloadLen)
assert!(Exts(ip_auth::HeaderError::ZeroPayloadLen)
.source()
.is_some());
}
Expand Down
7 changes: 3 additions & 4 deletions etherparse/src/err/ipv6/header_read_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use super::HeaderError;
#[cfg(feature = "std")]
#[derive(Debug)]
pub enum HeaderReadError {
/// Error when an unexpected end of a slice is reached
/// even though more data was expected to be present.
/// IO error was encoutered while reading header.
Io(std::io::Error),

/// Error caused by the contents of the header.
Expand All @@ -15,7 +14,7 @@ pub enum HeaderReadError {
#[cfg(feature = "std")]
impl HeaderReadError {
/// Returns the `std::io::Error` value if the `HeaderReadError` is `Io`.
/// Otherwise `None is returned.
/// Otherwise `None` is returned.
#[inline]
pub fn io_error(self) -> Option<std::io::Error> {
use HeaderReadError::*;
Expand All @@ -26,7 +25,7 @@ impl HeaderReadError {
}

/// Returns the `err::ipv6::HeaderError` value if the `HeaderReadError` is `Content`.
/// Otherwise `None is returned.
/// Otherwise `None` is returned.
#[inline]
pub fn content_error(self) -> Option<HeaderError> {
use HeaderReadError::*;
Expand Down
10 changes: 5 additions & 5 deletions etherparse/src/err/ipv6/slice_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub enum SliceError {
Header(ipv6::HeaderError),

/// Error while slicing an ipv6 extension header.
Extensions(ipv6_exts::HeaderError),
Exts(ipv6_exts::HeaderError),
}

impl core::fmt::Display for SliceError {
Expand All @@ -19,7 +19,7 @@ impl core::fmt::Display for SliceError {
match self {
Len(value) => value.fmt(f),
Header(err) => err.fmt(f),
Extensions(value) => value.fmt(f),
Exts(value) => value.fmt(f),
}
}
}
Expand All @@ -31,7 +31,7 @@ impl std::error::Error for SliceError {
match self {
Len(err) => Some(err),
Header(err) => Some(err),
Extensions(err) => Some(err),
Exts(err) => Some(err),
}
}
}
Expand Down Expand Up @@ -94,7 +94,7 @@ mod tests {
// extensions
{
let err = ipv6_exts::HeaderError::IpAuth(ip_auth::HeaderError::ZeroPayloadLen);
assert_eq!(format!("{}", &err), format!("{}", Extensions(err.clone())));
assert_eq!(format!("{}", &err), format!("{}", Exts(err.clone())));
}
}

Expand All @@ -113,7 +113,7 @@ mod tests {
assert!(Header(HeaderError::UnexpectedVersion { version_number: 6 })
.source()
.is_some());
assert!(Extensions(ipv6_exts::HeaderError::IpAuth(
assert!(Exts(ipv6_exts::HeaderError::IpAuth(
ip_auth::HeaderError::ZeroPayloadLen
))
.source()
Expand Down
7 changes: 3 additions & 4 deletions etherparse/src/err/ipv6_exts/header_read_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use super::HeaderError;
#[cfg(feature = "std")]
#[derive(Debug)]
pub enum HeaderReadError {
/// Error when an unexpected end of a slice is reached
/// even though more data was expected to be present.
/// IO error was encoutered while reading header.
Io(std::io::Error),

/// Error caused by the contents of the header.
Expand All @@ -15,7 +14,7 @@ pub enum HeaderReadError {
#[cfg(feature = "std")]
impl HeaderReadError {
/// Returns the `std::io::Error` value if the `HeaderReadError` is `Io`.
/// Otherwise `None is returned.
/// Otherwise `None` is returned.
#[inline]
pub fn io_error(self) -> Option<std::io::Error> {
use HeaderReadError::*;
Expand All @@ -26,7 +25,7 @@ impl HeaderReadError {
}

/// Returns the `err::ipv6_exts::HeaderError` value if the `HeaderReadError` is `Content`.
/// Otherwise `None is returned.
/// Otherwise `None` is returned.
#[inline]
pub fn content_error(self) -> Option<HeaderError> {
use HeaderReadError::*;
Expand Down
8 changes: 8 additions & 0 deletions etherparse/src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub mod ipv6_exts;
pub mod packet;
pub mod tcp;

mod from_slice_error;
pub use from_slice_error::*;

mod layer;
pub use layer::*;

Expand All @@ -16,5 +19,10 @@ pub use len_error::*;
mod len_source;
pub use len_source::*;

#[cfg(feature = "std")]
mod read_error;
#[cfg(feature = "std")]
pub use read_error::*;

mod slice_write_space_error;
pub use slice_write_space_error::*;
56 changes: 28 additions & 28 deletions etherparse/src/err/packet/eth_slice_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ pub enum EthSliceError {
/// Length related errors (e.g. not enough data in slice).
Len(err::LenError),
/// Error when decoding an IPv4 header.
Ipv4Header(err::ipv4::HeaderError),
Ipv4(err::ipv4::HeaderError),
/// Error when decoding an IPv6 header.
Ipv6Header(err::ipv6::HeaderError),
Ipv6(err::ipv6::HeaderError),
/// Error when decoding an IPv4 extension header.
Ipv4ExtHeader(err::ip_auth::HeaderError),
Ipv4Exts(err::ip_auth::HeaderError),
/// Error when decoding an IPv6 extension header.
Ipv6ExtHeader(err::ipv6_exts::HeaderError),
Ipv6Exts(err::ipv6_exts::HeaderError),
/// Error when decoding a TCP header.
TcpHeader(err::tcp::HeaderError),
Tcp(err::tcp::HeaderError),
}

impl core::fmt::Display for EthSliceError {
Expand All @@ -23,11 +23,11 @@ impl core::fmt::Display for EthSliceError {

match self {
Len(err) => err.fmt(f),
Ipv4Header(err) => err.fmt(f),
Ipv6Header(err) => err.fmt(f),
Ipv4ExtHeader(err) => err.fmt(f),
Ipv6ExtHeader(err) => err.fmt(f),
TcpHeader(err) => err.fmt(f),
Ipv4(err) => err.fmt(f),
Ipv6(err) => err.fmt(f),
Ipv4Exts(err) => err.fmt(f),
Ipv6Exts(err) => err.fmt(f),
Tcp(err) => err.fmt(f),
}
}
}
Expand All @@ -38,11 +38,11 @@ impl std::error::Error for EthSliceError {
use EthSliceError::*;
match self {
Len(err) => Some(err),
Ipv4Header(err) => Some(err),
Ipv6Header(err) => Some(err),
Ipv4ExtHeader(err) => Some(err),
Ipv6ExtHeader(err) => Some(err),
TcpHeader(err) => Some(err),
Ipv4(err) => Some(err),
Ipv6(err) => Some(err),
Ipv4Exts(err) => Some(err),
Ipv6Exts(err) => Some(err),
Tcp(err) => Some(err),
}
}
}
Expand All @@ -62,14 +62,14 @@ mod tests {
fn debug() {
let err = err::ipv4::HeaderError::UnexpectedVersion { version_number: 1 };
assert_eq!(
format!("Ipv4Header({:?})", err.clone()),
format!("{:?}", Ipv4Header(err))
format!("Ipv4({:?})", err.clone()),
format!("{:?}", Ipv4(err))
);
}

#[test]
fn clone_eq_hash() {
let err = Ipv4Header(err::ipv4::HeaderError::UnexpectedVersion { version_number: 1 });
let err = Ipv4(err::ipv4::HeaderError::UnexpectedVersion { version_number: 1 });
assert_eq!(err, err.clone());
let hash_a = {
let mut hasher = DefaultHasher::new();
Expand Down Expand Up @@ -101,31 +101,31 @@ mod tests {
// Ipv4Header
{
let err = err::ipv4::HeaderError::UnexpectedVersion { version_number: 1 };
assert_eq!(format!("{}", err), format!("{}", Ipv4Header(err)));
assert_eq!(format!("{}", err), format!("{}", Ipv4(err)));
}

// Ipv6Header
{
let err = err::ipv6::HeaderError::UnexpectedVersion { version_number: 1 };
assert_eq!(format!("{}", err), format!("{}", Ipv6Header(err)));
assert_eq!(format!("{}", err), format!("{}", Ipv6(err)));
}

// Ipv4ExtHeader
{
let err = err::ip_auth::HeaderError::ZeroPayloadLen;
assert_eq!(format!("{}", err), format!("{}", Ipv4ExtHeader(err)));
assert_eq!(format!("{}", err), format!("{}", Ipv4Exts(err)));
}

// Ipv6ExtHeader
{
let err = err::ipv6_exts::HeaderError::HopByHopNotAtStart;
assert_eq!(format!("{}", err), format!("{}", Ipv6ExtHeader(err)));
assert_eq!(format!("{}", err), format!("{}", Ipv6Exts(err)));
};

// TcpHeader
{
let err = err::tcp::HeaderError::DataOffsetTooSmall { data_offset: 1 };
assert_eq!(format!("{}", err), format!("{}", TcpHeader(err)));
assert_eq!(format!("{}", err), format!("{}", Tcp(err)));
}
}

Expand All @@ -147,31 +147,31 @@ mod tests {
// Ipv4Header
{
let err = err::ipv4::HeaderError::UnexpectedVersion { version_number: 1 };
assert!(Ipv4Header(err).source().is_some());
assert!(Ipv4(err).source().is_some());
}

// Ipv6Header
{
let err = err::ipv6::HeaderError::UnexpectedVersion { version_number: 1 };
assert!(Ipv6Header(err).source().is_some());
assert!(Ipv6(err).source().is_some());
}

// Ipv4ExtHeader
{
let err = err::ip_auth::HeaderError::ZeroPayloadLen;
assert!(Ipv4ExtHeader(err).source().is_some());
assert!(Ipv4Exts(err).source().is_some());
}

// Ipv6ExtHeader
{
let err = err::ipv6_exts::HeaderError::HopByHopNotAtStart;
assert!(Ipv6ExtHeader(err).source().is_some());
assert!(Ipv6Exts(err).source().is_some());
};

// TcpHeader
{
let err = err::tcp::HeaderError::DataOffsetTooSmall { data_offset: 1 };
assert!(TcpHeader(err).source().is_some());
assert!(Tcp(err).source().is_some());
}
}
}
Loading