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

Code before:

fn get_pyproject_version(context: &Context, config: &PackageConfig) -> Option<String> {
    let file_contents = context.read_file_from_pwd("pyproject.toml")?;
    let pyproject_toml: toml::Value = toml::from_str(&file_contents).ok()?;

    get_pep621_version(&pyproject_toml)
        .or_else(|| get_poetry_version(&pyproject_toml))
        .and_then(|raw_version| format_version(raw_version, config.version_format))
}

fn get_setup_cfg_version(context: &Context, config: &PackageConfig) -> Option<String> {
    let file_contents = context.read_file_from_pwd("setup.cfg")?;
    let ini = Ini::load_from_str(&file_contents).ok()?;
    let raw_version = ini.get_from(Some("metadata"), "version")?;

    if raw_version.starts_with("attr:") || raw_version.starts_with("file:") {
        None
    } else {
        format_version(raw_version, config.version_format)
    }
}

fn get_gradle_version(context: &Context, config: &PackageConfig) -> Option<String> {
    let build_file_contents = match context.read_file_from_pwd("build.gradle") {
        Some(contents) => contents,
        None => "none".to_string(),
    };

    //check version from gradle.properties file
    if build_file_contents == "none" {
        let properties_file_contents = context.read_file_from_pwd("gradle.properties")?;
        let re = Regex::new(r"/version=.*/gm").unwrap();
        let caps = re.captures(&properties_file_contents)?;
        return format_version(
            &caps["version"].strip_prefix("version=").unwrap(),
            config.version_format,
        );
    }
    //read version of gradle.build file
    let re = Regex::new(r#"(?m)^version ['"](?P<version>[^'"]+)['"]$"#).unwrap(); /*dark magic*/
    let caps = re.captures(&build_file_contents)?;

    format_version(&caps["version"], config.version_format)
}

fn get_composer_version(context: &Context, config: &PackageConfig) -> Option<String> {
    let file_contents = context.read_file_from_pwd("composer.json")?;
    let composer_json: json::Value = json::from_str(&file_contents).ok()?;
    let raw_version = composer_json.get("version")?.as_str()?;

    format_version(raw_version, config.version_format)

Review comment (suggestion):
Is `"none"` used in `build.gradle`? If not, please stick with `Option<String>`, and if it does use `"none"` please filter `Some("none")` into `None` instead and add a test that uses `version "none"`.

Code after:

fn get_pyproject_version(context: &Context, config: &PackageConfig) -> Option<String> {
    let file_contents = context.read_file_from_pwd("pyproject.toml")?;
    let pyproject_toml: toml::Value = toml::from_str(&file_contents).ok()?;

    get_pep621_version(&pyproject_toml)
        .or_else(|| get_poetry_version(&pyproject_toml))
        .and_then(|raw_version| format_version(raw_version, config.version_format))
}

fn get_setup_cfg_version(context: &Context, config: &PackageConfig) -> Option<String> {
    let file_contents = context.read_file_from_pwd("setup.cfg")?;
    let ini = Ini::load_from_str(&file_contents).ok()?;
    let raw_version = ini.get_from(Some("metadata"), "version")?;

    if raw_version.starts_with("attr:") || raw_version.starts_with("file:") {
        None
    } else {
        format_version(raw_version, config.version_format)
    }
}

fn get_gradle_version(context: &Context, config: &PackageConfig) -> Option<String> {
    context
        .read_file_from_pwd("gradle.properties")
        .and_then(|contents| {
            let re = Regex::new(r"version=(?P<version>.*)").unwrap();
            let caps = re.captures(&contents)?;
            format_version(&caps["version"], config.version_format)
        }).or_else(|| {
            let build_file_contents = context.read_file_from_pwd("build.gradle")?;
            let re = Regex::new(r#"(?m)^version ['"](?P<version>[^'"]+)['"]$"#).unwrap(); /*dark magic*/
            let caps = re.captures(&build_file_contents)?;
            format_version(&caps["version"], config.version_format)

        })
}

fn get_composer_version(context: &Context, config: &PackageConfig) -> Option<String> {
    let file_contents = context.read_file_from_pwd("composer.json")?;
    let composer_json: json::Value = json::from_str(&file_contents).ok()?;
    let raw_version = composer_json.get("version")?.as_str()?;

    format_version(raw_version, config.version_format)
}

fn get_julia_project_version(context: &Context, config: &PackageConfig) -> Option<String> {
    let file_contents = context.read_file_from_pwd("Project.toml")?;
    let project_toml: toml::Value = toml::from_str(&file_contents).ok()?;
    let raw_version = project_toml.get("version")?.as_str()?;