impl fmt::Display and FromStr for Version
This commit is contained in:
parent
748a786632
commit
935f10cb9d
@ -11,6 +11,7 @@ pub enum Error {
|
|||||||
ParseGitRev,
|
ParseGitRev,
|
||||||
ParsePreRelease,
|
ParsePreRelease,
|
||||||
ParseSemver,
|
ParseSemver,
|
||||||
|
ParseVersion,
|
||||||
TryFromInt,
|
TryFromInt,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
34
src/lib.rs
34
src/lib.rs
@ -1,3 +1,5 @@
|
|||||||
|
use std::{fmt, str::FromStr};
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
mod extended;
|
mod extended;
|
||||||
mod gitrev;
|
mod gitrev;
|
||||||
@ -22,6 +24,38 @@ pub enum Version {
|
|||||||
GitRev(GitRev),
|
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 {
|
impl From<Simple> for Version {
|
||||||
fn from(value: Simple) -> Self {
|
fn from(value: Simple) -> Self {
|
||||||
Self::Simple(value)
|
Self::Simple(value)
|
||||||
|
Loading…
Reference in New Issue
Block a user