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

Code before:
                    };
                    Some(c_compiler).map(Cow::Borrowed).map(Ok)
                }
                "compiler_version" => {
                    let c_compiler_info = &c_compiler_info.deref().as_ref()?.stdout;
                    if c_compiler_info.contains("clang")
                        || c_compiler_info.contains("Free Software Foundation")
                    {
                        // Clang says ...
                        //   Apple clang version 13.0.0 ...\n
                        //   OpenBSD clang version 11.1.0\n...
                        //   FreeBSD clang version 11.0.1 ...\n
                        // so seems to have the version always in position 3.
                        // gcc says ...
                        //   gcc (OmniOS 151036/9.3.0-il-1) 9.3.0\n...
                        //   gcc (Debian 10.2.1-6) 10.2.1 ...\n
                        // so again in position 3.
                        // But this is CRUFTY AS HELL
                        VersionFormatter::format_module_version(
                            module.get_name(),
                            c_compiler_info.split_whitespace().nth(3)?,
                            config.version_format,
                        )
                        .map(Cow::Owned)
                        .map(Ok)
                    } else {
                        None
                    }
                }
                _ => None,
            })
            .parse(None, Some(context))
    });

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

    Some(module)
}

#[cfg(test)]
mod tests {
    use crate::{test::ModuleRenderer, utils::CommandOutput};
    use ansi_term::Color;
    use std::fs::File;
    use std::io;

Review comment (suggestion):
Maybe add `log::error!("Unknown C compiler.")` here, if you want to keep restricting this.

Code after:
                    //   OpenBSD clang version 11.1.0\n...
                    //   FreeBSD clang version 11.0.1 ...\n
                    // so we always want the first semver-ish whitespace-
                    // separated "word".
                    // gcc says ...
                    //   gcc (OmniOS 151036/9.3.0-il-1) 9.3.0\n...
                    //   gcc (Debian 10.2.1-6) 10.2.1 ...\n
                    //   cc (GCC) 3.3.5 (Debian 1:3.3.5-13)\n...
                    // so again we always want the first semver-ish word.
                    VersionFormatter::format_module_version(
                        module.get_name(),
                        c_compiler_info.split_whitespace().find_map(
                            |word| match Version::parse(word) {
                                Ok(_v) => Some(word),
                                Err(_e) => None,
                            },
                        )?,
                        config.version_format,
                    )
                    .map(Cow::Owned)
                    .map(Ok)
                }
                _ => None,
            })
            .parse(None, Some(context))
    });

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

    Some(module)
}

#[cfg(test)]
mod tests {
    use crate::{test::ModuleRenderer, utils::CommandOutput};
    use ansi_term::Color;
    use std::fs::File;
    use std::io;

    #[test]
    fn folder_without_c_files() -> io::Result<()> {
        let dir = tempfile::tempdir()?;

        let actual = ModuleRenderer::new("c").path(dir.path()).collect();