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

Code before:

/// Creates a module with the current Nim version
///
/// Will display the Nim version if any of the following criteria are met:
///     - The current directory contains a file with extension `.nim`, `.nims`, or `.nimble`
///     - The current directory contains a `nim.cfg` file
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let is_nim_project = context
        .try_begin_scan()?
        .set_files(&["nim.cfg"])
        .set_extensions(&["nim", "nims", "nimble"])
        .is_match();

    if !is_nim_project {
        return None;
    }
    let nim_version_output = utils::exec_cmd("nim", &["--version"])?.stdout;
    let nim_version = parse_nim_version(&nim_version_output)?;

    let mut module = context.new_module("nim");
    let config = NimConfig::try_load(module.config);

    module.set_style(config.style);

    module.create_segment("symbol", &config.symbol);
    module.create_segment("version", &SegmentConfig::new(nim_version));

    Some(module)
}

fn parse_nim_version(version_cmd_output: &str) -> Option<&str> {
    version_cmd_output
        .lines()
        // First line has the version
        .next()?
        .split(' ')
        .find(|&s| s.chars().all(|c| c >= '0' && c <= '9' || c == '.'))
}

#[cfg(test)]
mod tests {
    use super::parse_nim_version;
    use crate::modules::utils::test::render_module;
    use ansi_term::Color;
    use std::fs::File;
    use std::io;

    #[test]
    fn nim_version() {
        let ok_versions = ["1.1.1", "2", "."];
        let not_ok_versions = ["abc", " \n.", ". ", "abc."];

Review comment (suggestion):
```suggestion
    module.create_segment("version", &config.version.with_value(&nim_version));
```

Code after:

/// Creates a module with the current Nim version
///
/// Will display the Nim version if any of the following criteria are met:
///     - The current directory contains a file with extension `.nim`, `.nims`, or `.nimble`
///     - The current directory contains a `nim.cfg` file
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let is_nim_project = context
        .try_begin_scan()?
        .set_files(&["nim.cfg"])
        .set_extensions(&["nim", "nims", "nimble"])
        .is_match();

    if !is_nim_project {
        return None;
    }

    let nim_version_output = utils::exec_cmd("nim", &["--version"])?.stdout;
    let formatted_nim_version = format!("v{}", parse_nim_version(&nim_version_output)?);

    let mut module = context.new_module("nim");
    let config = NimConfig::try_load(module.config);

    module.set_style(config.style);

    module.create_segment("symbol", &config.symbol);
    module.create_segment("version", &SegmentConfig::new(&formatted_nim_version));

    Some(module)
}

fn parse_nim_version(version_cmd_output: &str) -> Option<&str> {
    version_cmd_output
        .lines()
        // First line has the version
        .next()?
        .split(' ')
        .find(|&s| s.chars().all(|c| c >= '0' && c <= '9' || c == '.'))
}

#[cfg(test)]
mod tests {
    use super::parse_nim_version;
    use crate::modules::utils::test::render_module;
    use ansi_term::Color;
    use std::fs::File;
    use std::io;

    #[test]
    fn nim_version() {
        let ok_versions = ["1.1.1", "2", "."];