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

Code before:

    // Parse config under `display`.
    // Select the style that is most minimally greater than the current battery percentage.
    // If no such style exists do not display battery module.
    let mut closest_style = None;
    for display_style in &config.display {
        // Skip style if its threshold is lower than the current battery percentage.
        if percentage > display_style.threshold as f32 {
            continue;
        }
        // Unwrap the `Option`
        let Some(closest_so_far) = closest_style else {
            // If `closest_style` is `None` then this is the first valid style
            // encountered, so save it and continue to the next loop iteration.
            closest_style = Some(display_style);
            continue;
        };
        // If this style's threshold is less than the previous closest so far,
        // save it and continue to the next loop iteration
        if display_style.threshold < closest_so_far.threshold {
            closest_style = Some(display_style);
            continue;
        }
    }
    let display_style = closest_style?;
    //let display_style = config.display.iter().find(|display_style| percentage <= display_style.threshold as f32)?;

    // Parse the format string and build the module
    match StringFormatter::new(config.format) {
        Ok(formatter) => {
            let formatter = formatter
                .map_meta(|variable, _| match variable {
                    "symbol" => match state {
                        battery::State::Full => Some(config.full_symbol),
                        battery::State::Charging => display_style
                            .charging_symbol
                            .or(Some(config.charging_symbol)),
                        battery::State::Discharging => display_style
                            .discharging_symbol
                            .or(Some(config.discharging_symbol)),
                        battery::State::Unknown => Some(config.unknown_symbol),
                        battery::State::Empty => Some(config.empty_symbol),
                    },
                    _ => None,
                })
                .map_style(|style| match style {
                    "style" => Some(Ok(display_style.style)),
                    _ => None,
                })
                .map(|variable| match variable {
                    "percentage" => Some(Ok(format!("{}%", percentage.round()))),

Review comment (suggestion):
I think that it would be easier to handle it like this:
```suggestion
    let display_style = config
        .display
        .iter()
        .filter(|display_style| percentage <= display_style.threshold as f32)
        .min_by_key(|display_style| display_style.threshold)?;
```

Code after:

    // Parse config under `display`.
    // Select the style that is most minimally greater than the current battery percentage.
    // If no such style exists do not display battery module.
    let display_style = config
        .display
        .iter()
        .filter(|display_style| percentage <= display_style.threshold as f32)
        .min_by_key(|display_style| display_style.threshold)?;

    // Parse the format string and build the module
    match StringFormatter::new(config.format) {
        Ok(formatter) => {
            let formatter = formatter
                .map_meta(|variable, _| match variable {
                    "symbol" => match state {
                        battery::State::Full => Some(config.full_symbol),
                        battery::State::Charging => display_style
                            .charging_symbol
                            .or(Some(config.charging_symbol)),
                        battery::State::Discharging => display_style
                            .discharging_symbol
                            .or(Some(config.discharging_symbol)),
                        battery::State::Unknown => Some(config.unknown_symbol),
                        battery::State::Empty => Some(config.empty_symbol),
                    },
                    _ => None,
                })
                .map_style(|style| match style {
                    "style" => Some(Ok(display_style.style)),
                    _ => None,
                })
                .map(|variable| match variable {
                    "percentage" => Some(Ok(format!("{}%", percentage.round()))),
                    _ => None,
                });

            match formatter.parse(None, Some(context)) {
                Ok(format_string) => {
                    module.set_segments(format_string);
                    Some(module)
                }
                Err(e) => {
                    log::warn!("Cannot parse `battery.format`: {}", e);
                    None
                }
            }
        }
        Err(e) => {
            log::warn!("Cannot load `battery.format`: {}", e);
            None