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

Code before:
        .any(|v| v.as_str() == Some("version"))
        .then(|| parse_pep621_dynamic_version(context, pyproject))?
}

fn get_pep621_static_version(pyproject: &toml::Table) -> Option<String> {
    pyproject
        .get("project")?
        .get("version")?
        .as_str()
        .map(|s| s.to_owned())
}

fn get_pep621_version(context: &Context, pyproject: &toml::Table) -> Option<String> {
    get_pep621_static_version(pyproject).or_else(|| get_pep621_dynamic_version(context, pyproject))
}

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

    get_pep621_version(context, &pyproject_toml)
        .or_else(|| get_poetry_version(&pyproject_toml))
        .and_then(|raw_version| format_string_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"(?m)^\s*version\s*=\s*(?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( |\s*=\s*)['"](?P<version>[^'"]+)['"]$"#).unwrap(); /*dark magic*/
            let caps = re.captures(&build_file_contents)?;
            format_version(&caps["version"], config.version_format)

        })

Review comment (suggestion):
passing through the context allows us to use `context.current_dir` to access files referenced by `pyproject.toml` above

Code after:
    pyproject
        .get("project")?
        .get("dynamic")?
        .as_array()?
        .iter()
        .any(|v| v.as_str() == Some("version"))
        .then(|| parse_pep621_dynamic_version(context, pyproject))?
}

fn get_pep621_static_version(pyproject: &toml::Table) -> Option<String> {
    pyproject
        .get("project")?
        .get("version")?
        .as_str()
        .map(|s| s.to_owned())
}

fn get_pep621_version(context: &Context, pyproject: &toml::Table) -> Option<String> {
    get_pep621_static_version(pyproject).or_else(|| get_pep621_dynamic_version(context, pyproject))
}

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

    get_pep621_version(context, &pyproject_toml)
        .or_else(|| get_poetry_version(&pyproject_toml))
        .and_then(|raw_version| format_string_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"(?m)^\s*version\s*=\s*(?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")?;