Commit bd27ecfa authored by Scott Abbey's avatar Scott Abbey

Introduce parse_meta and Identifier for metadata strings

parent 1d96fcf6
......@@ -52,8 +52,16 @@ pub struct Version {
pub major: u64,
pub minor: u64,
pub patch: u64,
pub pre: Option<Vec<String>>,
pub build: Option<Vec<String>>,
pub pre: Option<Vec<Identifier>>,
pub build: Option<Vec<Identifier>>,
}
#[derive(Debug,PartialEq)]
pub enum Identifier {
/// An identifier that's solely numbers.
Numeric(u64),
/// An identifier with letters and numbers.
AlphaNumeric(String),
}
pub fn parse_version(version: &str) -> Result<Version, Box<Error>> {
......@@ -62,13 +70,9 @@ pub fn parse_version(version: &str) -> Result<Version, Box<Error>> {
None => return Err(From::from("Version did not parse properly.")),
};
let pre = captures.name("pre").map(|pre| {
vec![pre.to_string()]
});
let pre = captures.name("pre").map(parse_meta);
let build = captures.name("build").map(|build| {
vec![build.to_string()]
});
let build = captures.name("build").map(parse_meta);
Ok(Version {
major: captures.name("major").unwrap().parse().unwrap(),
......@@ -79,6 +83,26 @@ pub fn parse_version(version: &str) -> Result<Version, Box<Error>> {
})
}
// by the time we get here, we know that it's all valid characters, so this doesn't need to return
// a result or anything
fn parse_meta(pre: &str) -> Vec<Identifier> {
// Originally, I wanted to implement this method via calling parse, but parse is tolerant of
// leading zeroes, and we want anything with leading zeroes to be considered alphanumeric, not
// numeric. So the strategy is to check with a regex first, and then call parse once we've
// determined that it's a number without a leading zero.
let regex = Regex::new(r"^[1-9][0-9]*$").unwrap();
pre.split(".")
.map(|part| {
if regex.is_match(part) {
// we can unwrap here because we know it is only digits due to the regex
Identifier::Numeric(part.parse().unwrap())
} else {
Identifier::AlphaNumeric(part.to_string())
}
}).collect()
}
#[cfg(test)]
mod tests {
use super::*;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment