use crate::DerivedPropertyValue;
use std::fmt;
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
Invalid,
BadCodepoint(CodepointInfo),
Unexpected(UnexpectedError),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Invalid => write!(f, "invalid label"),
Error::BadCodepoint(info) => write!(f, "bad codepoint: {}", info),
Error::Unexpected(unexpected) => write!(f, "unexpected: {}", unexpected),
}
}
}
impl std::error::Error for Error {}
#[derive(Debug, PartialEq, Eq)]
pub struct CodepointInfo {
pub cp: u32,
pub position: usize,
pub property: DerivedPropertyValue,
}
impl CodepointInfo {
pub fn new(cp: u32, position: usize, property: DerivedPropertyValue) -> Self {
Self {
cp,
position,
property,
}
}
}
impl fmt::Display for CodepointInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"code point {:#06x}, position: {}, property: {}",
self.cp, self.position, self.property
)
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum UnexpectedError {
ContextRuleNotApplicable(CodepointInfo),
MissingContextRule(CodepointInfo),
ProfileRuleNotApplicable,
Undefined,
}
impl fmt::Display for UnexpectedError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
UnexpectedError::ContextRuleNotApplicable(info) => {
write!(f, "context rule not applicable [{}]", info)
}
UnexpectedError::MissingContextRule(info) => {
write!(f, "missing context rule [{}]", info)
}
UnexpectedError::ProfileRuleNotApplicable => write!(f, "profile rule not appplicable"),
UnexpectedError::Undefined => write!(f, "undefined"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fmt_error() {
let _val = format!("{}", Error::Invalid);
let _val = format!(
"{}",
Error::BadCodepoint(CodepointInfo {
cp: 0,
position: 0,
property: DerivedPropertyValue::PValid
})
);
let _val = format!("{}", Error::Unexpected(UnexpectedError::Undefined));
}
#[test]
fn fmt_unexpected_error() {
let _val = format!("{}", UnexpectedError::Undefined);
let _val = format!("{}", UnexpectedError::ProfileRuleNotApplicable);
let _val = format!(
"{}",
UnexpectedError::MissingContextRule(CodepointInfo {
cp: 0,
position: 0,
property: DerivedPropertyValue::PValid
})
);
let _val = format!(
"{}",
UnexpectedError::ContextRuleNotApplicable(CodepointInfo {
cp: 0,
position: 0,
property: DerivedPropertyValue::PValid
})
);
}
}