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

Code before:
                // but on some platforms gcc is installed as *gcc*, not cc
                &["gcc", "--version"],
                // for completeness, although I've never seen a clang that wasn't cc
                &["clang", "--version"],
            ])
        });

        formatter
            .map_meta(|var, _| match var {
                "symbol" => Some(config.symbol),
                _ => None,
            })
            .map_style(|variable| match variable {
                "style" => Some(Ok(config.style)),
                _ => None,
            })
            .map(|variable| match variable {
                "compiler_name" => {
                    let c_compiler_info = &c_compiler_info.deref().as_ref()?.stdout;

                    let c_compiler = if c_compiler_info.contains("clang") {
                        "clang"
                    } else if c_compiler_info.contains("Free Software Foundation") {
                        "gcc"
                    } else {
                        "Unknown compiler"
                    };
                    Some(c_compiler).map(Cow::Borrowed).map(Ok)
                }
                "compiler_version" => {
                    let c_compiler_info = &c_compiler_info.deref().as_ref()?.stdout;
                    if config.format.contains("$compiler_version")
                        && (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)

Review comment (suggestion):
I would prefer `None` in the case of an unknown compiler.

Code after:
            .map_meta(|var, _| match var {
                "symbol" => Some(config.symbol),
                _ => None,
            })
            .map_style(|variable| match variable {
                "style" => Some(Ok(config.style)),
                _ => None,
            })
            .map(|variable| match variable {
                "name" => {
                    let c_compiler_info = &c_compiler_info.deref().as_ref()?.stdout;

                    let c_compiler = if c_compiler_info.contains("clang") {
                        "clang"
                    } else if c_compiler_info.contains("Free Software Foundation") {
                        "gcc"
                    } else {
                        return None;
                    };
                    Some(c_compiler).map(Cow::Borrowed).map(Ok)
                }
                "version" => {
                    let c_compiler_info = &c_compiler_info.deref().as_ref()?.stdout;

                    // Clang says ...
                    //   Apple clang version 13.0.0 ...\n
                    //   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))