From bb12ee81f56033c7de58e62ebc9e960857b56f89 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Fri, 5 Jan 2024 09:59:32 -0500 Subject: [PATCH] Simplify parsing NonZeroU16 --- src/prerelease.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/prerelease.rs b/src/prerelease.rs index 085e877..20e5b24 100644 --- a/src/prerelease.rs +++ b/src/prerelease.rs @@ -30,56 +30,57 @@ impl str::FromStr for PreRelease { type Err = Error; fn from_str(s: &str) -> Result { + let max = unsafe { NonZeroU16::new_unchecked(1025) }; match s { "Alpha" | "alpha" => Ok(Self::Alpha(None)), "Beta" | "beta" => Ok(Self::Beta(None)), "RC" | "Rc" | "rc" => Ok(Self::RC(None)), s if s.starts_with("Alpha") => { let suf = s.strip_prefix("Alpha").unwrap(); - match suf.parse::() { - Ok(v) if v > 0 && v < 1025 => Ok(Self::Alpha(Some(v.try_into()?))), + match suf.parse() { + Ok(v) if v < max => Ok(Self::Alpha(Some(v))), _ => Err(Error::ParsePreRelease), } } s if s.starts_with("alpha") => { let suf = s.strip_prefix("alpha").unwrap(); - match suf.parse::() { - Ok(v) if v > 0 && v < 1025 => Ok(Self::Alpha(Some(v.try_into()?))), + match suf.parse() { + Ok(v) if v < max => Ok(Self::Alpha(Some(v))), _ => Err(Error::ParsePreRelease), } } s if s.starts_with("Beta") => { let suf = s.strip_prefix("Beta").unwrap(); - match suf.parse::() { - Ok(v) if v > 0 && v < 1025 => Ok(Self::Beta(Some(v.try_into()?))), + match suf.parse() { + Ok(v) if v < max => Ok(Self::Beta(Some(v))), _ => Err(Error::ParsePreRelease), } } s if s.starts_with("beta") => { let suf = s.strip_prefix("beta").unwrap(); - match suf.parse::() { - Ok(v) if v > 0 && v < 1025 => Ok(Self::Beta(Some(v.try_into()?))), + match suf.parse() { + Ok(v) if v < max => Ok(Self::Beta(Some(v))), _ => Err(Error::ParsePreRelease), } } s if s.starts_with("RC") => { let suf = s.strip_prefix("RC").unwrap(); - match suf.parse::() { - Ok(v) if v > 0 && v < 1025 => Ok(Self::RC(Some(v.try_into()?))), + match suf.parse() { + Ok(v) if v < max => Ok(Self::RC(Some(v))), _ => Err(Error::ParsePreRelease), } } s if s.starts_with("Rc") => { let suf = s.strip_prefix("Rc").unwrap(); - match suf.parse::() { - Ok(v) if v > 0 && v < 1025 => Ok(Self::RC(Some(v.try_into()?))), + match suf.parse() { + Ok(v) if v < max => Ok(Self::RC(Some(v))), _ => Err(Error::ParsePreRelease), } } s if s.starts_with("rc") => { let suf = s.strip_prefix("rc").unwrap(); - match suf.parse::() { - Ok(v) if v > 0 && v < 1025 => Ok(Self::RC(Some(v.try_into()?))), + match suf.parse() { + Ok(v) if v < max => Ok(Self::RC(Some(v))), _ => Err(Error::ParsePreRelease), } }