refactor(nix-compat/nixhash): use a bit more map and ok_or_else
This looks more readable like this. Change-Id: Iaa750fae66c7263612f169405eb7d38fb9541b04 Reviewed-on: https://cl.snix.dev/c/snix/+/30552 Autosubmit: Florian Klink <flokli@flokli.de> Reviewed-by: Ilan Joselevich <personal@ilanjoselevich.com> Tested-by: besadii
This commit is contained in:
parent
963546bd37
commit
7b6b94c5ca
1 changed files with 4 additions and 10 deletions
|
|
@ -210,11 +210,7 @@ pub enum Error {
|
||||||
/// one communicated out-of-band.
|
/// one communicated out-of-band.
|
||||||
pub fn from_str(s: &str, algo_str: Option<&str>) -> NixHashResult<NixHash> {
|
pub fn from_str(s: &str, algo_str: Option<&str>) -> NixHashResult<NixHash> {
|
||||||
// if algo_str is some, parse or bail out
|
// if algo_str is some, parse or bail out
|
||||||
let algo: Option<HashAlgo> = if let Some(algo_str) = algo_str {
|
let algo: Option<HashAlgo> = algo_str.map(HashAlgo::try_from).transpose()?;
|
||||||
Some(algo_str.try_into()?)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
// Peek at the beginning of the string to detect SRI hashes.
|
// Peek at the beginning of the string to detect SRI hashes.
|
||||||
if s.starts_with("sha1-")
|
if s.starts_with("sha1-")
|
||||||
|
|
@ -240,6 +236,7 @@ pub fn from_str(s: &str, algo_str: Option<&str>) -> NixHashResult<NixHash> {
|
||||||
|| s.starts_with("md5:")
|
|| s.starts_with("md5:")
|
||||||
{
|
{
|
||||||
let parsed_nixhash = from_nix_str(s)?;
|
let parsed_nixhash = from_nix_str(s)?;
|
||||||
|
|
||||||
// ensure the algo matches with what has been passed externally, if so.
|
// ensure the algo matches with what has been passed externally, if so.
|
||||||
if let Some(algo) = algo {
|
if let Some(algo) = algo {
|
||||||
if algo != parsed_nixhash.algo() {
|
if algo != parsed_nixhash.algo() {
|
||||||
|
|
@ -250,11 +247,8 @@ pub fn from_str(s: &str, algo_str: Option<&str>) -> NixHashResult<NixHash> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Neither of these, assume a bare digest, so there MUST be an externally-passed algo.
|
// Neither of these, assume a bare digest, so there MUST be an externally-passed algo.
|
||||||
match algo {
|
let algo = algo.ok_or_else(|| Error::MissingInlineHashAlgo(s.to_string()))?;
|
||||||
// Fail if there isn't.
|
decode_digest(s.as_bytes(), algo)
|
||||||
None => Err(Error::MissingInlineHashAlgo(s.to_string())),
|
|
||||||
Some(algo) => decode_digest(s.as_bytes(), algo),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses a Nix hash string ($algo:$digest) to a NixHash.
|
/// Parses a Nix hash string ($algo:$digest) to a NixHash.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue