use crate::{Error, UnexpectedError};
use std::borrow::Cow;
pub trait Rules {
fn width_mapping_rule<'a, T>(&self, _s: T) -> Result<Cow<'a, str>, Error>
where
T: Into<Cow<'a, str>>,
{
Err(Error::Unexpected(UnexpectedError::ProfileRuleNotApplicable))
}
fn additional_mapping_rule<'a, T>(&self, _s: T) -> Result<Cow<'a, str>, Error>
where
T: Into<Cow<'a, str>>,
{
Err(Error::Unexpected(UnexpectedError::ProfileRuleNotApplicable))
}
fn case_mapping_rule<'a, T>(&self, _s: T) -> Result<Cow<'a, str>, Error>
where
T: Into<Cow<'a, str>>,
{
Err(Error::Unexpected(UnexpectedError::ProfileRuleNotApplicable))
}
fn normalization_rule<'a, T>(&self, _s: T) -> Result<Cow<'a, str>, Error>
where
T: Into<Cow<'a, str>>,
{
Err(Error::Unexpected(UnexpectedError::ProfileRuleNotApplicable))
}
fn directionality_rule<'a, T>(&self, _s: T) -> Result<Cow<'a, str>, Error>
where
T: Into<Cow<'a, str>>,
{
Err(Error::Unexpected(UnexpectedError::ProfileRuleNotApplicable))
}
}
pub trait Profile {
fn prepare<'a, S>(&self, s: S) -> Result<Cow<'a, str>, Error>
where
S: Into<Cow<'a, str>>;
fn enforce<'a, S>(&self, s: S) -> Result<Cow<'a, str>, Error>
where
S: Into<Cow<'a, str>>;
fn compare<A, B>(&self, s1: A, s2: B) -> Result<bool, Error>
where
A: AsRef<str>,
B: AsRef<str>;
}
pub trait PrecisFastInvocation {
fn prepare<'a, S>(s: S) -> Result<Cow<'a, str>, Error>
where
S: Into<Cow<'a, str>>;
fn enforce<'a, S>(s: S) -> Result<Cow<'a, str>, Error>
where
S: Into<Cow<'a, str>>;
fn compare<A, B>(s1: A, s2: B) -> Result<bool, Error>
where
A: AsRef<str>,
B: AsRef<str>;
}
pub fn stabilize<'a, F, S>(s: S, f: F) -> Result<Cow<'a, str>, Error>
where
S: Into<Cow<'a, str>>,
F: for<'b> Fn(&'b str) -> Result<Cow<'b, str>, Error>,
{
let mut c = s.into();
for _i in 0..=2 {
let tmp = f(&c)?;
if tmp == c {
return Ok(c);
}
c = Cow::from(tmp.into_owned());
}
Err(Error::Invalid)
}
#[cfg(test)]
mod profiles {
use super::*;
#[derive(Default, Debug)]
struct TestDefaultRule {}
impl Rules for TestDefaultRule {}
#[test]
fn test_default_rule() {
let rule = TestDefaultRule::default();
assert_eq!(
rule.width_mapping_rule("Test"),
Err(Error::Unexpected(UnexpectedError::ProfileRuleNotApplicable))
);
assert_eq!(
rule.additional_mapping_rule("Test"),
Err(Error::Unexpected(UnexpectedError::ProfileRuleNotApplicable))
);
assert_eq!(
rule.case_mapping_rule("Test"),
Err(Error::Unexpected(UnexpectedError::ProfileRuleNotApplicable))
);
assert_eq!(
rule.normalization_rule("Test"),
Err(Error::Unexpected(UnexpectedError::ProfileRuleNotApplicable))
);
assert_eq!(
rule.directionality_rule("Test"),
Err(Error::Unexpected(UnexpectedError::ProfileRuleNotApplicable))
);
}
}