use std::fmt::{self, Display, Formatter};
use std::str::FromStr;
#[derive(Copy, Debug)]
pub struct Version(u32, u32, u32);
impl Version {
pub fn new(major: u32, minor: u32, patch: u32) -> Version {
Version(major, minor, patch)
}
}
impl Display for Version {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let &Version(major, minor, patch) = self;
write!(f, "{}:{}:{}", major, minor, patch)
}
}
macro_rules! try_option(
($inp:expr) => (
match $inp {
Some(v) => { v },
None => { return None; },
}
);
);
impl FromStr for Version {
fn from_str(s: &str) -> Option<Version> {
let mut sp = s.split('.');
let major = match sp.next() {
Some(s) => try_option!(s.parse()),
None => return None,
};
let minor = match sp.next() {
Some(s) => try_option!(s.parse()),
None => 0,
};
let patch = match sp.next() {
Some(s) => try_option!(s.parse()),
None => 0,
};
Some(Version::new(major, minor, patch))
}
}