impl fmt::Display and FromStr for Version

This commit is contained in:
Nathan Fisher 2024-01-08 19:11:37 -05:00
parent 748a786632
commit 935f10cb9d
2 changed files with 35 additions and 0 deletions

View File

@ -11,6 +11,7 @@ pub enum Error {
ParseGitRev,
ParsePreRelease,
ParseSemver,
ParseVersion,
TryFromInt,
}

View File

@ -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<Self, Self::Err> {
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<Simple> for Version {
fn from(value: Simple) -> Self {
Self::Simple(value)