use std::any::type_name;
use std::convert::TryFrom;
use std::fmt;
use crate::lexer::LexerError;
use crate::parser::ParserError;
#[allow(dead_code)]
#[derive(PartialEq, Eq, Clone)]
pub enum Token<'a> {
PlainText(&'a str),
OpeningBracket,
ClosingBracket,
CRLF, IgnorableDestination, ControlSymbol(ControlSymbol<'a>),
Empty, }
#[allow(dead_code)]
impl<'a> fmt::Debug for Token<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
#[rustfmt::skip]
return match self {
Token::PlainText(text) => write!(f, r"PlainText : {:?}", *text),
Token::OpeningBracket => write!(f, "OpeningBracket"),
Token::ClosingBracket => write!(f, "ClosingBracket"),
Token::CRLF => write!(f, "CRLF"),
Token::IgnorableDestination => write!(f, "IgnorableDestination"),
Token::ControlSymbol(symbol) => write!(f, "ControlSymbol : {:?}", symbol),
Token::Empty => write!(f, "Empty"),
};
}
}
pub type ControlSymbol<'a> = (ControlWord<'a>, Property);
#[allow(dead_code)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Property {
On, Off, Value(i32), None, }
impl Property {
pub fn as_bool(&self) -> bool {
match self {
Property::On => true,
Property::Off => false,
Property::None => true,
Property::Value(val) => *val == 1,
}
}
pub fn get_value_as<T: TryFrom<i32>>(&self) -> Result<T, ParserError> {
let error: Result<T, ParserError> = Err(ParserError::ValueCastError(type_name::<T>().to_string()));
if let Property::Value(value) = &self {
return T::try_from(*value).or(error);
}
return T::try_from(0).or(error);
}
pub fn get_value(&self) -> i32 {
return self.get_value_as::<i32>().expect("i32 to i32 conversion should never fail");
}
pub fn get_unicode_value(&self) -> Result<u16, ParserError> {
let mut offset = 0;
if let Property::Value(value) = &self {
if *value < 0 {
offset = 65_536;
}
return u16::try_from(value + offset).or(Err(ParserError::UnicodeParsingError(*value)));
}
return Err(ParserError::UnicodeParsingError(0));
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ControlWord<'a> {
Rtf,
Ansi,
Unicode,
UnicodeIgnoreCount,
FontTable,
FontCharset,
FontNumber,
FontSize, ColorNumber,
ColorTable,
FileTable,
StyleSheet,
Italic,
Bold,
Underline,
UnderlineNone,
Superscript, Subscript, Smallcaps,
Strikethrough,
Par, Pard, Sectd,
Plain,
ParStyle, ParDefTab, FirstLineIdent,
LeftIndent,
RightIndent,
LeftAligned,
RightAligned,
Center,
Justify,
SpaceBefore,
SpaceAfter,
SpaceBetweenLine,
SpaceLineMul, ColorRed,
ColorGreen,
ColorBlue,
Unknown(&'a str),
}
impl<'a> ControlWord<'a> {
pub fn from(input: &str) -> Result<ControlSymbol, LexerError> {
let mut it = input.chars().rev();
let mut suffix_index = 0;
while let Some(c) = it.next() {
match c {
'0'..='9' | '-' => {
suffix_index += 1;
}
_ => break,
}
}
let index = input.len() - suffix_index;
let prefix = &input[..index];
let suffix = &input[index..];
let property = if suffix == "" {
Property::None
} else {
let Ok(value) = suffix.parse::<i32>() else {
return Err(LexerError::Error(format!("[Lexer] Unable to parse {} as integer", &suffix)));
};
Property::Value(value)
};
#[rustfmt::skip]
let control_word = match prefix {
r"\rtf" => ControlWord::Rtf,
r"\ansi" => ControlWord::Ansi,
r"\u" => ControlWord::Unicode,
r"\uc" => ControlWord::UnicodeIgnoreCount,
r"\fonttbl" => ControlWord::FontTable,
r"\colortbl" => ControlWord::ColorTable,
r"\filetbl" => ControlWord::FileTable,
r"\stylesheet" => ControlWord::StyleSheet,
r"\fcharset" => ControlWord::FontCharset,
r"\f" => ControlWord::FontNumber,
r"\fs" => ControlWord::FontSize,
r"\cf" => ControlWord::ColorNumber,
r"\i" => ControlWord::Italic,
r"\b" => ControlWord::Bold,
r"\ul" => ControlWord::Underline,
r"\ulnone" => ControlWord::UnderlineNone,
r"\super" => ControlWord::Superscript,
r"\sub" => ControlWord::Subscript,
r"\scaps" => ControlWord::Smallcaps,
r"\strike" => ControlWord::Strikethrough,
r"\par" => ControlWord::Par,
r"\pard" => ControlWord::Pard,
r"\sectd" => ControlWord::Sectd,
r"\plain" => ControlWord::Plain,
r"\s" => ControlWord::ParStyle,
r"\pardeftab" => ControlWord::ParDefTab,
r"\ql" => ControlWord::LeftAligned,
r"\qr" => ControlWord::RightAligned,
r"\qj" => ControlWord::Justify,
r"\qc" => ControlWord::Center,
r"\fi" => ControlWord::FirstLineIdent,
r"\ri" => ControlWord::RightIndent,
r"\li" => ControlWord::LeftIndent,
r"\sb" => ControlWord::SpaceBefore,
r"\sa" => ControlWord::SpaceAfter,
r"\sl" => ControlWord::SpaceBetweenLine,
r"\slmul" => ControlWord::SpaceLineMul,
r"\red" => ControlWord::ColorRed,
r"\green" => ControlWord::ColorGreen,
r"\blue" => ControlWord::ColorBlue,
_ => ControlWord::Unknown(prefix),
};
return Ok((control_word, property));
}
}
#[cfg(test)]
mod tests {
use crate::tokens::{ControlWord, Property};
#[test]
fn control_word_from_input_test() {
let input = r"\rtf1";
assert_eq!(ControlWord::from(input).unwrap(), (ControlWord::Rtf, Property::Value(1)))
}
#[test]
fn control_word_with_negative_parameter() {
let input = r"\rtf-1";
assert_eq!(ControlWord::from(input).unwrap(), (ControlWord::Rtf, Property::Value(-1)))
}
}