use utf_types::utf8::Ascii;
use utf_types::utf8::FourByteSequence;
use utf_types::utf8::ThreeByteSequence;
use utf_types::utf8::TwoByteSequence;
use crate::Utf8Handler;
use core::fmt::Formatter;
#[derive(Debug, PartialEq, Clone, Copy, Eq, Hash)]
#[non_exhaustive]
pub struct Utf8CharsError;
impl core::fmt::Display for Utf8CharsError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
write!(f, "byte sequence not well-formed UTF-8")
}
}
impl core::error::Error for Utf8CharsError {}
#[derive(Debug, Clone)]
pub(crate) struct ErrorReportingHandler;
impl ErrorReportingHandler {
pub(crate) fn new() -> Self {
Self {}
}
}
impl Utf8Handler for ErrorReportingHandler {
type Output = Result<char, Utf8CharsError>;
#[inline(always)]
fn single_byte(&self, ascii: Ascii) -> Self::Output {
Ok(ascii.to_char())
}
#[inline(always)]
fn two_byte(&self, sequence: TwoByteSequence) -> Self::Output {
Ok(sequence.to_char())
}
#[inline(always)]
fn three_byte(&self, sequence: ThreeByteSequence) -> Self::Output {
Ok(sequence.to_char())
}
#[inline(always)]
fn four_byte(&self, sequence: FourByteSequence) -> Self::Output {
Ok(sequence.to_char())
}
#[inline(always)]
fn error(&self) -> Self::Output {
Err(Utf8CharsError)
}
}
crate::macros::named_iterators_from_no_argument_handler!(
ErrorReportingHandler,
Result<char, Utf8CharsError>,
,
ErrorReportingUtf8Chars,
,
ErrorReportingUtf8CharIndices,
);
#[cfg(test)]
mod tests {
use crate::ErrorReportingUtf8Chars;
#[test]
fn test_size() {
assert_eq!(
core::mem::size_of::<Option<<ErrorReportingUtf8Chars<'_> as Iterator>::Item>>(),
core::mem::size_of::<Option<char>>()
);
}
#[test]
fn test_eq() {
let a: <ErrorReportingUtf8Chars<'_> as Iterator>::Item = Ok('a');
let a_again: <ErrorReportingUtf8Chars<'_> as Iterator>::Item = Ok('a');
assert_eq!(a, a_again);
}
}