Repository: starship/starship
File: src/modules/meson.rs (Rust)

Code before:
use crate::configs::meson::MesonConfig;
use crate::formatter::StringFormatter;

/// Creates a module with the current Meson dev environment
///
/// Will display the Meson environment if `$MESON_DEVENV` and `MESON_PROJECT_NAME` are set.
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let meson_env = context.get_env("MESON_DEVENV").unwrap_or_default();
    let project_env = context.get_env("MESON_PROJECT_NAME").unwrap_or_default();
    if meson_env != "1" || project_env.trim().is_empty() {
        return None;
    }

    let mut module = context.new_module("meson");
    let config: MesonConfig = MesonConfig::try_load(module.config);

    let truncation_symbol = get_first_grapheme(config.truncation_symbol);
    let len = if config.truncation_length <= 0 {
        log::warn!(
            "\"truncation_length\" should be a positive value, found {}",
            config.truncation_length
        );
        std::usize::MAX
    } else {
        config.truncation_length as usize
    };

    let mut project_name_graphemes: Vec<&str> = project_env.graphemes(true).collect();

    let e = &mut (&mut project_name_graphemes);
    let e = &mut **e;
    let trunc_len = len.min(e.len());
    if trunc_len < e.len() {
        // The truncation symbol should only be added if we truncate
        e[trunc_len] = truncation_symbol;
        e.truncate(trunc_len + 1);
    }

    let parsed = StringFormatter::new(config.format).and_then(|formatter| {
        formatter
            .map_meta(|variable, _| match variable {
                "symbol" => Some(config.symbol),
                _ => None,
            })
            .map_style(|variable| match variable {
                "style" => Some(Ok(config.style)),
                _ => None,
            })
            .map(|variable| match variable {
                "project" => Some(Ok(project_name_graphemes.concat())),
                _ => None,

Review comment (suggestion):
```suggestion
    let len = config.truncation_length as usize;
```

Code after:
use crate::formatter::StringFormatter;

/// Creates a module with the current Meson dev environment
///
/// Will display the Meson environment if `$MESON_DEVENV` and `MESON_PROJECT_NAME` are set.
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let meson_env = context.get_env("MESON_DEVENV")?;
    let project_env = context.get_env("MESON_PROJECT_NAME")?;
    if meson_env != "1" || project_env.trim().is_empty() {
        return None;
    }

    let mut module = context.new_module("meson");
    let config: MesonConfig = MesonConfig::try_load(module.config);

    let truncated_text = truncate_text(
        &project_env,
        config.truncation_length as usize,
        config.truncation_symbol,
    );

    let parsed = StringFormatter::new(config.format).and_then(|formatter| {
        formatter
            .map_meta(|variable, _| match variable {
                "symbol" => Some(config.symbol),
                _ => None,
            })
            .map_style(|variable| match variable {
                "style" => Some(Ok(config.style)),
                _ => None,
            })
            .map(|variable| match variable {
                "project" => Some(Ok(&truncated_text)),
                _ => None,
            })
            .parse(None, Some(context))
    });

    module.set_segments(match parsed {
        Ok(segments) => segments,
        Err(error) => {
            log::warn!("Error in module `meson`:\n{}", error);
            return None;
        }
    });

    Some(module)
}

#[cfg(test)]
mod tests {