Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow parsing not-quite-semver versions #25

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 79 additions & 6 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ pub fn parse(version: &str) -> Result<Version, String> {
if let Some(len) = b'.'.p(&s[i..]) {
i += len;
} else {
if i == s.len() {
return Ok(Version {
major: major,
minor: 0,
patch: 0,
pre: vec![],
build: vec![],
});
}
return Err("Expected dot".to_string());
}
let minor = if let Some((minor, len)) = numeric_identifier(&s[i..]) {
Expand All @@ -45,13 +54,27 @@ pub fn parse(version: &str) -> Result<Version, String> {
if let Some(len) = b'.'.p(&s[i..]) {
i += len;
} else {
return Err("Expected dot".to_string());
if i == s.len() {
return Ok(Version {
major: major,
minor: minor,
patch: 0,
pre: vec![],
build: vec![],
});
}
if s[i] != b'-' && s[i] != b'+' {
return Err("Expected dot".to_string());
}
}
let patch = if let Some((patch, len)) = numeric_identifier(&s[i..]) {
i += len;
patch
} else {
return Err("Error parsing patch identifier".to_string());
if s[i] != b'-' && s[i] != b'+' {
return Err("Error parsing patch identifier".to_string());
}
0
};
let (pre, pre_len) = common::parse_optional_meta(&s[i..], b'-')?;
i += pre_len;
Expand Down Expand Up @@ -123,18 +146,22 @@ mod tests {
fn parse_no_minor_patch() {
let version = "1";

let parsed = version::parse(version);
let parsed = version::parse(version).unwrap();

assert!(parsed.is_err(), format!("'{}' incorrectly considered a valid parse", version));
assert_eq!(1, parsed.major);
assert_eq!(0, parsed.minor);
assert_eq!(0, parsed.patch);
}

#[test]
fn parse_no_patch() {
let version = "1.2";

let parsed = version::parse(version);
let parsed = version::parse(version).unwrap();

assert!(parsed.is_err(), format!("'{}' incorrectly considered a valid parse", version));
assert_eq!(1, parsed.major);
assert_eq!(2, parsed.minor);
assert_eq!(0, parsed.patch);
}

#[test]
Expand Down Expand Up @@ -271,6 +298,29 @@ mod tests {
assert_eq!(expected_pre, parsed.pre);
}

#[test]
fn parse_pre_on_minor() {
let version = "1.2-beta";

let parsed = version::parse(version).unwrap();

assert_eq!(1, parsed.major);
assert_eq!(2, parsed.minor);
assert_eq!(0, parsed.patch);

let expected_pre = vec![Identifier::AlphaNumeric(String::from("beta"))];
assert_eq!(expected_pre, parsed.pre);
}

#[test]
fn parse_pre_on_major() {
let version = "1-beta";

let parsed = version::parse(version);

assert!(parsed.is_err(), "1-beta incorrectly considered a valid major version");
}

#[test]
fn parse_basic_build() {
let version = "1.2.3+build";
Expand Down Expand Up @@ -362,4 +412,27 @@ mod tests {
let expected_pre = vec![Identifier::AlphaNumeric(String::from("WIP"))];
assert_eq!(expected_pre, parsed.pre);
}

#[test]
fn parse_build_on_minor() {
let version = "1.2+build";

let parsed = version::parse(version).unwrap();

assert_eq!(1, parsed.major);
assert_eq!(2, parsed.minor);
assert_eq!(0, parsed.patch);

let expected_build = vec![Identifier::AlphaNumeric(String::from("build"))];
assert_eq!(expected_build, parsed.build);
}

#[test]
fn parse_build_on_major() {
let version = "1+build";

let parsed = version::parse(version);

assert!(parsed.is_err(), "1+build incorrectly considered a valid major version");
}
}