pub fn parse(self, default_style: Option<Style>) -> Result<Vec<Segment>, StringFormatterError> {
        fn parse_textgroup<'a>(
            textgroup: TextGroup<'a>,
            variables: &'a VariableMapType<'a>,
            style_variables: &'a StyleVariableMapType<'a>,
        ) -> Result<Vec<Segment>, StringFormatterError> {
            let style = parse_style(textgroup.style, style_variables);
            parse_format(
                textgroup.format,
                style.transpose()?,
                &variables,
                &style_variables,
            )
        }

        fn parse_style<'a>(
            style: Vec<StyleElement>,
            variables: &'a StyleVariableMapType<'a>,
        ) -> Option<Result<Style, StringFormatterError>> {
            let style_strings = style
                .into_iter()
                .map(|style| match style {
                    StyleElement::Text(text) => Ok(text),
                    StyleElement::Variable(name) => {
                        let variable = variables.get(name.as_ref()).unwrap_or(&None);
                        match variable {
                            Some(style_string) => style_string.clone(),
                            None => Ok("".into()),
                        }
                    }
                })
                .collect::<Result<Vec<Cow<str>>, StringFormatterError>>();
            style_strings
                .map(|style_strings| {
                    let style_string: String =
                        style_strings.iter().flat_map(|s| s.chars()).collect();
                    parse_style_string(&style_string)
                })
                .transpose()
        }

        fn parse_format<'a>(
            format: Vec<FormatElement<'a>>,
            style: Option<Style>,
            variables: &'a VariableMapType<'a>,
            style_variables: &'a StyleVariableMapType<'a>,
        ) -> Result<Vec<Segment>, StringFormatterError> {
            let results: Result<Vec<Vec<Segment>>, StringFormatterError> = format
                .into_iter()
                .map(|el| {
                    match el {
                        FormatElement::Text(text) => Ok(vec![Segment::new(style, text)]),
                        FormatElement::TextGroup(textgroup) => {
                            let textgroup = TextGroup {
                                format: textgroup.format,
                                style: textgroup.style,
                            };
                            parse_textgroup(textgroup, &variables, &style_variables)
                        }
                        FormatElement::Variable(name) => variables
                            .get(name.as_ref())
                            .expect("Uncached variable found")
                            .as_ref()
                            .map(|segments| match segments.clone()? {
                                VariableValue::Styled(segments) => Ok(segments
                                    .into_iter()
                                    .map(|mut segment| {
                                        // Derive upper style if the style of segments are none.
                                        if segment.style.is_none() {
                                            segment.style = style;
                                        };
                                        segment
                                    })
                                    .collect()),
                                VariableValue::Plain(text) => Ok(vec![Segment::new(style, text)]),
                                VariableValue::Meta(format) => {
                                    let formatter = StringFormatter {
                                        format,
                                        variables: clone_without_meta(variables),
                                        style_variables: style_variables.clone(),
                                    };
                                    formatter.parse(style)
                                }
                            })
                            .unwrap_or_else(|| Ok(Vec::new())),
                        FormatElement::Conditional(format) => {
                            // Show the conditional format string if all the variables inside are not
                            // none.
                            fn should_show_elements<'a>(
                                format_elements: &[FormatElement],
                                variables: &'a VariableMapType<'a>,
                            ) -> bool {
                                format_elements.get_variables().iter().any(|var| {
                                    variables
                                        .get(var.as_ref())
                                        .map(|map_result| {
                                            let map_result = map_result.as_ref();
                                            map_result
                                                .and_then(|result| result.as_ref().ok())
                                                .map(|result| match result {
                                                    // If the variable is a meta variable, also
                                                    // check the format string inside it.
                                                    VariableValue::Meta(meta_elements) => {
                                                        let meta_variables =
                                                            clone_without_meta(variables);
                                                        should_show_elements(
                                                            &meta_elements,
                                                            &meta_variables,
                                                        )
                                                    }
                                                    _ => true,
                                                })
                                                // The variable is None or Err, or a meta variable
                                                // that shouldn't show
                                                .unwrap_or(false)
                                        })
                                        // Can't find the variable in format string
                                        .unwrap_or(false)
                                })
                            }

                            let should_show: bool = should_show_elements(&format, variables);

                            if should_show {
                                parse_format(format, style, variables, style_variables)
                            } else {
                                Ok(Vec::new())
                            }
                        }
                    }
                })
                .collect();
            Ok(results?.into_iter().flatten().collect())
        }

        parse_format(
            self.format,
            default_style,
            &self.variables,
            &self.style_variables,
        )
    }
