From 3436e616f8d59f87012781e4e9d01d067c7e77ad Mon Sep 17 00:00:00 2001 From: Awiteb Date: Mon, 6 May 2024 23:29:48 +0300 Subject: [PATCH] chore: Reformat the codebase with the new configration --- src/cli/add_command.rs | 6 ++++-- src/cli/edit_command.rs | 26 +++++++++++++++----------- src/cli/export_command.rs | 10 ++++++---- src/cli/gen_command.rs | 4 ++-- src/cli/import_command.rs | 7 +++++-- src/cli/list_command.rs | 4 ++-- src/cli/mod.rs | 5 +++-- src/cli/remove_command.rs | 3 ++- src/errors.rs | 5 ++++- src/macros.rs | 5 +---- src/main.rs | 5 +++-- src/utils.rs | 7 +++---- src/vault/bitwarden.rs | 28 ++++++++++++++-------------- src/vault/cipher.rs | 3 ++- src/vault/mod.rs | 22 ++++++++++++---------- 15 files changed, 78 insertions(+), 62 deletions(-) diff --git a/src/cli/add_command.rs b/src/cli/add_command.rs index 579c753..02200b5 100644 --- a/src/cli/add_command.rs +++ b/src/cli/add_command.rs @@ -19,7 +19,9 @@ use inquire::{Password, PasswordDisplayMode}; use crate::{ vault::{Vault, Vaults}, - LprsCommand, LprsError, LprsResult, + LprsCommand, + LprsError, + LprsResult, }; #[derive(Debug, Args)] @@ -32,7 +34,7 @@ pub struct Add { #[arg(short, long)] // FIXME: I think replacing `Option>` with custom type will be better #[allow(clippy::option_option)] - password: Option>, + password: Option>, } impl LprsCommand for Add { diff --git a/src/cli/edit_command.rs b/src/cli/edit_command.rs index fbed9a6..33e0d6b 100644 --- a/src/cli/edit_command.rs +++ b/src/cli/edit_command.rs @@ -21,7 +21,9 @@ use inquire::{Password, PasswordDisplayMode}; use crate::{ vault::{Vault, Vaults}, - LprsCommand, LprsError, LprsResult, + LprsCommand, + LprsError, + LprsResult, }; #[derive(Debug, Args)] @@ -33,7 +35,7 @@ pub struct Edit { #[arg(short, long)] /// The new vault name - name: Option, + name: Option, #[arg(short, long)] /// The new vault username username: Option, @@ -44,10 +46,10 @@ pub struct Edit { password: Option>, #[arg(short, long)] /// The new vault service - service: Option, + service: Option, #[arg(short = 'o', long)] /// The new vault note - note: Option, + note: Option, } impl LprsCommand for Edit { @@ -66,13 +68,15 @@ impl LprsCommand for Edit { // Get the password from stdin or from its value if provided let password = match self.password { Some(Some(password)) => Some(password), - Some(None) => Some( - Password::new("New vault password:") - .without_confirmation() - .with_formatter(&|p| "*".repeat(p.chars().count())) - .with_display_mode(PasswordDisplayMode::Masked) - .prompt()?, - ), + Some(None) => { + Some( + Password::new("New vault password:") + .without_confirmation() + .with_formatter(&|p| "*".repeat(p.chars().count())) + .with_display_mode(PasswordDisplayMode::Masked) + .prompt()?, + ) + } None => None, }; diff --git a/src/cli/export_command.rs b/src/cli/export_command.rs index 304ad1c..d488a97 100644 --- a/src/cli/export_command.rs +++ b/src/cli/export_command.rs @@ -20,16 +20,18 @@ use clap::Args; use crate::{ vault::{BitWardenPasswords, Format, Vaults}, - LprsCommand, LprsError, LprsResult, + LprsCommand, + LprsError, + LprsResult, }; #[derive(Debug, Args)] #[command(author, version, about, long_about = None)] -/// Export command, used to export the vaults in `lprs` format or `BitWarden` format. -/// The exported file will be a json file. +/// Export command, used to export the vaults in `lprs` format or `BitWarden` +/// format. The exported file will be a json file. pub struct Export { /// The path to export to - path: PathBuf, + path: PathBuf, /// Format to export vaults in #[arg(short, long, value_name = "FORMAT", default_value_t= Format::Lprs)] format: Format, diff --git a/src/cli/gen_command.rs b/src/cli/gen_command.rs index 2e4ad05..10f423c 100644 --- a/src/cli/gen_command.rs +++ b/src/cli/gen_command.rs @@ -36,10 +36,10 @@ pub struct Gen { lowercase: bool, /// With numbers (0-9) #[arg(short, long)] - numbers: bool, + numbers: bool, /// With symbols (!,# ...) #[arg(short, long)] - symbols: bool, + symbols: bool, } impl LprsCommand for Gen { diff --git a/src/cli/import_command.rs b/src/cli/import_command.rs index 7b5e993..700a8a1 100644 --- a/src/cli/import_command.rs +++ b/src/cli/import_command.rs @@ -25,12 +25,15 @@ use clap::Args; use crate::{ vault::{BitWardenPasswords, Format, Vault, Vaults}, - LprsCommand, LprsError, LprsResult, + LprsCommand, + LprsError, + LprsResult, }; #[derive(Debug, Args)] #[command(author, version, about, long_about = None)] -/// Import command, used to import vaults from the exported files, `lprs` or `BitWarden` +/// Import command, used to import vaults from the exported files, `lprs` or +/// `BitWarden` pub struct Import { /// The file path to import from path: PathBuf, diff --git a/src/cli/list_command.rs b/src/cli/list_command.rs index 7edc27f..2276850 100644 --- a/src/cli/list_command.rs +++ b/src/cli/list_command.rs @@ -27,13 +27,13 @@ use crate::{vault::Vaults, LprsCommand, LprsError, LprsResult}; pub struct List { /// Return the password with spesifc index #[arg(short, long, value_name = "INDEX")] - get: Option, + get: Option, /// Filter the select list #[arg(short, long, value_name = "TEXT")] filter: Option, /// Enable regex when use `--filter` option #[arg(short, long)] - regex: bool, + regex: bool, } impl LprsCommand for List { diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 4db418c..a9f055a 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -31,7 +31,8 @@ pub mod edit_command; pub mod export_command; /// Generate command, used to generate a password pub mod gen_command; -/// Import command, used to import vaults from the exported files, `lprs` or `BitWarden` +/// Import command, used to import vaults from the exported files, `lprs` or +/// `BitWarden` pub mod import_command; /// List command, used to list the vaults and search pub mod list_command; @@ -72,7 +73,7 @@ pub struct Cli { pub vaults_file: Option, /// Show the logs in the stdout #[arg(short, long)] - pub verbose: bool, + pub verbose: bool, #[command(subcommand)] /// The provided command to run diff --git a/src/cli/remove_command.rs b/src/cli/remove_command.rs index c563ae2..46e1ae3 100644 --- a/src/cli/remove_command.rs +++ b/src/cli/remove_command.rs @@ -27,7 +27,8 @@ pub struct Remove { /// The password index index: NonZeroU64, - /// Force remove, will not return error if there is no password with this index + /// Force remove, will not return error if there is no password with this + /// index #[arg(short, long)] force: bool, } diff --git a/src/errors.rs b/src/errors.rs index 774b8a4..e425f56 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -24,7 +24,10 @@ pub type Result = result::Result; pub enum Error { #[error("Encryption Error: {0}")] Encryption(String), - #[error("Decryption Error: The given key cannot decrypt the given data. Either the data has been tampered with or the key is incorrect.")] + #[error( + "Decryption Error: The given key cannot decrypt the given data. Either the data has been \ + tampered with or the key is incorrect." + )] Decryption, #[error("Wrong Master Password Error: Wrong decryption password")] WrongMasterPassword, diff --git a/src/macros.rs b/src/macros.rs index 624ccb7..3bf60f4 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -29,10 +29,7 @@ /// #### Output /// ```rust /// impl crate::LprsCommand for TestCommands { -/// fn run( -/// &self, -/// vault_manager: crate::vault::Vaults, -/// ) -> crate::LprsResult<()> { +/// fn run(&self, vault_manager: crate::vault::Vaults) -> crate::LprsResult<()> { /// match self { /// Self::Test(command) => command.run(vault_manager), /// Self::Some(command) => command.run(vault_manager), diff --git a/src/main.rs b/src/main.rs index dc1d274..ba59a86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,11 +15,12 @@ // along with this program. If not, see . #![doc = include_str!("../README.md")] -use clap::Parser; -use inquire::InquireError; use std::env; use std::process::ExitCode; +use clap::Parser; +use inquire::InquireError; + /// The main module of the lprs crate, contains the cli and the commands. pub mod cli; /// The errors module, contains the errors and the result type. diff --git a/src/utils.rs b/src/utils.rs index 57a7bdc..860d797 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -18,10 +18,9 @@ use std::{fs, path::PathBuf}; use inquire::{validator::Validation, PasswordDisplayMode}; use passwords::{analyzer, scorer}; -use sha2::Digest; - #[cfg(feature = "update-notify")] use reqwest::blocking::Client as BlockingClient; +use sha2::Digest; use crate::{LprsError, LprsResult}; @@ -64,8 +63,8 @@ pub fn vaults_file() -> LprsResult { /// - Its score must be greater than 80.0 /// /// ## Errors -/// - There is no errors, just the return type of inquire validator -/// must be Result +/// - There is no errors, just the return type of inquire validator must be +/// Result pub fn password_validator(password: &str) -> Result { let analyzed = analyzer::analyze(password); Ok(if analyzed.length() < 15 { diff --git a/src/vault/bitwarden.rs b/src/vault/bitwarden.rs index e898e0e..c91fc15 100644 --- a/src/vault/bitwarden.rs +++ b/src/vault/bitwarden.rs @@ -14,8 +14,8 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -// This file is not important, it is just a struct that is used to serialize and deserialize the vaults -// from and to the BitWarden format. +// This file is not important, it is just a struct that is used to serialize and +// deserialize the vaults from and to the BitWarden format. #![allow(missing_docs)] use serde::{Deserialize, Serialize}; @@ -26,27 +26,27 @@ use super::{Vault, Vaults}; pub struct BitWardenLoginData { pub username: Option, pub password: Option, - pub uris: Option>, + pub uris: Option>, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct BitWardenUri { #[serde(rename = "match")] - pub mt: Option, + pub mt: Option, pub uri: String, } #[derive(Default, Deserialize, Serialize)] pub struct BitWardenFolder { - pub id: String, + pub id: String, pub name: String, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct BitWardenPassword { #[serde(rename = "type")] - pub ty: i32, - pub name: String, + pub ty: i32, + pub name: String, pub login: Option, pub notes: Option, } @@ -55,8 +55,8 @@ pub struct BitWardenPassword { #[derive(Default, Deserialize, Serialize)] pub struct BitWardenPasswords { pub encrypted: bool, - pub folders: Vec, - pub items: Vec, + pub folders: Vec, + pub items: Vec, } impl From for Vault { @@ -78,12 +78,12 @@ impl From for Vault { impl From for BitWardenPassword { fn from(value: Vault) -> Self { Self { - ty: 1, - name: value.name, + ty: 1, + name: value.name, login: Some(BitWardenLoginData { username: value.username, password: value.password, - uris: value + uris: value .service .map(|s| vec![BitWardenUri { mt: None, uri: s }]), }), @@ -96,8 +96,8 @@ impl From for BitWardenPasswords { fn from(value: Vaults) -> Self { Self { encrypted: false, - folders: Vec::new(), - items: value + folders: Vec::new(), + items: value .vaults .into_iter() .map(BitWardenPassword::from) diff --git a/src/vault/cipher.rs b/src/vault/cipher.rs index e71ea75..0bf3819 100644 --- a/src/vault/cipher.rs +++ b/src/vault/cipher.rs @@ -14,9 +14,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +use std::time::{SystemTime, UNIX_EPOCH}; + use aes::cipher::{block_padding::Pkcs7, BlockDecryptMut, BlockEncryptMut, KeyIvInit}; use rand::{rngs::StdRng, Rng, SeedableRng}; -use std::time::{SystemTime, UNIX_EPOCH}; use crate::{LprsError, LprsResult}; diff --git a/src/vault/mod.rs b/src/vault/mod.rs index 38deef9..4bded17 100644 --- a/src/vault/mod.rs +++ b/src/vault/mod.rs @@ -33,9 +33,11 @@ pub use bitwarden::*; /// The vaults format pub enum Format { /// The lprs format, which is the default format - /// and is is the result of the serialization/deserialization of the Vaults struct + /// and is is the result of the serialization/deserialization of the Vaults + /// struct Lprs, - /// The BitWarden format, which is the result of the serialization/deserialization of the BitWardenPasswords struct + /// The BitWarden format, which is the result of the + /// serialization/deserialization of the BitWardenPasswords struct BitWarden, } @@ -44,7 +46,7 @@ pub enum Format { pub struct Vault { /// The name of the vault #[arg(short, long)] - pub name: String, + pub name: String, /// The username #[arg(short, long)] pub username: Option, @@ -53,10 +55,10 @@ pub struct Vault { pub password: Option, /// The service name. e.g the website url #[arg(short, long)] - pub service: Option, + pub service: Option, /// Add a note to the vault #[arg(short = 'o', long)] - pub note: Option, + pub note: Option, } /// The vaults manager @@ -65,9 +67,9 @@ pub struct Vaults { /// Hash of the master password pub master_password: [u8; 32], /// The json vaults file - pub vaults_file: PathBuf, + pub vaults_file: PathBuf, /// The vaults - pub vaults: Vec, + pub vaults: Vec, } impl Vault { @@ -80,11 +82,11 @@ impl Vault { note: Option>, ) -> Self { Self { - name: name.into(), + name: name.into(), username: username.map(Into::into), password: password.map(Into::into), - service: service.map(Into::into), - note: note.map(Into::into), + service: service.map(Into::into), + note: note.map(Into::into), } }