From cce48864a2338a11cf7b9585350a327f70b8376b Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Sun, 19 Jan 2025 21:07:40 -0600 Subject: update strsub --- src/version/manifest.rs | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'src/version/manifest.rs') diff --git a/src/version/manifest.rs b/src/version/manifest.rs index c8c49b8..18653f3 100644 --- a/src/version/manifest.rs +++ b/src/version/manifest.rs @@ -1,5 +1,6 @@ use core::fmt; - +use std::convert::Infallible; +use std::str::FromStr; use chrono::{DateTime, Utc}; use serde::{de::Visitor, Deserialize}; use sha1_smol::Digest; @@ -19,6 +20,32 @@ pub enum VersionType { Other(String) } +impl FromStr for VersionType { + type Err = Infallible; + + fn from_str(s: &str) -> Result { + match s { + "snapshot" => Ok(Self::Snapshot), + "release" => Ok(Self::Release), + "old_beta" => Ok(Self::OldBeta), + "old_alpha" => Ok(Self::OldAlpha), + _ => Ok(Self::Other(s.to_owned())) + } + } +} + +impl VersionType { + pub fn to_str(&self) -> &str { + match self { + Self::Snapshot => "snapshot", + Self::Release => "release", + Self::OldBeta => "old_beta", + Self::OldAlpha => "old_alpha", + Self::Other(s) => s + } + } +} + struct VersionTypeVisitor; impl<'de> Visitor<'de> for VersionTypeVisitor { @@ -31,13 +58,7 @@ impl<'de> Visitor<'de> for VersionTypeVisitor { fn visit_str(self, v: &str) -> Result where E: serde::de::Error, { - match v { - "snapshot" => Ok(VersionType::Snapshot), - "release" => Ok(VersionType::Release), - "old_beta" => Ok(VersionType::OldBeta), - "old_alpha" => Ok(VersionType::OldAlpha), - _ => Ok(VersionType::Other(v.to_owned())) - } + Ok(VersionType::from_str(v).unwrap(/* infallible */)) } } -- cgit v1.2.3-70-g09d2