/// Parse the format string and consume self.
///
/// This method will throw an Error in the following conditions:
///
/// - Format string in meta variables fails to parse
/// - Variable mapper returns an error.
Parse the format string and consume self.
This method will throw an Error in the following conditions.

Format string in meta variables fails to parse
Variable mapper returns an error.
Parse the format string and consume self . This method will throw an Error in the following conditions . Format string in meta variables fails to parse Variable mapper returns an error .
pub fn parse(self, default_style: Option<Style>) -> Result<Vec<Segment>, StringFormatterError> {
        fn parse_textgroup<'a>(
            textgroup: TextGroup<'a>,
            variables: &'a VariableMapType<'a>,
            style_variables: &'a StyleVariableMapType<'a>,
        ) -> Result<Vec<Segment>, StringFormatterError> {
            let style = parse_style(textgroup.style, style_variables);
            parse_format(
                textgroup.format,
                style.transpose()?,
                &variables,
                &style_variables,
            )
        }
        fn parse_style<'a>(
            style: Vec<StyleElement>,
            variables: &'a StyleVariableMapType<'a>,
        ) -> Option<Result<Style, StringFormatterError>> {
            let style_strings = style
                .into_iter()
                .map(|style| match style {
                    StyleElement::Text(text) => Ok(text),
                    StyleElement::Variable(name) => {
                        let variable = variables.get(name.as_ref()).unwrap_or(&None);
                        match variable {
                            Some(style_string) => style_string.clone(),
                            None => Ok("".into()),
                        }
                    }
                })
                .collect::<Result<Vec<Cow<str>>, StringFormatterError>>();
            style_strings
                .map(|style_strings| {
                    let style_string: String =
                        style_strings.iter().flat_map(|s| s.chars()).collect();
                    parse_style_string(&style_string)
                })
                .transpose()
        }
        fn parse_format<'a>(
            format: Vec<FormatElement<'a>>,
            style: Option<Style>,
            variables: &'a VariableMapType<'a>,
            style_variables: &'a StyleVariableMapType<'a>,
        ) -> Result<Vec<Segment>, StringFormatterError> {
            let results: Result<Vec<Vec<Segment>>, StringFormatterError> = format
                .into_iter()
                .map(|el| {
                    match el {
                        FormatElement::Text(text) => Ok(vec![Segment::new(style, text)]),
                        FormatElement::TextGroup(textgroup) => {
                            let textgroup = TextGroup {
                                format: textgroup.format,
                                style: textgroup.style,
                            };
                            parse_textgroup(textgroup, &variables, &style_variables)
                        }
                        FormatElement::Variable(name) => variables
                            .get(name.as_ref())
                            .expect("Uncached variable found")
                            .as_ref()
                            .map(|segments| match segments.clone()? {
                                VariableValue::Styled(segments) => Ok(segments
                                    .into_iter()
                                    .map(|mut segment| {
                                        // Derive upper style if the style of segments are none.
                                        if segment.style.is_none() {
                                            segment.style = style;
                                        };
                                        segment
                                    })
                                    .collect()),
                                VariableValue::Plain(text) => Ok(vec![Segment::new(style, text)]),
                                VariableValue::Meta(format) => {
                                    let formatter = StringFormatter {
                                        format,
                                        variables: clone_without_meta(variables),
                                        style_variables: style_variables.clone(),
                                    };
                                    formatter.parse(style)
                                }
                            })
                            .unwrap_or_else(|| Ok(Vec::new())),
                        FormatElement::Conditional(format) => {
                            // Show the conditional format string if all the variables inside are not
                            // none.
                            fn should_show_elements<'a>(
                                format_elements: &[FormatElement],
                                variables: &'a VariableMapType<'a>,
                            ) -> bool {
                                format_elements.get_variables().iter().any(|var| {
                                    variables
                                        .get(var.as_ref())
                                        .map(|map_result| {
                                            let map_result = map_result.as_ref();
                                            map_result
                                                .and_then(|result| result.as_ref().ok())
                                                .map(|result| match result {
                                                    // If the variable is a meta variable, also
                                                    // check the format string inside it.
                                                    VariableValue::Meta(meta_elements) => {
                                                        let meta_variables =
                                                            clone_without_meta(variables);
                                                        should_show_elements(
                                                            &meta_elements,
                                                            &meta_variables,
                                                        )
                                                    }
                                                    _ => true,
                                                })
                                                // The variable is None or Err, or a meta variable
                                                // that shouldn't show
                                                .unwrap_or(false)
                                        })
                                        // Can't find the variable in format string
                                        .unwrap_or(false)
                                })
                            }
                            let should_show: bool = should_show_elements(&format, variables);
                            if should_show {
                                parse_format(format, style, variables, style_variables)
                            } else {
                                Ok(Vec::new())
                            }
                        }
                    }
                })
                .collect();
            Ok(results?.into_iter().flatten().collect())
        }
        parse_format(
            self.format,
            default_style,
            &self.variables,
            &self.style_variables,
        )
    }
