diff options
author | Zhenhui Xie <xiezh0831@yahoo.co.jp> | 2020-07-08 06:45:32 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-07 18:45:32 -0400 |
commit | ec76fafff08933f6f31fb99ea974bdb5ae97a0af (patch) | |
tree | bb2c822cdf291635f03d27677c419488ecf77f53 /src/formatter/model.rs | |
parent | 0f52b7b12e8c1a2060aa873a68032937dfa2c044 (diff) |
feat: refactor modules to use format strings (#1374)
Diffstat (limited to 'src/formatter/model.rs')
-rw-r--r-- | src/formatter/model.rs | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/formatter/model.rs b/src/formatter/model.rs index 8e4bbea39..291bf80be 100644 --- a/src/formatter/model.rs +++ b/src/formatter/model.rs @@ -1,17 +1,103 @@ use std::borrow::Cow; +use std::collections::BTreeSet; +/// Type that holds a number of variables of type `T` +pub trait VariableHolder<T> { + fn get_variables(&self) -> BTreeSet<T>; +} + +/// Type that holds a number of style variables of type `T` +pub trait StyleVariableHolder<T> { + fn get_style_variables(&self) -> BTreeSet<T>; +} + +#[derive(Clone)] pub struct TextGroup<'a> { pub format: Vec<FormatElement<'a>>, pub style: Vec<StyleElement<'a>>, } +#[derive(Clone)] pub enum FormatElement<'a> { Text(Cow<'a, str>), Variable(Cow<'a, str>), TextGroup(TextGroup<'a>), + Conditional(Vec<FormatElement<'a>>), } +#[derive(Clone)] pub enum StyleElement<'a> { Text(Cow<'a, str>), Variable(Cow<'a, str>), } + +impl<'a> VariableHolder<Cow<'a, str>> for FormatElement<'a> { + fn get_variables(&self) -> BTreeSet<Cow<'a, str>> { + match self { + FormatElement::Variable(var) => { + let mut variables = BTreeSet::new(); + variables.insert(var.clone()); + variables + } + FormatElement::TextGroup(textgroup) => textgroup.format.get_variables(), + FormatElement::Conditional(format) => format.get_variables(), + _ => Default::default(), + } + } +} + +impl<'a> VariableHolder<Cow<'a, str>> for Vec<FormatElement<'a>> { + fn get_variables(&self) -> BTreeSet<Cow<'a, str>> { + self.iter().fold(BTreeSet::new(), |mut acc, el| { + acc.extend(el.get_variables()); + acc + }) + } +} + +impl<'a> VariableHolder<Cow<'a, str>> for &[FormatElement<'a>] { + fn get_variables(&self) -> BTreeSet<Cow<'a, str>> { + self.iter().fold(BTreeSet::new(), |mut acc, el| { + acc.extend(el.get_variables()); + acc + }) + } +} + +impl<'a> StyleVariableHolder<Cow<'a, str>> for StyleElement<'a> { + fn get_style_variables(&self) -> BTreeSet<Cow<'a, str>> { + match self { + StyleElement::Variable(var) => { + let mut variables = BTreeSet::new(); + variables.insert(var.clone()); + variables + } + _ => Default::default(), + } + } +} + +impl<'a> StyleVariableHolder<Cow<'a, str>> for Vec<StyleElement<'a>> { + fn get_style_variables(&self) -> BTreeSet<Cow<'a, str>> { + self.iter().fold(BTreeSet::new(), |mut acc, el| { + acc.extend(el.get_style_variables()); + acc + }) + } +} + +impl<'a> StyleVariableHolder<Cow<'a, str>> for Vec<FormatElement<'a>> { + fn get_style_variables(&self) -> BTreeSet<Cow<'a, str>> { + self.iter().fold(BTreeSet::new(), |mut acc, el| match el { + FormatElement::TextGroup(textgroup) => { + acc.extend(textgroup.style.get_style_variables()); + acc + } + FormatElement::Conditional(format) => { + acc.extend(format.get_style_variables()); + acc + } + _ => acc, + }) + } +} |