From 3e6b2713b211472c9ea66f019591cdb255d07956 Mon Sep 17 00:00:00 2001 From: Alexandru Macovei Date: Sun, 17 Jan 2021 23:35:33 +0200 Subject: [PATCH] refactor: simplify StringFormatter::new --- src/formatter/string_formatter.rs | 46 ++++++++++++++----------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/src/formatter/string_formatter.rs b/src/formatter/string_formatter.rs index c3583fe3..ee063ef1 100644 --- a/src/formatter/string_formatter.rs +++ b/src/formatter/string_formatter.rs @@ -5,7 +5,6 @@ use std::borrow::Cow; use std::collections::{BTreeMap, BTreeSet}; use std::error::Error; use std::fmt; -use std::iter::FromIterator; use crate::config::parse_style_string; 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. pub fn new(format: &'a str) -> Result { - parse(format) - .map(|format| { - // Cache all variables - let variables = VariableMapType::from_iter( - format - .get_variables() - .into_iter() - .map(|key| (key.to_string(), None)) - .collect::)>>(), - ); - let style_variables = StyleVariableMapType::from_iter( - format - .get_style_variables() - .into_iter() - .map(|key| (key.to_string(), None)) - .collect::)>>(), - ); - (format, variables, style_variables) - }) - .map(|(format, variables, style_variables)| Self { - format, - variables, - style_variables, - }) - .map_err(StringFormatterError::Parse) + let format = parse(format).map_err(StringFormatterError::Parse)?; + + // Cache all variables + let variables = format + .get_variables() + .into_iter() + .map(|key| (key.to_string(), None)) + .collect::(); + + let style_variables = format + .get_style_variables() + .into_iter() + .map(|key| (key.to_string(), None)) + .collect::(); + + Ok(Self { + format, + variables, + style_variables, + }) } /// Maps variable name to its value