use core::fmt;
use std::error::Error;
use crate::{
Charset,
CharsetDecodeErrorKind,
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CharsetDecodeError {
charset: Charset,
kind: CharsetDecodeErrorKind,
index: usize,
}
pub type CharsetDecodeResult<T> = Result<T, CharsetDecodeError>;
impl CharsetDecodeError {
#[inline]
pub const fn new(charset: Charset, kind: CharsetDecodeErrorKind, index: usize) -> Self {
Self { charset, kind, index }
}
#[inline]
pub const fn charset(self) -> Charset {
self.charset
}
#[inline]
pub const fn kind(self) -> CharsetDecodeErrorKind {
self.kind
}
#[inline]
pub const fn index(self) -> usize {
self.index
}
#[inline]
pub const fn required(self) -> Option<usize> {
self.kind.required()
}
#[inline]
pub const fn available(self) -> Option<usize> {
self.kind.available()
}
#[inline]
pub const fn value(self) -> Option<u32> {
self.kind.value()
}
#[inline]
pub const fn offset_by(self, base: usize) -> Self {
Self {
charset: self.charset,
kind: self.kind,
index: self.index + base,
}
}
}
impl fmt::Display for CharsetDecodeError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(value) = self.kind.value() {
write!(
formatter,
"{} decoding error at index {} for value 0x{:x}: {}",
self.charset, self.index, value, self.kind,
)
} else {
write!(
formatter,
"{} decoding error at index {}: {}",
self.charset, self.index, self.kind,
)
}
}
}
impl Error for CharsetDecodeError {}