Skip to content

Commit

Permalink
Fix invalid version for os with 0 as patch number (#37)
Browse files Browse the repository at this point in the history
* Fix invalid version for os with 0 as patch number

* Address PR comments
  • Loading branch information
Nico Chatzi authored May 21, 2021
1 parent 52ed5f4 commit 9011b00
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-instruments"
version = "0.4.0"
version = "0.4.1"
authors = ["Colin Rofls <[email protected]>"]
license = "MIT"
description = "Profile binary targets on macOS using Xcode Instruments."
Expand Down
37 changes: 33 additions & 4 deletions src/instruments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,27 @@ fn get_macos_version() -> Result<Version> {
return Err(anyhow!("macOS version cannot be determined"));
}

let version_string = std::str::from_utf8(&stdout)?;
Version::parse(&version_string).map_err(|error| {
anyhow!("cannot parse version: `{}`, because of {}", version_string, error)
})
semver_from_utf8(&stdout)
}

/// Returns a semver given a slice of bytes
///
/// This function tries to construct a semver struct given a raw utf8 byte array
/// that may not contain a patch number, `"11.1"` is parsed as `"11.1.0"`.
fn semver_from_utf8(version: &[u8]) -> Result<Version> {
let to_semver = |version_string: &str| {
Version::parse(&version_string).map_err(|error| {
anyhow!("cannot parse version: `{}`, because of {}", version_string, error)
})
};

let version_string = std::str::from_utf8(version)?;
match version_string.split('.').count() {
1 => to_semver(&format!("{}.0.0", version_string.trim())),
2 => to_semver(&format!("{}.0", version_string.trim())),
3 => to_semver(version_string),
_ => Err(anyhow!("invalid version: {}", version_string)),
}
}

/// Parse xctrace template listing.
Expand Down Expand Up @@ -466,3 +483,15 @@ fn get_tty() -> Result<Option<String>> {
.next()
.map(|tty| format!("/dev/{}", tty)))
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn semvers_can_be_parsed() {
assert_eq!(semver_from_utf8(b"2.3.4").unwrap(), Version::parse("2.3.4").unwrap());
assert_eq!(semver_from_utf8(b"11.1").unwrap(), Version::parse("11.1.0").unwrap());
assert_eq!(semver_from_utf8(b"11").unwrap(), Version::parse("11.0.0").unwrap());
}
}

0 comments on commit 9011b00

Please sign in to comment.