chiark / gitweb /
materials format: Provide try_from_integer
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 4 May 2022 21:55:50 +0000 (22:55 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 4 May 2022 22:00:10 +0000 (23:00 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/materials-format.rs

index 9bdf634c7f8183f127cd9408101223d511ba0261..223f0ad677e1af016b52cd3d229842a8a1bf7e15 100644 (file)
@@ -49,6 +49,15 @@ impl TryFrom<Raw> for Version {
   }
 }
 
+impl Version {
+  #[throws(VersionError)]
+  pub fn try_from_integer<I>(i: I) -> Version
+  where I: num::Integer + ToPrimitive {
+    let v = i.to_u32().ok_or_else(|| MFVE::Other("integer out of range"))?;
+    v.try_into()?
+  }
+}
+
 impl FromStr for Version {
   type Err = VersionError;
   #[throws(VersionError)]