Commit 9846f184 authored by Scott Abbey's avatar Scott Abbey

Move parse_meta to common.rs and rename parse_version

parent 835a7524
use regex::Regex;
use version::Identifier;
// 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
pub 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| {
// another wrinkle: we made sure that any number starts with a non-zero. But there's a
// problem: an actual zero is a number, yet gets left out by this heuristic. So let's
// also check for the single, lone zero.
if regex.is_match(part) || part == "0" {
// 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()
}
......@@ -4,3 +4,6 @@ extern crate regex;
extern crate lazy_static;
pub mod version;
// for private stuff the two share
mod common;
use regex::Regex;
use common;
lazy_static! {
static ref REGEX: Regex = {
......@@ -57,15 +58,15 @@ pub enum Identifier {
AlphaNumeric(String),
}
pub fn parse_version(version: &str) -> Result<Version, String> {
pub fn parse(version: &str) -> Result<Version, String> {
let captures = match REGEX.captures(version.trim()) {
Some(captures) => captures,
None => return Err(From::from("Version did not parse properly.")),
};
let pre = captures.name("pre").map(parse_meta);
let pre = captures.name("pre").map(common::parse_meta);
let build = captures.name("build").map(parse_meta);
let build = captures.name("build").map(common::parse_meta);
Ok(Version {
major: captures.name("major").unwrap().parse().unwrap(),
......@@ -76,29 +77,6 @@ pub fn parse_version(version: &str) -> Result<Version, String> {
})
}
// 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| {
// another wrinkle: we made sure that any number starts with a non-zero. But there's a
// problem: an actual zero is a number, yet gets left out by this heuristic. So let's
// also check for the single, lone zero.
if regex.is_match(part) || part == "0" {
// 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