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

Code before:
    {
        return None;
    }

    let raw_version = package_json.get("version")?.as_str()?;
    if raw_version == "null" {
        return None;
    };

    let formatted_version = format_version(raw_version, config.version_format)?;
    if formatted_version == "v0.0.0-development" || formatted_version.starts_with("v0.0.0-semantic")
    {
        return Some("semantic".to_string());
    };

    Some(formatted_version)
}

fn get_jsr_package_version(context: &Context, config: &PackageConfig) -> Option<String> {
    let json_content = context
        .read_file_from_pwd("jsr.json")
        .and_then(|contents| json::from_str(&contents).unwrap_or_default())
        .or_else(|| json::from_str(&context.read_file_from_pwd("deno.json")?).unwrap_or_default())
        .or_else(|| {
            jsonc_parser::parse_to_serde_value(
                &context.read_file_from_pwd("deno.jsonc")?,
                &Default::default(),
            )
            .unwrap_or_default()
        })?;

    let raw_version = json_content.get("version")?.as_str()?;
    format_version(raw_version, config.version_format)
}

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

fn get_pep621_version(pyproject: &toml::Table) -> 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::Table = toml::from_str(&file_contents).ok()?;


Review comment (suggestion):
This should probably cover `jsr.jsonc`. I would also prefer if this code use`.find()` with an iterator over a list of the filenames.

Code after:
    {
        return None;
    }

    let raw_version = package_json.get("version")?.as_str()?;
    if raw_version == "null" {
        return None;
    };

    let formatted_version = format_version(raw_version, config.version_format)?;
    if formatted_version == "v0.0.0-development" || formatted_version.starts_with("v0.0.0-semantic")
    {
        return Some("semantic".to_string());
    };

    Some(formatted_version)
}

fn get_jsr_package_version(context: &Context, config: &PackageConfig) -> Option<String> {
    let (filename, contents) = ["deno.json", "deno.jsonc", "jsr.json", "jsr.jsonc"]
        .iter()
        .find_map(|filename| {
            context
                .read_file_from_pwd(filename)
                .map(|contents| (filename, contents))
        })?;

    let json_content: json::Value = if filename.ends_with(".jsonc") {
        jsonc_parser::parse_to_serde_value(&contents, &Default::default()).ok()??
    } else {
        json::from_str(&contents).ok()?
    };

    let raw_version = json_content.get("version")?.as_str()?;
    format_version(raw_version, config.version_format)
}

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

fn get_pep621_version(pyproject: &toml::Table) -> 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")?;