Skip to content

Commit

Permalink
clippy and fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
doy committed Jan 1, 2024
1 parent 21e70ce commit b962193
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 49 deletions.
8 changes: 4 additions & 4 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions src/bin/rbw-agent/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()?;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/rbw-agent/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Timeout {
futures_util::stream::once(tokio::time::sleep(
dur,
))
.map(|_| Event::Timer)
.map(|()| Event::Timer)
.boxed(),
);
}
Expand Down
59 changes: 30 additions & 29 deletions src/bin/rbw/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down Expand Up @@ -819,8 +819,7 @@ pub fn list(fields: &[String]) -> anyhow::Result<()> {
let mut ciphers: Vec<DecryptedCipher> = db
.entries
.iter()
.cloned()
.map(|entry| decrypt_cipher(&entry))
.map(decrypt_cipher)
.collect::<anyhow::Result<_>>()?;
ciphers.sort_unstable_by(|a, b| a.name.cmp(&b.name));

Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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());
Expand Down Expand Up @@ -1763,8 +1761,11 @@ fn parse_editor(contents: &str) -> (Option<String>, Option<String>) {
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();
}
Expand Down Expand Up @@ -1848,6 +1849,13 @@ fn generate_totp(secret: &str) -> anyhow::Result<String> {
))
}

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::*;
Expand Down Expand Up @@ -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}")),
)
}
16 changes: 8 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
},
)
Expand All @@ -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")
}
}
},
)
})
}
Expand All @@ -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")
}
}
},
)
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use crate::prelude::*;
use std::io::{Read as _, Write as _};

pub fn edit(contents: &str, help: &str) -> Result<String> {
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),
};
}
Expand Down
4 changes: 1 addition & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down

0 comments on commit b962193

Please sign in to comment.