use crate::{ParseError, ParserResult};
mod private {
pub trait Sealed {}
}
pub trait EncodingType: private::Sealed {
type CodeUnit: PartialEq + core::fmt::Debug + Clone;
#[doc(hidden)]
fn slice(source: &[Self::CodeUnit], start: usize, end: usize) -> Option<&[Self::CodeUnit]>;
#[doc(hidden)]
fn get_ascii(source: &[Self::CodeUnit], index: usize) -> ParserResult<Option<u8>>;
#[doc(hidden)]
fn check_calendar_key(key: &[Self::CodeUnit]) -> bool;
}
#[derive(Debug, PartialEq, Clone)]
#[allow(clippy::exhaustive_structs)] pub struct Utf16;
impl private::Sealed for Utf16 {}
impl EncodingType for Utf16 {
type CodeUnit = u16;
fn slice(source: &[Self::CodeUnit], start: usize, end: usize) -> Option<&[Self::CodeUnit]> {
source.get(start..end)
}
fn get_ascii(source: &[Self::CodeUnit], index: usize) -> ParserResult<Option<u8>> {
source.get(index).copied().map(to_ascii_byte).transpose()
}
fn check_calendar_key(key: &[Self::CodeUnit]) -> bool {
key == [0x75, 0x2d, 0x63, 0x61]
}
}
#[inline]
fn to_ascii_byte(b: u16) -> ParserResult<u8> {
if !(0x01..0x7F).contains(&b) {
return Err(ParseError::NonAsciiCodePoint);
}
Ok(b as u8)
}
#[derive(Debug, PartialEq, Clone)]
#[allow(clippy::exhaustive_structs)] pub struct Utf8;
impl private::Sealed for Utf8 {}
impl EncodingType for Utf8 {
type CodeUnit = u8;
fn slice(source: &[Self::CodeUnit], start: usize, end: usize) -> Option<&[Self::CodeUnit]> {
source.get(start..end)
}
fn get_ascii(source: &[Self::CodeUnit], index: usize) -> ParserResult<Option<u8>> {
Ok(source.get(index).copied())
}
fn check_calendar_key(key: &[Self::CodeUnit]) -> bool {
key == "u-ca".as_bytes()
}
}