Commit ffcc1117 authored by Scott Abbey's avatar Scott Abbey

Add support for other range types

parent 7cc297e8
use regex::Regex;
use common;
use version::Identifier;
use std::str::FromStr;
......@@ -117,4 +118,73 @@ pub fn parse(range: &str) -> Result<VersionReq, String> {
}],
});
}
let captures = match REGEX.captures(range.trim()) {
Some(captures) => captures,
None => return Err(From::from("VersionReq did not parse properly.")),
};
// operations default to Compatible
// unwrap is okay because we validate that we only have correct strings in the regex
let mut operation = captures.name("operation")
.map(str::parse)
.map(Result::unwrap)
.unwrap_or(Op::Compatible);
// first unwrap is okay because we always have major
// second unwrap is okay becasue we know it's a number
let major = captures.name("major")
.unwrap()
.parse()
.unwrap();
let minor = captures.name("minor");
// oh my what have I done? This code is gross.
let minor = if minor.is_some() {
let minor = minor.unwrap();
match minor.parse() {
Ok(number) => Some(number),
Err(_) => {
// if we get an error, it's because it's a wildcard
operation = Op::Wildcard(WildcardVersion::Minor);
None
},
}
} else {
None
};
let patch = captures.name("patch");
// oh my what have I done? This code is gross.
let patch = if patch.is_some() {
let patch = patch.unwrap();
match patch.parse() {
Ok(number) => Some(number),
Err(_) => {
// if we get an error, it's because it's a wildcard
operation = Op::Wildcard(WildcardVersion::Patch);
None
},
}
} else {
None
};
let pre = captures.name("pre").map(common::parse_meta).unwrap_or_else(Vec::new);
let predicate = Predicate {
op: operation,
major: major,
minor: minor,
patch: patch,
pre: pre,
};
Ok(VersionReq {
predicates: vec![predicate],
})
}
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