Simplify parsing NonZeroU16

This commit is contained in:
Nathan Fisher 2024-01-05 09:59:32 -05:00
parent db5805b8db
commit bb12ee81f5

View File

@ -30,56 +30,57 @@ impl str::FromStr for PreRelease {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
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::<u16>() {
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::<u16>() {
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::<u16>() {
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::<u16>() {
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::<u16>() {
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::<u16>() {
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::<u16>() {
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),
}
}