summaryrefslogtreecommitdiffstats
path: root/src/package/version.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/package/version.rs')
-rw-r--r--src/package/version.rs28
1 files changed, 23 insertions, 5 deletions
diff --git a/src/package/version.rs b/src/package/version.rs
index c27bf92..359c80f 100644
--- a/src/package/version.rs
+++ b/src/package/version.rs
@@ -10,6 +10,7 @@
use std::ops::Deref;
+use anyhow::Context;
use anyhow::Error;
use anyhow::Result;
use pom::parser::Parser as PomParser;
@@ -25,11 +26,7 @@ pub struct PackageVersionConstraint {
}
impl PackageVersionConstraint {
- pub fn new(s: String) -> Result<Self> {
- Self::parser().parse(s.as_bytes()).map_err(Error::from)
- }
-
- pub fn parser<'a>() -> PomParser<'a, u8, Self> {
+ fn parser<'a>() -> PomParser<'a, u8, Self> {
(pom::parser::sym(b'=') + PackageVersion::parser())
.convert(|(constraint, version)| {
String::from_utf8(vec![constraint]).map(|c| (c, version))
@@ -53,6 +50,27 @@ impl PackageVersionConstraint {
}
}
+impl std::convert::TryFrom<String> for PackageVersionConstraint {
+ type Error = anyhow::Error;
+
+ fn try_from(s: String) -> Result<Self> {
+ Self::try_from(&s as &str)
+ }
+}
+
+impl std::convert::TryFrom<&str> for PackageVersionConstraint {
+ type Error = anyhow::Error;
+
+ fn try_from(s: &str) -> Result<Self> {
+ PackageVersionConstraint::parser()
+ .parse(s.as_bytes())
+ .context("Failed to parse package version constraint")
+ .context("A package version constraint must have a comparator and a version string, like so: =0.1.0")
+ .map_err(Error::from)
+
+ }
+}
+
#[derive(
parse_display::Display,
Serialize,