Maybe you can flat using dot-free style function calls too.
However, it unnecessarly add runtime overhead, pollute the callstack, and even not intuitive as direct-style control flows are.
lazy_static! { | |
static ref RE: Regex = Regex::new(r"^(?P<alg>[A-Za-z0-9_+.-]+):(?P<hash>[A-Fa-f0-9]+)$").unwrap(); | |
} | |
fn from_str_v1(s: &str) -> Result<Self, Self::Err> { | |
RE.captures(s) | |
.and_then(|cap| match (cap.name("alg"), cap.name("hash")) { | |
(Some(alg), Some(hash)) => match SupportedAlgorithm::from_str(alg.as_str()) { | |
Ok(alg) => Some(Self { | |
alg, | |
hash: hash.as_str().to_string(), | |
}), | |
_ => None, | |
}, | |
_ => None, | |
}) | |
.ok_or_else(|| RegistryError::DigestInvalid) | |
} | |
fn from_str_v2(s: &str) -> Result<Self, Self::Err> { | |
let Ok(cap) = RE.captures(s) else { | |
return Err(RegistryError::DigestInvalid) | |
} | |
let (Some(alg), Some(hash)) = (cap.name("alg"), cap.name("hash")) else { | |
return Err(RegistryError::DigestInvalid) | |
} | |
let Ok(alg) = SupportedAlgorithm::from_str(alg.as_str()) else { | |
return Err(RegistryError::DigestInvalid) | |
} | |
Ok(Self { | |
alg, | |
hash: hash.as_str().to_string(), | |
}) | |
} |