diff options
author | Neal H. Walfield <neal@pep.foundation> | 2023-05-12 12:03:07 +0200 |
---|---|---|
committer | Neal H. Walfield <neal@pep.foundation> | 2023-05-12 13:28:16 +0200 |
commit | f910aa90dfa15e5d3b842047dee89cd0a14379b4 (patch) | |
tree | d01cf0af5b96c88a4e31e9c95b2eb03cb26d3a5e | |
parent | f1305df5022704e9cc60fd13f3bf724f8eeb7745 (diff) |
autocrypt: Refactor to avoid unnecessary unwraps and panics.
- The sole caller of `decode_autocrypt_like_header` already has the
data in the form that `decode_autocrypt_like_header` wants. Pass it
as is. This avoids double parsing and, since we now use the
expected types, `unwrap`s and a `panic`.
-rw-r--r-- | .cargo/config.toml | 1 | ||||
-rw-r--r-- | autocrypt/src/lib.rs | 30 |
2 files changed, 18 insertions, 13 deletions
diff --git a/.cargo/config.toml b/.cargo/config.toml index 3e57a8d6..ef32e648 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -9,4 +9,5 @@ rustflags = [ "-Aclippy::redundant-clone", "-Aclippy::try-err", "-Aclippy::unnecessary-lazy-evaluations", + "-Aclippy::manual-map", ] diff --git a/autocrypt/src/lib.rs b/autocrypt/src/lib.rs index 6b4e647f..f8d23420 100644 --- a/autocrypt/src/lib.rs +++ b/autocrypt/src/lib.rs @@ -267,8 +267,19 @@ impl AutocryptHeaders { if let Some(rest) = line.strip_prefix(FROM) { headers.from = Some(rest.trim_matches(' ').into()); - } else if line.starts_with(AUTOCRYPT) || line.starts_with(AUTOCRYPT_GOSSIP) { - headers.headers.push(Self::decode_autocrypt_like_header(&line)); + } else { + if let Some((key, value)) = + if let Some(v) = line.strip_prefix(AUTOCRYPT) { + Some((AutocryptHeaderType::Sender, v)) + } else if let Some(v) = line.strip_prefix(AUTOCRYPT_GOSSIP) { + Some((AutocryptHeaderType::Gossip, v)) + } else { + None + } + { + headers.headers.push( + Self::decode_autocrypt_like_header(key, value)); + } } } @@ -278,17 +289,10 @@ impl AutocryptHeaders { /// Decode header that has the same format as the Autocrypt header. /// This function should be called only on "Autocrypt" or "Autocrypt-Gossip" /// headers. - fn decode_autocrypt_like_header(line: &str) -> AutocryptHeader { - let mut parts = line.splitn(2, ": "); - let header_name = parts.next().unwrap(); - let ac_value = parts.next().unwrap(); - - let header_type = match header_name { - "Autocrypt" => AutocryptHeaderType::Sender, - "Autocrypt-Gossip" => AutocryptHeaderType::Gossip, - other => panic!("Expected Autocrypt header but found: {}", other) - }; - + fn decode_autocrypt_like_header(header_type: AutocryptHeaderType, + ac_value: &str) + -> AutocryptHeader + { let mut header = AutocryptHeader::empty(header_type); for pair in ac_value.split(';') { |