use crate::error::{ParseError, ParseErrorKind};
use crate::source::{SourceColumn, SourcePosition};
use super::rejection::{NonAsciiCodeByte, NonPrintableCodeByte};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ExecutableCodeByte {
byte: u8,
source_column: SourceColumn,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct CompactByte {
byte: u8,
source_column: SourceColumn,
}
impl ExecutableCodeByte {
pub(crate) fn validate(byte: u8, position: SourcePosition) -> Result<Self, ParseError> {
if let Some(rejected) = NonAsciiCodeByte::parse(byte) {
return Err(ParseError::at_position(
position,
ParseErrorKind::NonAsciiInCode { byte: rejected },
));
}
if let Some(rejected) = NonPrintableCodeByte::parse(byte) {
return Err(ParseError::at_position(
position,
ParseErrorKind::NonPrintableAsciiInCode { byte: rejected },
));
}
Ok(Self {
byte,
source_column: position.column(),
})
}
pub(crate) const fn as_u8(self) -> u8 {
self.byte
}
pub(crate) const fn source_column(self) -> SourceColumn {
self.source_column
}
}
impl CompactByte {
pub(crate) const fn from_executable(byte: ExecutableCodeByte) -> Self {
Self {
byte: byte.as_u8(),
source_column: byte.source_column(),
}
}
pub(crate) const fn as_u8(self) -> u8 {
self.byte
}
pub(crate) const fn source_column(self) -> SourceColumn {
self.source_column
}
}