mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-11-28 07:46:28 +00:00
fix: handle package version panic (#57)
This commit is contained in:
parent
2cf69a82b7
commit
876e7d36b1
@ -1,5 +1,4 @@
|
||||
use ansi_term::Color;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
use super::{Context, Module};
|
||||
@ -53,7 +52,7 @@ fn get_go_version() -> Option<String> {
|
||||
}
|
||||
|
||||
fn format_go_version(go_stdout: String) -> Option<String> {
|
||||
let mut version = go_stdout
|
||||
let version = go_stdout
|
||||
// split into ["", "1.12.4 linux/amd64"]
|
||||
.splitn(2, "go version go")
|
||||
// return "1.12.4 linux/amd64"
|
||||
|
@ -49,51 +49,42 @@ fn read_file(file_name: &str) -> io::Result<String> {
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
fn extract_cargo_version(file_contents: String) -> Option<String> {
|
||||
fn extract_cargo_version(file_contents: &str) -> Option<String> {
|
||||
let cargo_toml = file_contents.parse::<toml::Value>().ok()?;
|
||||
let raw_version = cargo_toml.get("package")?.get("version")?.as_str()?;
|
||||
|
||||
match cargo_toml["package"]["version"].as_str() {
|
||||
Some(raw_version) => {
|
||||
let version = format_version(raw_version.to_string());
|
||||
Some(version)
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
let formatted_version = format_version(raw_version);
|
||||
Some(formatted_version)
|
||||
}
|
||||
|
||||
fn extract_package_version(file_contents: String) -> Option<String> {
|
||||
let json: Option<serde_json::Value> = serde_json::from_str(&file_contents).ok()?;
|
||||
fn extract_package_version(file_contents: &str) -> Option<String> {
|
||||
let package_json: serde_json::Value = serde_json::from_str(&file_contents).ok()?;
|
||||
let raw_version = package_json.get("version")?.as_str()?;
|
||||
if raw_version == "null" {
|
||||
return None;
|
||||
};
|
||||
|
||||
match json {
|
||||
Some(json) => {
|
||||
let raw_version = json["version"].to_string();
|
||||
if raw_version == "null" {
|
||||
None
|
||||
} else {
|
||||
Some(format_version(raw_version))
|
||||
}
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
let formatted_version = format_version(raw_version);
|
||||
Some(formatted_version)
|
||||
}
|
||||
|
||||
fn get_package_version(context: &Context) -> Option<String> {
|
||||
let has_cargo_toml = context.dir_files.iter().any(is_cargo_toml);
|
||||
if has_cargo_toml {
|
||||
let file_contents = read_file("Cargo.toml").ok()?;
|
||||
return extract_cargo_version(file_contents);
|
||||
return extract_cargo_version(&file_contents);
|
||||
}
|
||||
|
||||
let has_package_json = context.dir_files.iter().any(is_package_json);
|
||||
if has_package_json {
|
||||
let file_contents = read_file("package.json").ok()?;
|
||||
return extract_package_version(file_contents);
|
||||
return extract_package_version(&file_contents);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn format_version(version: String) -> String {
|
||||
fn format_version(version: &str) -> String {
|
||||
format!("v{}", version.replace('"', "").trim())
|
||||
}
|
||||
|
||||
@ -103,7 +94,51 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_format_version() {
|
||||
let input = String::from("0.1.0");
|
||||
assert_eq!(format_version(input), "v0.1.0");
|
||||
assert_eq!(format_version("0.1.0"), "v0.1.0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extract_cargo_version() {
|
||||
let cargo_with_version = "[package]
|
||||
name = \"starship\"
|
||||
version = \"0.1.0\"";
|
||||
|
||||
let expected_version = Some("v0.1.0".to_string());
|
||||
assert_eq!(extract_cargo_version(&cargo_with_version), expected_version);
|
||||
|
||||
let cargo_without_version = "[package]
|
||||
name = \"starship\"";
|
||||
|
||||
let expected_version = None;
|
||||
assert_eq!(
|
||||
extract_cargo_version(&cargo_without_version),
|
||||
expected_version
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extract_package_version() {
|
||||
let package_with_version = serde_json::json!({
|
||||
"name": "spacefish",
|
||||
"version": "0.1.0"
|
||||
})
|
||||
.to_string();
|
||||
|
||||
let expected_version = Some("v0.1.0".to_string());
|
||||
assert_eq!(
|
||||
extract_package_version(&package_with_version),
|
||||
expected_version
|
||||
);
|
||||
|
||||
let package_without_version = serde_json::json!({
|
||||
"name": "spacefish"
|
||||
})
|
||||
.to_string();
|
||||
|
||||
let expected_version = None;
|
||||
assert_eq!(
|
||||
extract_package_version(&package_without_version),
|
||||
expected_version
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user