diff --git a/src/error.rs b/src/error.rs index 1024f82..80121b1 100644 --- a/src/error.rs +++ b/src/error.rs @@ -11,6 +11,7 @@ pub enum Error { ParseGitRev, ParsePreRelease, ParseSemver, + ParseVersion, TryFromInt, } diff --git a/src/lib.rs b/src/lib.rs index 3ab66be..5e6657c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +use std::{fmt, str::FromStr}; + mod error; mod extended; mod gitrev; @@ -22,6 +24,38 @@ pub enum Version { GitRev(GitRev), } +impl fmt::Display for Version { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Simple(s) => write!(f, "{s}"), + Self::Rapid(r) => write!(f, "{r}"), + Self::SemVer(s) => write!(f, "{s}"), + Self::Extended(x) => write!(f, "{x}"), + Self::GitRev(g) => write!(f, "{g}"), + } + } +} + +impl FromStr for Version { + type Err = Error; + + fn from_str(s: &str) -> Result { + if let Ok(v) = Simple::from_str(s) { + Ok(Self::Simple(v)) + } else if let Ok(v) = Rapid::from_str(s) { + Ok(Self::Rapid(v)) + } else if let Ok(v) = SemVer::from_str(s) { + Ok(Self::SemVer(v)) + } else if let Ok(v) = Extended::from_str(s) { + Ok(Self::Extended(v)) + } else if let Ok(v) = GitRev::from_str(s) { + Ok(Self::GitRev(v)) + } else { + Err(Error::ParseVersion) + } + } +} + impl From for Version { fn from(value: Simple) -> Self { Self::Simple(value)