Commit 786b09cb authored by Scott Abbey's avatar Scott Abbey

Implement traits for Identifier and Version

parent 2090c7fe
use std::fmt;
use regex::Regex;
use common;
lazy_static! {
......@@ -41,6 +44,7 @@ lazy_static! {
};
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Version {
pub major: u64,
pub minor: u64,
......@@ -49,7 +53,7 @@ pub struct Version {
pub build: Vec<Identifier>,
}
#[derive(Debug,PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Identifier {
/// An identifier that's solely numbers.
Numeric(u64),
......@@ -77,6 +81,32 @@ pub fn parse(version: &str) -> Result<Version, String> {
})
}
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{}.{}.{}", self.major, self.minor, self.patch));
if !self.pre.is_empty() {
let strs: Vec<_> =
self.pre.iter().map(ToString::to_string).collect();
try!(write!(f, "-{}", strs.join(".")));
}
if !self.build.is_empty() {
let strs: Vec<_> =
self.build.iter().map(ToString::to_string).collect();
try!(write!(f, "+{}", strs.join(".")));
}
Ok(())
}
}
impl fmt::Display for Identifier {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Identifier::Numeric(ref id) => id.fmt(f),
Identifier::AlphaNumeric(ref id) => id.fmt(f),
}
}
}
#[cfg(test)]
mod tests {
use version;
......
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