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), } }