use thiserror::Error;
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
pub enum CharsetEncodeErrorKind {
#[error("The code point is not a valid Unicode scalar value.")]
InvalidCodePoint {
value: u32,
},
#[error("The input character index is outside the input buffer.")]
InvalidInputIndex {
input_len: usize,
},
#[error("The output unit index is outside the output buffer.")]
InvalidOutputIndex {
output_len: usize,
},
#[error("The character cannot be represented by the target encoding.")]
UnmappableCharacter {
value: u32,
},
#[error(
"The output buffer is too small (required {required} units, available {available} units)."
)]
BufferTooSmall {
required: usize,
available: usize,
},
}
impl CharsetEncodeErrorKind {
#[must_use]
#[inline(always)]
pub const fn value(self) -> Option<u32> {
match self {
Self::InvalidCodePoint { value, .. } => Some(value),
Self::UnmappableCharacter { value, .. } => Some(value),
Self::BufferTooSmall { .. }
| Self::InvalidInputIndex { .. }
| Self::InvalidOutputIndex { .. } => None,
}
}
#[must_use]
#[inline(always)]
pub const fn required(self) -> Option<usize> {
match self {
Self::BufferTooSmall { required, .. } => Some(required),
Self::InvalidInputIndex { .. }
| Self::InvalidOutputIndex { .. }
| Self::InvalidCodePoint { .. }
| Self::UnmappableCharacter { .. } => None,
}
}
#[must_use]
#[inline(always)]
pub const fn available(self) -> Option<usize> {
match self {
Self::BufferTooSmall { available, .. } => Some(available),
Self::InvalidInputIndex { .. }
| Self::InvalidOutputIndex { .. }
| Self::InvalidCodePoint { .. }
| Self::UnmappableCharacter { .. } => None,
}
}
#[must_use]
#[inline(always)]
pub const fn input_len(self) -> Option<usize> {
match self {
Self::InvalidInputIndex { input_len } => Some(input_len),
Self::InvalidCodePoint { .. }
| Self::UnmappableCharacter { .. }
| Self::BufferTooSmall { .. }
| Self::InvalidOutputIndex { .. } => None,
}
}
#[must_use]
#[inline(always)]
pub const fn output_len(self) -> Option<usize> {
match self {
Self::InvalidOutputIndex { output_len } => Some(output_len),
Self::InvalidCodePoint { .. }
| Self::InvalidInputIndex { .. }
| Self::UnmappableCharacter { .. }
| Self::BufferTooSmall { .. } => None,
}
}
}