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

Code before:

    Some(formatted_version)
}

fn get_poetry_version(pyproject: &toml::Value, config: &PackageConfig) -> Option<String> {
    let raw_version = pyproject
        .get("tool")?
        .get("poetry")?
        .get("version")?
        .as_str()?;

    format_version(raw_version, config.version_format)
}

fn get_pep621_version(pyproject: &toml::Value, config: &PackageConfig) -> Option<String> {
    let raw_version = pyproject.get("project")?.get("version")?.as_str()?;
    format_version(raw_version, config.version_format)
}

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, config)
        .or_else(|| get_poetry_version(&pyproject_toml, config))
}

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 file_contents = context.read_file_from_pwd("build.gradle")?;
    let re = Regex::new(r#"(?m)^version ['"](?P<version>[^'"]+)['"]$"#).unwrap();
    let caps = re.captures(&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()?;

Review comment (suggestion):
This looks good, but I would prefer handling `format_version` in `get_pyproject_version`:
```rust
fn get_poetry_version<'a>(pyproject: &'a toml::Value) -> Option<&'a str> {
    pyproject
        .get("tool")?
        .get("poetry")?
        .get("version")?
        .as_str()
}

fn get_pep621_version<'a>(pyproject: &'a toml::Value) -> Option<&'a str> {
    pyproject.get("project")?.get("version")?.as_str()
}

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))
}
```

Code after:

    Some(formatted_version)
}

fn get_poetry_version(pyproject: &toml::Value) -> Option<&str> {
    pyproject
        .get("tool")?
        .get("poetry")?
        .get("version")?
        .as_str()
}

fn get_pep621_version(pyproject: &toml::Value) -> Option<&str> {
    pyproject.get("project")?.get("version")?.as_str()
}

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 file_contents = context.read_file_from_pwd("build.gradle")?;
    let re = Regex::new(r#"(?m)^version ['"](?P<version>[^'"]+)['"]$"#).unwrap();
    let caps = re.captures(&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)