From b9621938865d4ad24d7bcee7fdbfa0c9c45978c8 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 1 Jan 2024 16:06:10 -0500 Subject: clippy and fmt --- src/api.rs | 8 +++--- src/bin/rbw-agent/notifications.rs | 4 +-- src/bin/rbw-agent/timeout.rs | 2 +- src/bin/rbw/commands.rs | 59 +++++++++++++++++++------------------- src/config.rs | 16 +++++------ src/edit.rs | 4 +-- src/error.rs | 4 +-- 7 files changed, 48 insertions(+), 49 deletions(-) diff --git a/src/api.rs b/src/api.rs index e770013..ca3c43b 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1046,10 +1046,10 @@ impl Client { ) -> Result<()> { let mut req = CiphersPutReq { ty: match data { - crate::db::EntryData::Login {..} => 1, - crate::db::EntryData::SecureNote {..} => 2, - crate::db::EntryData::Card {..} => 3, - crate::db::EntryData::Identity {..} => 4, + crate::db::EntryData::Login { .. } => 1, + crate::db::EntryData::SecureNote { .. } => 2, + crate::db::EntryData::Card { .. } => 3, + crate::db::EntryData::Identity { .. } => 4, }, folder_id: folder_uuid.map(std::string::ToString::to_string), organization_id: org_id.map(std::string::ToString::to_string), diff --git a/src/bin/rbw-agent/notifications.rs b/src/bin/rbw-agent/notifications.rs index 297fa9c..8176603 100644 --- a/src/bin/rbw-agent/notifications.rs +++ b/src/bin/rbw-agent/notifications.rs @@ -148,7 +148,7 @@ fn parse_message( rmpv::decode::read_value(&mut &data[len_buffer_length..]).ok()?; let unpacked_message = unpacked_messagepack.as_array()?; - let message_type = unpacked_message.get(0)?.as_u64()?; + let message_type = unpacked_message.first()?.as_u64()?; // invocation if message_type != 1 { return None; @@ -159,7 +159,7 @@ fn parse_message( } let args = unpacked_message.get(4)?.as_array()?; - let map = args.get(0)?.as_map()?; + let map = args.first()?.as_map()?; for (k, v) in map { if k.as_str()? == "Type" { let ty = v.as_i64()?; diff --git a/src/bin/rbw-agent/timeout.rs b/src/bin/rbw-agent/timeout.rs index e613ff0..e2aba06 100644 --- a/src/bin/rbw-agent/timeout.rs +++ b/src/bin/rbw-agent/timeout.rs @@ -40,7 +40,7 @@ impl Timeout { futures_util::stream::once(tokio::time::sleep( dur, )) - .map(|_| Event::Timer) + .map(|()| Event::Timer) .boxed(), ); } diff --git a/src/bin/rbw/commands.rs b/src/bin/rbw/commands.rs index aa7f5b9..2a4dd12 100644 --- a/src/bin/rbw/commands.rs +++ b/src/bin/rbw/commands.rs @@ -671,16 +671,16 @@ impl std::convert::TryFrom<&String> for ListField { } } -const HELP_PW: &str = r#" +const HELP_PW: &str = r" # The first line of this file will be the password, and the remainder of the # file (after any blank lines after the password) will be stored as a note. # Lines with leading # will be ignored. -"#; +"; -const HELP_NOTES: &str = r#" +const HELP_NOTES: &str = r" # The content of this file will be stored as a note. # Lines with leading # will be ignored. -"#; +"; pub fn config_show() -> anyhow::Result<()> { let config = rbw::config::Config::load()?; @@ -819,8 +819,7 @@ pub fn list(fields: &[String]) -> anyhow::Result<()> { let mut ciphers: Vec = db .entries .iter() - .cloned() - .map(|entry| decrypt_cipher(&entry)) + .map(decrypt_cipher) .collect::>()?; ciphers.sort_unstable_by(|a, b| a.name.cmp(&b.name)); @@ -1193,14 +1192,13 @@ pub fn edit( }; (data, notes, history) } - DecryptedData::SecureNote {} => - { + DecryptedData::SecureNote {} => { let data = rbw::db::EntryData::SecureNote {}; - let editor_content = match decrypted.notes { - Some(notes) => format!("{notes}\n"), - None => format!("\n"), - }; + let editor_content = decrypted.notes.map_or_else( + || "\n".to_string(), + |notes| format!("{notes}\n"), + ); let contents = rbw::edit::edit(&editor_content, HELP_NOTES)?; // prepend blank line to be parsed as pw by `parse_editor` @@ -1415,10 +1413,10 @@ fn find_entry_raw( ) -> anyhow::Result<(rbw::db::Entry, DecryptedCipher)> { let mut matches: Vec<(rbw::db::Entry, DecryptedCipher)> = entries .iter() - .cloned() - .filter(|(_, decrypted_cipher)| { + .filter(|&(_, decrypted_cipher)| { decrypted_cipher.exact_match(name, username, folder, true) }) + .cloned() .collect(); if matches.len() == 1 { @@ -1428,10 +1426,10 @@ fn find_entry_raw( if folder.is_none() { matches = entries .iter() - .cloned() - .filter(|(_, decrypted_cipher)| { + .filter(|&(_, decrypted_cipher)| { decrypted_cipher.exact_match(name, username, folder, false) }) + .cloned() .collect(); if matches.len() == 1 { @@ -1441,10 +1439,10 @@ fn find_entry_raw( matches = entries .iter() - .cloned() - .filter(|(_, decrypted_cipher)| { + .filter(|&(_, decrypted_cipher)| { decrypted_cipher.partial_match(name, username, folder, true) }) + .cloned() .collect(); if matches.len() == 1 { @@ -1454,10 +1452,10 @@ fn find_entry_raw( if folder.is_none() { matches = entries .iter() - .cloned() - .filter(|(_, decrypted_cipher)| { + .filter(|&(_, decrypted_cipher)| { decrypted_cipher.partial_match(name, username, folder, false) }) + .cloned() .collect(); if matches.len() == 1 { return Ok(matches[0].clone()); @@ -1763,8 +1761,11 @@ fn parse_editor(contents: &str) -> (Option, Option) { let mut notes: String = lines .skip_while(|line| line.is_empty()) .filter(|line| !line.starts_with('#')) - .map(|line| format!("{line}\n")) - .collect(); + .fold(String::new(), |mut notes, line| { + notes.push_str(line); + notes.push('\n'); + notes + }); while notes.ends_with('\n') { notes.pop(); } @@ -1848,6 +1849,13 @@ fn generate_totp(secret: &str) -> anyhow::Result { )) } +fn display_field(name: &str, field: Option<&str>, clipboard: bool) -> bool { + field.map_or_else( + || false, + |field| val_display_or_store(clipboard, &format!("{name}: {field}")), + ) +} + #[cfg(test)] mod test { use super::*; @@ -2011,10 +2019,3 @@ mod test { ) } } - -fn display_field(name: &str, field: Option<&str>, clipboard: bool) -> bool { - field.map_or_else( - || false, - |field| val_display_or_store(clipboard, &format!("{name}: {field}")), - ) -} diff --git a/src/config.rs b/src/config.rs index 557bfc0..efb1b5f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -147,11 +147,11 @@ impl Config { self.base_url.clone().map_or_else( || "https://api.bitwarden.com".to_string(), |url| { - let clean_url = format!("{}", url.trim_end_matches('/')); + let clean_url = url.trim_end_matches('/').to_string(); if clean_url == "https://api.bitwarden.eu" { clean_url } else { - format!("{}/api", clean_url) + format!("{clean_url}/api") } }, ) @@ -163,13 +163,13 @@ impl Config { self.base_url.clone().map_or_else( || "https://identity.bitwarden.com".to_string(), |url| { - let clean_url = format!("{}", url.trim_end_matches('/')); + let clean_url = url.trim_end_matches('/').to_string(); if clean_url == "https://identity.bitwarden.eu" { clean_url } else { - format!("{}/identity", clean_url) + format!("{clean_url}/identity") } - } + }, ) }) } @@ -180,13 +180,13 @@ impl Config { self.base_url.clone().map_or_else( || "https://notifications.bitwarden.com".to_string(), |url| { - let clean_url = format!("{}", url.trim_end_matches('/')); + let clean_url = url.trim_end_matches('/').to_string(); if clean_url == "https://notifications.bitwarden.eu" { clean_url } else { - format!("{}/notifications", clean_url) + format!("{clean_url}/notifications") } - } + }, ) }) } diff --git a/src/edit.rs b/src/edit.rs index cb59366..4862c0b 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -3,10 +3,10 @@ use crate::prelude::*; use std::io::{Read as _, Write as _}; pub fn edit(contents: &str, help: &str) -> Result { - if ! atty::is(atty::Stream::Stdin) { + if !atty::is(atty::Stream::Stdin) { // directly read from piped content return match std::io::read_to_string(std::io::stdin()) { - Err(e) => Err(Error::FailedToReadFromStdin{ err: e }), + Err(e) => Err(Error::FailedToReadFromStdin { err: e }), Ok(res) => Ok(res), }; } diff --git a/src/error.rs b/src/error.rs index 1ffbcdd..8a3b5e2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -22,9 +22,7 @@ pub enum Error { FailedToParsePinentry { out: String }, #[error("failed to read from stdin: {err}")] - FailedToReadFromStdin { - err: std::io::Error, - }, + FailedToReadFromStdin { err: std::io::Error }, #[error( "failed to run editor {}: {err}", -- cgit v1.2.3-54-g00ecf