pub fn parse ( self , default_style : Option < Style > ) -> Result < Vec < Segment > , StringFormatterError > { fn parse_textgroup < ' a > ( textgroup : TextGroup < ' a > , variables : & ' a VariableMapType < ' a > , style_variables : & ' a StyleVariableMapType < ' a > , ) -> Result < Vec < Segment > , StringFormatterError > { let style = parse_style ( textgroup . style , style_variables ) ; parse_format ( textgroup . format , style . transpose ( ) ? , & variables , & style_variables , ) } fn parse_style < ' a > ( style : Vec < StyleElement > , variables : & ' a StyleVariableMapType < ' a > , ) -> Option < Result < Style , StringFormatterError > > { let style_strings = style . into_iter ( ) . map ( | style | match style { StyleElement :: Text ( text ) => Ok ( text ) , StyleElement :: Variable ( name ) => { let variable = variables . get ( name . as_ref ( ) ) . unwrap_or ( & None ) ; match variable { Some ( style_string ) => style_string . clone ( ) , None => Ok ( " " . into ( ) ) , } } } ) . collect :: < Result < Vec < Cow < str > > , StringFormatterError > > ( ) ; style_strings . map ( | style_strings | { let style_string : String = style_strings . iter ( ) . flat_map ( | s | s . chars ( ) ) . collect ( ) ; parse_style_string ( & style_string ) } ) . transpose ( ) } fn parse_format < ' a > ( format : Vec < FormatElement < ' a > > , style : Option < Style > , variables : & ' a VariableMapType < ' a > , style_variables : & ' a StyleVariableMapType < ' a > , ) -> Result < Vec < Segment > , StringFormatterError > { let results : Result < Vec < Vec < Segment > > , StringFormatterError > = format . into_iter ( ) . map ( | el | { match el { FormatElement :: Text ( text ) => Ok ( vec ! [ Segment new ( style text ) ] ) , FormatElement :: TextGroup ( textgroup ) => { let textgroup = TextGroup { format : textgroup . format , style : textgroup . style , } ; parse_textgroup ( textgroup , & variables , & style_variables ) } FormatElement :: Variable ( name ) => variables . get ( name . as_ref ( ) ) . expect ( " " ) . as_ref ( ) . map ( | segments | match segments . clone ( ) ? { VariableValue :: Styled ( segments ) => Ok ( segments . into_iter ( ) . map ( | mut segment | { // Derive upper style if the style of segments are none. if segment . style . is_none ( ) { segment . style = style ; } ; segment } ) . collect ( ) ) , VariableValue :: Plain ( text ) => Ok ( vec ! [ Segment new ( style text ) ] ) , VariableValue :: Meta ( format ) => { let formatter = StringFormatter { format , variables : clone_without_meta ( variables ) , style_variables : style_variables . clone ( ) , } ; formatter . parse ( style ) } } ) . unwrap_or_else ( | | Ok ( Vec :: new ( ) ) ) , FormatElement :: Conditional ( format ) => { // Show the conditional format string if all the variables inside are not // none. fn should_show_elements < ' a > ( format_elements : & [ FormatElement ] , variables : & ' a VariableMapType < ' a > , ) -> bool { format_elements . get_variables ( ) . iter ( ) . any ( | var | { variables . get ( var . as_ref ( ) ) . map ( | map_result | { let map_result = map_result . as_ref ( ) ; map_result . and_then ( | result | result . as_ref ( ) . ok ( ) ) . map ( | result | match result { // If the variable is a meta variable, also // check the format string inside it. VariableValue :: Meta ( meta_elements ) => { let meta_variables = clone_without_meta ( variables ) ; should_show_elements ( & meta_elements , & meta_variables , ) } _ => true , } ) // The variable is None or Err, or a meta variable // that shouldn't show . unwrap_or ( false ) } ) // Can't find the variable in format string . unwrap_or ( false ) } ) } let should_show : bool = should_show_elements ( & format , variables ) ; if should_show { parse_format ( format , style , variables , style_variables ) } else { Ok ( Vec :: new ( ) ) } } } } ) . collect ( ) ; Ok ( results ? . into_iter ( ) . flatten ( ) . collect ( ) ) } parse_format ( self . format , default_style , & self . variables , & self . style_variables , ) }
Parse the format string and consume self.
Parse the format string and consume self .
self
default_style
Option<Style>