1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-26 11:37:32 +00:00

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

View File

@ -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<Self, StringFormatterError> {
parse(format)
.map(|format| {
let format = parse(format).map_err(StringFormatterError::Parse)?;
// Cache all variables
let variables = VariableMapType::from_iter(
format
let variables = format
.get_variables()
.into_iter()
.map(|key| (key.to_string(), None))
.collect::<Vec<(String, Option<_>)>>(),
);
let style_variables = StyleVariableMapType::from_iter(
format
.collect::<VariableMapType>();
let style_variables = format
.get_style_variables()
.into_iter()
.map(|key| (key.to_string(), None))
.collect::<Vec<(String, Option<_>)>>(),
);
(format, variables, style_variables)
})
.map(|(format, variables, style_variables)| Self {
.collect::<StyleVariableMapType>();
Ok(Self {
format,
variables,
style_variables,
})
.map_err(StringFormatterError::Parse)
}
/// Maps variable name to its value