use thiserror::Error;
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
pub enum CharsetDecodeErrorKind {
#[error("The encoded text sequence is malformed.")]
MalformedSequence {
value: Option<u32>,
},
#[error("The input unit index is outside the input buffer.")]
InvalidInputIndex {
input_len: usize,
},
#[error("The output character index is outside the output buffer.")]
InvalidOutputIndex {
output_len: usize,
},
#[error(
"The encoded text sequence is incomplete (required {required} units, available {available} units)."
)]
IncompleteSequence {
required: usize,
available: usize,
},
#[error(
"The decoded code point 0x{value:x} is not a valid Unicode scalar value."
)]
InvalidCodePoint {
value: u32,
},
}
impl CharsetDecodeErrorKind {
#[must_use]
#[inline(always)]
pub const fn required(self) -> Option<usize> {
match self {
Self::IncompleteSequence { required, .. } => Some(required),
Self::MalformedSequence { .. }
| Self::InvalidInputIndex { .. }
| Self::InvalidOutputIndex { .. }
| Self::InvalidCodePoint { .. } => None,
}
}
#[must_use]
#[inline(always)]
pub const fn available(self) -> Option<usize> {
match self {
Self::IncompleteSequence { available, .. } => Some(available),
Self::MalformedSequence { .. }
| Self::InvalidInputIndex { .. }
| Self::InvalidOutputIndex { .. }
| Self::InvalidCodePoint { .. } => None,
}
}
#[must_use]
#[inline(always)]
pub const fn value(self) -> Option<u32> {
match self {
Self::MalformedSequence { value } => value,
Self::InvalidCodePoint { value } => Some(value),
Self::IncompleteSequence { .. }
| Self::InvalidInputIndex { .. }
| Self::InvalidOutputIndex { .. } => None,
}
}
#[must_use]
#[inline(always)]
pub const fn input_len(self) -> Option<usize> {
match self {
Self::InvalidInputIndex { input_len } => Some(input_len),
Self::MalformedSequence { .. }
| Self::IncompleteSequence { .. }
| Self::InvalidOutputIndex { .. }
| Self::InvalidCodePoint { .. } => None,
}
}
#[must_use]
#[inline(always)]
pub const fn output_len(self) -> Option<usize> {
match self {
Self::InvalidOutputIndex { output_len } => Some(output_len),
Self::MalformedSequence { .. }
| Self::InvalidInputIndex { .. }
| Self::IncompleteSequence { .. }
| Self::InvalidCodePoint { .. } => None,
}
}
#[must_use]
#[inline(always)]
pub const fn is_incomplete(self) -> bool {
matches!(self, Self::IncompleteSequence { .. })
}
#[must_use]
#[inline(always)]
pub const fn incomplete(self) -> Option<(usize, usize)> {
match self {
Self::IncompleteSequence {
required,
available,
} => Some((required, available)),
Self::MalformedSequence { .. }
| Self::InvalidInputIndex { .. }
| Self::InvalidOutputIndex { .. }
| Self::InvalidCodePoint { .. } => None,
}
}
#[must_use]
#[inline(always)]
pub const fn is_malformed_input(self) -> bool {
matches!(
self,
Self::MalformedSequence { .. } | Self::InvalidCodePoint { .. }
)
}
}