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)]
#[non_exhaustive]
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)]
#[non_exhaustive]
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 != 0,
}
}
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);
}
T::try_from(0).or(error)
}
pub fn get_value(&self) -> i32 {
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)));
}
Err(ParserError::UnicodeParsingError(0))
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
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,
Emdash,
Endash,
Bullet,
LeftSingleQuote,
RightSingleQuote,
LeftDoubleQuote,
RightDoubleQuote,
Tab,
Line,
Page,
Sect,
Cell,
Row,
NestCell,
NestRow,
Hidden,
HexChar,
NonBreakingSpace,
OptionalHyphen,
NonBreakingHyphen,
FormulaCharacter,
IndexSubentry,
AnsiCodePage,
Mac,
Pc,
Pca,
DefaultFont,
Bin,
Info,
Upr,
Ud,
Unknown(&'a str),
}
const SKIPPED_DESTINATIONS: &[&str] = &[
"bkmkend", "bkmkstart", "colorschememapping", "datafield", "datastore", "do", "docvar", "fldinst", "generator", "latentstyles",
"listoverridetable", "listtable", "mmathPr", "nonshppict", "objclass", "objdata", "pgdsctbl", "pict", "pntxta", "pntxtb", "revtbl",
"rsidtbl", "sp", "tc", "template", "themedata", "userprops", "xe", "xmlnstbl",
];
impl<'a> ControlWord<'a> {
pub fn from(input: &str) -> Result<ControlSymbol<'_>, LexerError> {
let digits = input.bytes().rev().take_while(|b| b.is_ascii_digit() || *b == b'-').count();
let (prefix, suffix) = input.split_at(input.len() - digits);
let property = if suffix.is_empty() {
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)
};
let control_word = ControlWord::lookup(prefix);
Ok((control_word, property))
}
pub(crate) fn lookup<'w>(name: &'w str) -> ControlWord<'w> {
#[rustfmt::skip]
let word = match name.as_bytes().get(1) {
Some(b'a') => match name {
r"\ansi" => ControlWord::Ansi,
r"\ansicpg" => ControlWord::AnsiCodePage,
_ => ControlWord::Unknown(name),
},
Some(b'b') => match name {
r"\b" => ControlWord::Bold,
r"\bin" => ControlWord::Bin,
r"\blue" => ControlWord::ColorBlue,
r"\bullet" => ControlWord::Bullet,
_ => ControlWord::Unknown(name),
},
Some(b'c') => match name {
r"\cf" => ControlWord::ColorNumber,
r"\cell" => ControlWord::Cell,
r"\colortbl" => ControlWord::ColorTable,
_ => ControlWord::Unknown(name),
},
Some(b'd') => match name {
r"\deff" => ControlWord::DefaultFont,
_ => ControlWord::Unknown(name),
},
Some(b'e') => match name {
r"\emdash" => ControlWord::Emdash,
r"\endash" => ControlWord::Endash,
_ => ControlWord::Unknown(name),
},
Some(b'f') => match name {
r"\f" => ControlWord::FontNumber,
r"\fi" => ControlWord::FirstLineIdent,
r"\fs" => ControlWord::FontSize,
r"\filetbl" => ControlWord::FileTable,
r"\fonttbl" => ControlWord::FontTable,
r"\fcharset" => ControlWord::FontCharset,
_ => ControlWord::Unknown(name),
},
Some(b'g') => match name {
r"\green" => ControlWord::ColorGreen,
_ => ControlWord::Unknown(name),
},
Some(b'i') => match name {
r"\i" => ControlWord::Italic,
r"\info" => ControlWord::Info,
_ => ControlWord::Unknown(name),
},
Some(b'l') => match name {
r"\li" => ControlWord::LeftIndent,
r"\line" => ControlWord::Line,
r"\lquote" => ControlWord::LeftSingleQuote,
r"\ldblquote" => ControlWord::LeftDoubleQuote,
_ => ControlWord::Unknown(name),
},
Some(b'm') => match name {
r"\mac" => ControlWord::Mac,
_ => ControlWord::Unknown(name),
},
Some(b'n') => match name {
r"\nestrow" => ControlWord::NestRow,
r"\nestcell" => ControlWord::NestCell,
_ => ControlWord::Unknown(name),
},
Some(b'p') => match name {
r"\pc" => ControlWord::Pc,
r"\par" => ControlWord::Par,
r"\pca" => ControlWord::Pca,
r"\page" => ControlWord::Page,
r"\pard" => ControlWord::Pard,
r"\plain" => ControlWord::Plain,
r"\pardeftab" => ControlWord::ParDefTab,
_ => ControlWord::Unknown(name),
},
Some(b'q') => match name {
r"\qc" => ControlWord::Center,
r"\qj" => ControlWord::Justify,
r"\ql" => ControlWord::LeftAligned,
r"\qr" => ControlWord::RightAligned,
_ => ControlWord::Unknown(name),
},
Some(b'r') => match name {
r"\ri" => ControlWord::RightIndent,
r"\red" => ControlWord::ColorRed,
r"\row" => ControlWord::Row,
r"\rtf" => ControlWord::Rtf,
r"\rquote" => ControlWord::RightSingleQuote,
r"\rdblquote" => ControlWord::RightDoubleQuote,
_ => ControlWord::Unknown(name),
},
Some(b's') => match name {
r"\s" => ControlWord::ParStyle,
r"\sa" => ControlWord::SpaceAfter,
r"\sb" => ControlWord::SpaceBefore,
r"\sl" => ControlWord::SpaceBetweenLine,
r"\sub" => ControlWord::Subscript,
r"\sect" => ControlWord::Sect,
r"\scaps" => ControlWord::Smallcaps,
r"\sectd" => ControlWord::Sectd,
r"\slmul" => ControlWord::SpaceLineMul,
r"\super" => ControlWord::Superscript,
r"\slmult" => ControlWord::SpaceLineMul,
r"\strike" => ControlWord::Strikethrough,
r"\stylesheet" => ControlWord::StyleSheet,
_ => ControlWord::Unknown(name),
},
Some(b't') => match name {
r"\tab" => ControlWord::Tab,
_ => ControlWord::Unknown(name),
},
Some(b'u') => match name {
r"\u" => ControlWord::Unicode,
r"\uc" => ControlWord::UnicodeIgnoreCount,
r"\ud" => ControlWord::Ud,
r"\ul" => ControlWord::Underline,
r"\upr" => ControlWord::Upr,
r"\ulnone" => ControlWord::UnderlineNone,
_ => ControlWord::Unknown(name),
},
Some(b'v') => match name {
r"\v" => ControlWord::Hidden,
_ => ControlWord::Unknown(name),
},
_ => ControlWord::Unknown(name),
};
word
}
pub(crate) fn skips_group(&self) -> bool {
match self {
ControlWord::FontTable | ControlWord::ColorTable | ControlWord::StyleSheet | ControlWord::FileTable => true,
ControlWord::Unknown(name) => SKIPPED_DESTINATIONS.contains(&name.trim_start_matches('\\')),
_ => false,
}
}
}
#[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 lookup_finds_known_and_unknown_words() {
assert_eq!(ControlWord::from(r"\pard").unwrap(), (ControlWord::Pard, Property::None));
assert_eq!(ControlWord::from(r"\fs24").unwrap(), (ControlWord::FontSize, Property::Value(24)));
assert_eq!(ControlWord::from(r"\af3").unwrap(), (ControlWord::Unknown(r"\af"), Property::Value(3)));
assert_eq!(ControlWord::from(r"\zzz").unwrap(), (ControlWord::Unknown(r"\zzz"), Property::None));
}
#[test]
fn control_word_with_negative_parameter() {
let input = r"\rtf-1";
assert_eq!(ControlWord::from(input).unwrap(), (ControlWord::Rtf, Property::Value(-1)))
}
}