Skip to content

Commit

Permalink
Implement Error on EncodingConsumerError, DecodeError
Browse files Browse the repository at this point in the history
  • Loading branch information
sgwilym committed Jul 14, 2024
1 parent c748d90 commit 7fad5c1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
7 changes: 6 additions & 1 deletion data-model/src/encoding/compact_width.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::error::Error;

use crate::encoding::error::{DecodeError, EncodingConsumerError};
use crate::encoding::parameters::Decoder;
use crate::encoding::unsigned_int::{U16BE, U32BE, U64BE, U8BE};
Expand Down Expand Up @@ -92,7 +94,10 @@ impl CompactWidth {
pub async fn encode_compact_width_be<Consumer: BulkConsumer<Item = u8>>(
value: u64,
consumer: &mut Consumer,
) -> Result<(), EncodingConsumerError<Consumer::Error>> {
) -> Result<(), EncodingConsumerError<Consumer::Error>>
where
Consumer::Error: Error,
{
let width = CompactWidth::from_u64(value).width();

consumer
Expand Down
67 changes: 52 additions & 15 deletions data-model/src/encoding/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use core::error::Error;
use core::{fmt::Display, fmt::Formatter, num::TryFromIntError};
use either::Either;
use std::num::TryFromIntError;
use ufotofu::{
common::errors::{ConsumeFullSliceError, OverwriteFullSliceError},
local_nb::producer::PipeIntoSliceError,
};
use ufotofu::common::errors::{ConsumeFullSliceError, OverwriteFullSliceError};

/// Returned when a encoding fails to be consumed by a [`ufotofu::local_nb::Consumer`].
#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -23,6 +21,25 @@ impl<E> From<ConsumeFullSliceError<E>> for EncodingConsumerError<E> {
}
}

impl<E> Error for EncodingConsumerError<E>
where
E: 'static + Error,
{
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.reason)
}
}

impl<E> Display for EncodingConsumerError<E> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(
f,
"The consumer failed to consume after consuming {} bytes",
self.bytes_consumed
)
}
}

/// Everything that can go wrong when decoding a value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DecodeError<ProducerError> {
Expand All @@ -34,26 +51,46 @@ pub enum DecodeError<ProducerError> {
U64DoesNotFitUsize,
}

impl<'a, T, F, E> From<PipeIntoSliceError<'a, T, F, E>> for DecodeError<E> {
fn from(value: PipeIntoSliceError<'a, T, F, E>) -> Self {
impl<F, E> From<OverwriteFullSliceError<F, E>> for DecodeError<E> {
fn from(value: OverwriteFullSliceError<F, E>) -> Self {
match value.reason {
Either::Left(_) => DecodeError::InvalidInput,
Either::Right(err) => DecodeError::Producer(err),
}
}
}

impl<'a, T, F, E> From<OverwriteFullSliceError<'a, T, F, E>> for DecodeError<E> {
fn from(value: OverwriteFullSliceError<'a, T, F, E>) -> Self {
match value.reason {
Either::Left(_) => DecodeError::InvalidInput,
Either::Right(err) => DecodeError::Producer(err),
impl<ProducerError> From<TryFromIntError> for DecodeError<ProducerError> {
fn from(_: TryFromIntError) -> Self {
DecodeError::U64DoesNotFitUsize
}
}

impl<E> Error for DecodeError<E>
where
E: 'static + Error,
{
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
DecodeError::Producer(err) => Some(err),
DecodeError::InvalidInput => None,
DecodeError::U64DoesNotFitUsize => None,
}
}
}

impl<ProducerError> From<TryFromIntError> for DecodeError<ProducerError> {
fn from(_: TryFromIntError) -> Self {
DecodeError::U64DoesNotFitUsize
impl<E> Display for DecodeError<E> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
DecodeError::Producer(_) => {
write!(f, "The underlying producer encountered an error",)
}
DecodeError::InvalidInput => {
write!(f, "Decoding failed due to receiving invalid input",)
}
DecodeError::U64DoesNotFitUsize => {
write!(f, "Tried (and failed) to decode a u64 to a 32-bit usize",)
}
}
}
}
2 changes: 1 addition & 1 deletion data-model/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(new_uninit, async_fn_traits)]
#![feature(new_uninit, async_fn_traits, error_in_core)]

pub mod encoding;
pub mod entry;
Expand Down

0 comments on commit 7fad5c1

Please sign in to comment.