use super::{
buf::CharacterBuf,
character::{Character, Secondary},
flags::IntoScriptFlags,
};
pub trait IntoSecondary {
fn into_secondary(self) -> Secondary;
}
pub trait IntoCharacter {
fn into_character(self) -> Character;
}
pub trait IntoScript {
fn append_script_to(&self, list: &mut CharacterBuf, flags: IntoScriptFlags);
fn into_script(&self, flags: IntoScriptFlags) -> CharacterBuf {
let mut list = CharacterBuf::new();
self.append_script_to(&mut list, flags);
list
}
}
pub trait ScriptRepr: Sized {
fn new() -> Self;
fn push(&mut self, char: Character);
fn from_char(char: Character) -> Self {
let mut this = Self::new();
this.push(char);
this
}
fn from_chars(chars: &[Character]) -> Self {
let mut this = Self::new();
for char in chars {
this.push(*char);
}
this
}
fn append<T: IntoScript>(&mut self, item: &T, flags: IntoScriptFlags) {
for char in item.into_script(flags).vec {
self.push(char);
}
}
fn encode<T: IntoScript>(item: &T, flags: IntoScriptFlags) -> Self {
let buf = item.into_script(flags);
Self::from_chars(&buf.vec[..])
}
}
impl<T: IntoSecondary> IntoCharacter for T {
fn into_character(self) -> Character {
Character::Secondary(self.into_secondary())
}
}
impl<T: Clone + IntoCharacter> IntoScript for T {
fn append_script_to(&self, list: &mut CharacterBuf, _flags: IntoScriptFlags) {
list.push(self.clone());
}
}