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 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]
pub const fn value(self) -> Option<u32> {
match self {
Self::InvalidCodePoint { value, .. } => Some(value),
Self::UnmappableCharacter { value, .. } => Some(value),
Self::BufferTooSmall { .. } | Self::InvalidInputIndex { .. } => None,
}
}
#[must_use]
#[inline]
pub const fn required(self) -> Option<usize> {
match self {
Self::BufferTooSmall { required, .. } => Some(required),
Self::InvalidInputIndex { .. } | Self::InvalidCodePoint { .. } | Self::UnmappableCharacter { .. } => None,
}
}
#[must_use]
#[inline]
pub const fn available(self) -> Option<usize> {
match self {
Self::BufferTooSmall { available, .. } => Some(available),
Self::InvalidInputIndex { .. } | Self::InvalidCodePoint { .. } | Self::UnmappableCharacter { .. } => None,
}
}
#[must_use]
#[inline]
pub const fn input_len(self) -> Option<usize> {
match self {
Self::InvalidInputIndex { input_len } => Some(input_len),
Self::InvalidCodePoint { .. } | Self::UnmappableCharacter { .. } | Self::BufferTooSmall { .. } => None,
}
}
}