refactor: simplify StringFormatter::new

This commit is contained in:
Alexandru Macovei 2021-01-17 23:35:33 +02:00 committed by Matan Kushner
parent 7cb1b7e373
commit 3e6b2713b2
1 changed files with 20 additions and 26 deletions

View File

@ -5,7 +5,6 @@ use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet}; use std::collections::{BTreeMap, BTreeSet};
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use std::iter::FromIterator;
use crate::config::parse_style_string; use crate::config::parse_style_string;
use crate::segment::Segment; use crate::segment::Segment;
@ -65,31 +64,26 @@ impl<'a> StringFormatter<'a> {
/// ///
/// This method will throw an Error when the given format string fails to parse. /// This method will throw an Error when the given format string fails to parse.
pub fn new(format: &'a str) -> Result<Self, StringFormatterError> { pub fn new(format: &'a str) -> Result<Self, StringFormatterError> {
parse(format) let format = parse(format).map_err(StringFormatterError::Parse)?;
.map(|format| {
// Cache all variables // Cache all variables
let variables = VariableMapType::from_iter( let variables = format
format
.get_variables() .get_variables()
.into_iter() .into_iter()
.map(|key| (key.to_string(), None)) .map(|key| (key.to_string(), None))
.collect::<Vec<(String, Option<_>)>>(), .collect::<VariableMapType>();
);
let style_variables = StyleVariableMapType::from_iter( let style_variables = format
format
.get_style_variables() .get_style_variables()
.into_iter() .into_iter()
.map(|key| (key.to_string(), None)) .map(|key| (key.to_string(), None))
.collect::<Vec<(String, Option<_>)>>(), .collect::<StyleVariableMapType>();
);
(format, variables, style_variables) Ok(Self {
})
.map(|(format, variables, style_variables)| Self {
format, format,
variables, variables,
style_variables, style_variables,
}) })
.map_err(StringFormatterError::Parse)
} }
/// Maps variable name to its value /// Maps variable name to its value