diff --git a/src/cli/add_command.rs b/src/cli/add_command.rs index 3f5a698..d8389a9 100644 --- a/src/cli/add_command.rs +++ b/src/cli/add_command.rs @@ -17,7 +17,7 @@ use clap::Args; use crate::{ - vault::{vault_state::*, Vault, Vaults}, + vault::{Vault, Vaults}, LprsCommand, LprsError, LprsResult, }; @@ -25,14 +25,14 @@ use crate::{ #[command(author, version, about, long_about = None)] pub struct Add { #[command(flatten)] - vault_info: Vault, + vault_info: Vault, /// The password, if there is no value for it you will prompt it #[arg(short, long)] password: Option>, } impl LprsCommand for Add { - fn run(mut self, mut vault_manager: Vaults) -> LprsResult<()> { + fn run(mut self, mut vault_manager: Vaults) -> LprsResult<()> { match self.password { Some(Some(password)) => { log::debug!("User provided a password"); diff --git a/src/cli/clean_command.rs b/src/cli/clean_command.rs index bc29626..88001f9 100644 --- a/src/cli/clean_command.rs +++ b/src/cli/clean_command.rs @@ -18,17 +18,14 @@ use std::fs; use clap::Args; -use crate::{ - vault::{vault_state::*, Vaults}, - LprsCommand, LprsError, LprsResult, -}; +use crate::{vault::Vaults, LprsCommand, LprsError, LprsResult}; #[derive(Debug, Args)] #[command(author, version, about, long_about = None)] pub struct Clean {} impl LprsCommand for Clean { - fn run(self, vault_manager: Vaults) -> LprsResult<()> { + fn run(self, vault_manager: Vaults) -> LprsResult<()> { log::info!( "Cleaning the vaults file: {:?}", vault_manager.vaults_file.display() diff --git a/src/cli/edit_command.rs b/src/cli/edit_command.rs index 443f248..fba2433 100644 --- a/src/cli/edit_command.rs +++ b/src/cli/edit_command.rs @@ -19,7 +19,7 @@ use std::num::NonZeroU64; use clap::Args; use crate::{ - vault::{vault_state::*, Vault, Vaults}, + vault::{Vault, Vaults}, LprsCommand, LprsError, LprsResult, }; @@ -47,7 +47,7 @@ pub struct Edit { } impl LprsCommand for Edit { - fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { + fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { let index = self.index.get() as usize; log::debug!("Editing vault at index: {index}"); @@ -73,7 +73,7 @@ impl LprsCommand for Edit { }; log::info!("Applying the new values to the vault"); - *vault = Vault::::new( + *vault = Vault::new( self.name.as_ref().unwrap_or(&vault.name), self.username.as_ref().or(vault.username.as_ref()), password.as_ref().or(vault.password.as_ref()), diff --git a/src/cli/export_command.rs b/src/cli/export_command.rs index fb5d701..3d576a6 100644 --- a/src/cli/export_command.rs +++ b/src/cli/export_command.rs @@ -19,7 +19,7 @@ use std::{fs, io::Error as IoError, io::ErrorKind as IoErrorKind, path::PathBuf} use clap::Args; use crate::{ - vault::{vault_state::*, BitWardenPasswords, Format, Vault, Vaults}, + vault::{BitWardenPasswords, Format, Vault, Vaults}, LprsCommand, LprsError, LprsResult, }; @@ -34,7 +34,7 @@ pub struct Export { } impl LprsCommand for Export { - fn run(self, vault_manager: Vaults) -> LprsResult<()> { + fn run(self, vault_manager: Vaults) -> LprsResult<()> { log::debug!( "Exporting vault {} to: {} with format: {}", vault_manager.vaults_file.display(), @@ -42,9 +42,7 @@ impl LprsCommand for Export { self.format ); let exported_data = match self.format { - Format::Lprs => { - serde_json::to_string::>>(&vault_manager.encrypt_vaults()?) - } + Format::Lprs => serde_json::to_string::>(&vault_manager.encrypt_vaults()?), Format::BitWarden => serde_json::to_string(&BitWardenPasswords::from(vault_manager)), }?; diff --git a/src/cli/gen_command.rs b/src/cli/gen_command.rs index 92dfae3..ba4ff5f 100644 --- a/src/cli/gen_command.rs +++ b/src/cli/gen_command.rs @@ -18,10 +18,7 @@ use std::num::NonZeroU64; use clap::Args; -use crate::{ - vault::{vault_state::*, Vaults}, - LprsCommand, LprsError, LprsResult, -}; +use crate::{vault::Vaults, LprsCommand, LprsError, LprsResult}; #[derive(Debug, Args)] #[command(author, version, about, long_about = None)] @@ -45,7 +42,7 @@ pub struct Gen { } impl LprsCommand for Gen { - fn run(self, _vault_manager: Vaults) -> LprsResult<()> { + fn run(self, _vault_manager: Vaults) -> LprsResult<()> { println!( "{}", passwords::PasswordGenerator::new() diff --git a/src/cli/import_command.rs b/src/cli/import_command.rs index 1e2b098..31ca224 100644 --- a/src/cli/import_command.rs +++ b/src/cli/import_command.rs @@ -19,7 +19,7 @@ use std::{fs::File, io::Error as IoError, io::ErrorKind as IoErrorKind, path::Pa use clap::Args; use crate::{ - vault::{vault_state::*, BitWardenPasswords, Format, Vault, Vaults}, + vault::{BitWardenPasswords, Format, Vault, Vaults}, LprsCommand, LprsError, LprsResult, }; @@ -35,7 +35,7 @@ pub struct Import { } impl LprsCommand for Import { - fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { + fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { log::debug!( "Importing vaults from: {} with format: {} to the vault: {}", self.path.display(), diff --git a/src/cli/list_command.rs b/src/cli/list_command.rs index 266f426..10e8bc3 100644 --- a/src/cli/list_command.rs +++ b/src/cli/list_command.rs @@ -19,10 +19,7 @@ use std::num::NonZeroU64; use clap::Args; use inquire::Select; -use crate::{ - vault::{vault_state::*, Vaults}, - LprsCommand, LprsError, LprsResult, -}; +use crate::{vault::Vaults, LprsCommand, LprsError, LprsResult}; #[derive(Debug, Args)] #[command(author, version, about, long_about = None)] @@ -39,7 +36,7 @@ pub struct List { } impl LprsCommand for List { - fn run(self, vault_manager: Vaults) -> LprsResult<()> { + fn run(self, vault_manager: Vaults) -> LprsResult<()> { if vault_manager.vaults.is_empty() { return Err(LprsError::Other( "Looks like there is no vaults to list".to_owned(), diff --git a/src/cli/remove_command.rs b/src/cli/remove_command.rs index d255e5b..df97091 100644 --- a/src/cli/remove_command.rs +++ b/src/cli/remove_command.rs @@ -18,10 +18,7 @@ use std::num::NonZeroU64; use clap::Args; -use crate::{ - vault::{vault_state::*, Vaults}, - LprsCommand, LprsError, LprsResult, -}; +use crate::{vault::Vaults, LprsCommand, LprsError, LprsResult}; #[derive(Debug, Args)] #[command(author, version, about, long_about = None)] @@ -35,7 +32,7 @@ pub struct Remove { } impl LprsCommand for Remove { - fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { + fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { let index = (self.index.get() - 1) as usize; log::debug!("Removing vault at index: {index}"); diff --git a/src/macros.rs b/src/macros.rs index 1b19e23..7507f8d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -53,7 +53,7 @@ macro_rules! impl_commands { ($enum_name: ident, $($varint: ident)+) => { #[automatically_derived] impl $crate::LprsCommand for $enum_name{ - fn run(self, vault_manager: $crate::vault::Vaults<$crate::vault::vault_state::Plain>) -> $crate::LprsResult<()> { + fn run(self, vault_manager: $crate::vault::Vaults) -> $crate::LprsResult<()> { match self { $( Self::$varint(command) => command.run(vault_manager), diff --git a/src/traits.rs b/src/traits.rs index 708829a..4c7985e 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -14,15 +14,12 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{ - vault::{vault_state::*, Vaults}, - LprsResult, -}; +use crate::{vault::Vaults, LprsResult}; /// Trait to work with the commands pub trait LprsCommand { /// Run the command, should do all the logic, even the export - fn run(self, vault_manager: Vaults) -> LprsResult<()>; + fn run(self, vault_manager: Vaults) -> LprsResult<()>; /// Validate the gaiven args from the user. fn validate_args(&self) -> LprsResult<()> { diff --git a/src/vault/bitwarden.rs b/src/vault/bitwarden.rs index f287464..962127c 100644 --- a/src/vault/bitwarden.rs +++ b/src/vault/bitwarden.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use super::{vault_state::*, Vault, Vaults}; +use super::{Vault, Vaults}; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct BitWardenLoginData { @@ -39,7 +39,7 @@ pub struct BitWardenPasswords { pub items: Vec, } -impl From for Vault { +impl From for Vault { fn from(value: BitWardenPassword) -> Self { Self::new( value.name, @@ -55,8 +55,8 @@ impl From for Vault { } } -impl From> for BitWardenPassword { - fn from(value: Vault) -> Self { +impl From for BitWardenPassword { + fn from(value: Vault) -> Self { Self { ty: 1, name: value.name, @@ -72,8 +72,8 @@ impl From> for BitWardenPassword { } } -impl From> for BitWardenPasswords { - fn from(value: Vaults) -> Self { +impl From for BitWardenPasswords { + fn from(value: Vaults) -> Self { Self { encrypted: false, folders: Vec::new(), diff --git a/src/vault/mod.rs b/src/vault/mod.rs index a7d2fef..1c3ea8e 100644 --- a/src/vault/mod.rs +++ b/src/vault/mod.rs @@ -14,13 +14,12 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use std::{fs, marker::PhantomData, path::PathBuf}; +use std::{fs, path::PathBuf}; use clap::{Parser, ValueEnum}; use serde::{Deserialize, Serialize}; use crate::{LprsError, LprsResult}; -use vault_state::*; pub mod cipher; @@ -36,23 +35,10 @@ pub enum Format { BitWarden, } -/// The states of the vaults -pub mod vault_state { - /// Means the vault is encrypted - #[derive(Clone, Debug, Default)] - pub struct Encrypted; - /// Means the vault is not encrypted - #[derive(Clone, Debug, Default)] - pub struct Plain; -} - /// The vault struct #[serde_with_macros::skip_serializing_none] #[derive(Clone, Debug, Deserialize, Serialize, Parser)] -pub struct Vault -where - T: std::fmt::Debug + Clone, -{ +pub struct Vault { /// The name of the vault #[arg(short, long)] pub name: String, @@ -68,31 +54,20 @@ where /// Add a note to the vault #[arg(short = 'o', long)] pub note: Option, - - /// State phantom - #[serde(skip)] - #[arg(skip)] - phantom: PhantomData, } /// The vaults manager #[derive(Default)] -pub struct Vaults -where - T: std::fmt::Debug + Clone, -{ +pub struct Vaults { /// Hash of the master password pub master_password: Vec, /// The json vaults file pub vaults_file: PathBuf, /// The vaults - pub vaults: Vec>, + pub vaults: Vec, } -impl Vault -where - T: std::fmt::Debug + Clone, -{ +impl Vault { /// Create new [`Vault`] instance pub fn new( name: impl Into, @@ -107,15 +82,12 @@ where password: password.map(Into::into), service: service.map(Into::into), note: note.map(Into::into), - phantom: std::marker::PhantomData, } } -} -impl Vault { /// Decrypt the vault - pub fn decrypt(&self, master_password: &[u8]) -> LprsResult> { - Ok(Vault::::new( + pub fn decrypt(&self, master_password: &[u8]) -> LprsResult { + Ok(Vault::new( cipher::decrypt(master_password, &self.name)?, cipher::decrypt_some(master_password, self.username.as_ref())?, cipher::decrypt_some(master_password, self.password.as_ref())?, @@ -123,12 +95,10 @@ impl Vault { cipher::decrypt_some(master_password, self.note.as_ref())?, )) } -} -impl Vault { /// Encrypt the vault - pub fn encrypt(&self, master_password: &[u8]) -> LprsResult> { - Ok(Vault::::new( + pub fn encrypt(&self, master_password: &[u8]) -> LprsResult { + Ok(Vault::new( cipher::encrypt(master_password, &self.name)?, cipher::encrypt_some(master_password, self.username.as_ref())?, cipher::encrypt_some(master_password, self.password.as_ref())?, @@ -154,23 +124,18 @@ impl Vault { } } -impl Vaults -where - T: std::fmt::Debug + Clone, -{ +impl Vaults { /// Create new [`Vaults`] instnce - pub fn new(master_password: Vec, vaults_file: PathBuf, vaults: Vec>) -> Self { + pub fn new(master_password: Vec, vaults_file: PathBuf, vaults: Vec) -> Self { Self { master_password, vaults_file, vaults, } } -} -impl Vaults { /// Encrypt the vaults - pub fn encrypt_vaults(&self) -> LprsResult>> { + pub fn encrypt_vaults(&self) -> LprsResult> { self.vaults .iter() .map(|p| p.encrypt(&self.master_password)) @@ -179,11 +144,10 @@ impl Vaults { /// Reload the vaults from the file then decrypt it pub fn try_reload(vaults_file: PathBuf, master_password: Vec) -> LprsResult { - let vaults = - serde_json::from_str::>>(&fs::read_to_string(&vaults_file)?)? - .into_iter() - .map(|p| p.decrypt(master_password.as_slice())) - .collect::>>>()?; + let vaults = serde_json::from_str::>(&fs::read_to_string(&vaults_file)?)? + .into_iter() + .map(|p| p.decrypt(master_password.as_slice())) + .collect::>>()?; Ok(Self::new(master_password, vaults_file, vaults)) } @@ -202,7 +166,7 @@ impl Vaults { } /// Add new vault - pub fn add_vault(&mut self, vault: Vault) { + pub fn add_vault(&mut self, vault: Vault) { self.vaults.push(vault) } } @@ -219,7 +183,7 @@ impl std::fmt::Display for Format { } } -impl std::fmt::Display for Vault { +impl std::fmt::Display for Vault { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Name: {}", self.name)?; if let Some(ref username) = self.username { diff --git a/src/vault/validator.rs b/src/vault/validator.rs index 784a84b..c29d69f 100644 --- a/src/vault/validator.rs +++ b/src/vault/validator.rs @@ -18,7 +18,7 @@ use std::{fs, path::Path}; use crate::LprsResult; -use super::{vault_state::*, Vault}; +use super::Vault; /// Return if the vaults file new file or not pub fn is_new_vaults_file(path: &Path) -> LprsResult { @@ -26,7 +26,7 @@ pub fn is_new_vaults_file(path: &Path) -> LprsResult { let file_content = fs::read_to_string(path)?; if !file_content.is_empty() && file_content.trim() != "[]" - && serde_json::from_str::>>(&file_content).is_ok() + && serde_json::from_str::>(&file_content).is_ok() { return Ok(false); }