use core::num::NonZeroUsize;
use thiserror::Error;
#[derive(Clone, Copy, Debug, Eq, Error, Hash, PartialEq)]
#[must_use]
pub enum TranscodeDomainError<E> {
#[error("codec reset error: {source}")]
Reset {
#[source]
source: E,
},
#[error("codec main error at input index {input_index}: {source}")]
Main {
#[source]
source: E,
input_index: usize,
input_consumed: Option<NonZeroUsize>,
},
#[error("codec finish error: {source}")]
Finish {
#[source]
source: E,
},
}
impl<E> TranscodeDomainError<E> {
#[inline]
pub const fn reset(source: E) -> Self {
Self::Reset { source }
}
#[inline]
pub const fn main(source: E, input_index: usize) -> Self {
Self::Main {
source,
input_index,
input_consumed: None,
}
}
#[inline]
pub const fn main_with_consumed(source: E, input_index: usize, input_consumed: Option<NonZeroUsize>) -> Self {
Self::Main {
source,
input_index,
input_consumed,
}
}
#[inline]
pub const fn finish(source: E) -> Self {
Self::Finish { source }
}
#[inline]
#[must_use]
pub const fn source(&self) -> &E {
match self {
Self::Reset { source } | Self::Main { source, .. } | Self::Finish { source } => source,
}
}
#[inline]
#[must_use]
pub fn into_source(self) -> E {
match self {
Self::Reset { source } | Self::Main { source, .. } | Self::Finish { source } => source,
}
}
#[inline]
pub const fn input_index(&self) -> Option<usize> {
match self {
Self::Main { input_index, .. } => Some(*input_index),
Self::Reset { .. } | Self::Finish { .. } => None,
}
}
#[inline]
pub const fn input_consumed(&self) -> Option<NonZeroUsize> {
match self {
Self::Main { input_consumed, .. } => *input_consumed,
Self::Reset { .. } | Self::Finish { .. } => None,
}
}
#[inline]
pub fn map_source<T, F>(self, f: F) -> TranscodeDomainError<T>
where
F: FnOnce(E) -> T,
{
match self {
Self::Reset { source } => TranscodeDomainError::Reset { source: f(source) },
Self::Main {
source,
input_index,
input_consumed,
} => TranscodeDomainError::Main {
source: f(source),
input_index,
input_consumed,
},
Self::Finish { source } => TranscodeDomainError::Finish { source: f(source) },
}
}
}