refactor: Use Either<usize, String>
type instade of String
for index or name #65
7 changed files with 100 additions and 58 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -349,6 +349,12 @@ version = "1.0.17"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125"
|
checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "either"
|
||||||
|
version = "1.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "env_logger"
|
name = "env_logger"
|
||||||
version = "0.10.2"
|
version = "0.10.2"
|
||||||
|
@ -708,6 +714,7 @@ dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"clap_complete",
|
"clap_complete",
|
||||||
"directories",
|
"directories",
|
||||||
|
"either",
|
||||||
"inquire",
|
"inquire",
|
||||||
"log",
|
"log",
|
||||||
"passwords",
|
"passwords",
|
||||||
|
|
|
@ -32,6 +32,7 @@ base64 = "0.22.1"
|
||||||
clap_complete = "4.5.2"
|
clap_complete = "4.5.2"
|
||||||
totp-lite = "2.0.1"
|
totp-lite = "2.0.1"
|
||||||
base32 = "0.5.0"
|
base32 = "0.5.0"
|
||||||
|
either = { version = "1.13.0", default-features = false }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["update-notify"]
|
default = ["update-notify"]
|
||||||
|
|
|
@ -14,9 +14,14 @@
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
||||||
|
|
||||||
|
use std::{fmt::Display, str::FromStr};
|
||||||
|
|
||||||
|
use either::Either::{self, Left, Right};
|
||||||
|
|
||||||
use crate::{LprsError, LprsResult};
|
use crate::{LprsError, LprsResult};
|
||||||
|
|
||||||
/// Parse the key & value arguments.
|
/// Parse the key & value arguments.
|
||||||
|
///
|
||||||
/// ## Errors
|
/// ## Errors
|
||||||
/// - If the argument value syntax not `key=value`
|
/// - If the argument value syntax not `key=value`
|
||||||
pub fn kv_parser(value: &str) -> LprsResult<(String, Option<String>)> {
|
pub fn kv_parser(value: &str) -> LprsResult<(String, Option<String>)> {
|
||||||
|
@ -30,3 +35,27 @@ pub fn kv_parser(value: &str) -> LprsResult<(String, Option<String>)> {
|
||||||
Ok((value.trim().to_owned(), None))
|
Ok((value.trim().to_owned(), None))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parse `Either` type arguments.
|
||||||
|
///
|
||||||
|
/// ## Errors
|
||||||
|
/// - If the argument value can't be parsed to `L` or `R`
|
||||||
|
pub fn either_parser<L, R>(value: &str) -> LprsResult<Either<L, R>>
|
||||||
|
where
|
||||||
|
L: FromStr,
|
||||||
|
R: FromStr,
|
||||||
|
<L as FromStr>::Err: Display,
|
||||||
|
<R as FromStr>::Err: Display,
|
||||||
|
{
|
||||||
|
value
|
||||||
|
.trim()
|
||||||
|
.parse::<L>()
|
||||||
|
.map_err(|err| LprsError::ArgParse(err.to_string()))
|
||||||
|
.map(Left)
|
||||||
|
.or_else(|_| {
|
||||||
|
value
|
||||||
|
.parse::<R>()
|
||||||
|
.map_err(|err| LprsError::ArgParse(err.to_string()))
|
||||||
|
.map(Right)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -14,10 +14,13 @@
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
||||||
|
|
||||||
|
use std::num::NonZeroUsize;
|
||||||
|
|
||||||
use clap::Args;
|
use clap::Args;
|
||||||
|
use either::Either;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
clap_parsers,
|
clap_parsers::{either_parser, kv_parser},
|
||||||
utils,
|
utils,
|
||||||
vault::{cipher, Vaults},
|
vault::{cipher, Vaults},
|
||||||
LprsCommand,
|
LprsCommand,
|
||||||
|
@ -29,8 +32,8 @@ use crate::{
|
||||||
/// Edit command, used to edit the vault content
|
/// Edit command, used to edit the vault content
|
||||||
pub struct Edit {
|
pub struct Edit {
|
||||||
/// The vault to edit, index or name
|
/// The vault to edit, index or name
|
||||||
#[arg(name = "INDEX-or-NAME")]
|
#[arg(name = "INDEX-or-NAME", value_parser = either_parser::<NonZeroUsize, String>)]
|
||||||
location: String,
|
location: Either<NonZeroUsize, String>,
|
||||||
|
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
/// The new vault name
|
/// The new vault name
|
||||||
|
@ -61,7 +64,7 @@ pub struct Edit {
|
||||||
/// If the custom field not exist will created it, if it's will update it,
|
/// If the custom field not exist will created it, if it's will update it,
|
||||||
/// if there is no value, you will enter it through a prompt (e.g `-c key`)
|
/// if there is no value, you will enter it through a prompt (e.g `-c key`)
|
||||||
#[arg(name = "KEY=VALUE", short = 'c', long = "custom")]
|
#[arg(name = "KEY=VALUE", short = 'c', long = "custom")]
|
||||||
#[arg(value_parser = clap_parsers::kv_parser)]
|
#[arg(value_parser = kv_parser)]
|
||||||
custom_fields: Vec<(String, Option<String>)>,
|
custom_fields: Vec<(String, Option<String>)>,
|
||||||
/// Force edit, will not return error if there is a problem with the args.
|
/// Force edit, will not return error if there is a problem with the args.
|
||||||
///
|
///
|
||||||
|
@ -72,8 +75,7 @@ pub struct Edit {
|
||||||
|
|
||||||
impl LprsCommand for Edit {
|
impl LprsCommand for Edit {
|
||||||
fn run(self, mut vault_manager: Vaults) -> LprsResult<()> {
|
fn run(self, mut vault_manager: Vaults) -> LprsResult<()> {
|
||||||
let vault =
|
let vault = match utils::vault_by_index_or_name(self.location, &mut vault_manager.vaults) {
|
||||||
match utils::vault_by_index_or_name(self.location.trim(), &mut vault_manager.vaults) {
|
|
||||||
Ok((_, v)) => v,
|
Ok((_, v)) => v,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
if self.force {
|
if self.force {
|
||||||
|
|
|
@ -14,11 +14,13 @@
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
||||||
|
|
||||||
use std::str::FromStr;
|
use std::{num::NonZeroUsize, str::FromStr};
|
||||||
|
|
||||||
use clap::Args;
|
use clap::Args;
|
||||||
|
use either::Either;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
clap_parsers::either_parser,
|
||||||
utils,
|
utils,
|
||||||
vault::{cipher, Vault, Vaults},
|
vault::{cipher, Vault, Vaults},
|
||||||
LprsCommand,
|
LprsCommand,
|
||||||
|
@ -94,8 +96,9 @@ impl VaultGetField {
|
||||||
/// Command to get a entire vault or single field from it
|
/// Command to get a entire vault or single field from it
|
||||||
pub struct Get {
|
pub struct Get {
|
||||||
/// Whether the index of the vault or its name
|
/// Whether the index of the vault or its name
|
||||||
#[arg(value_name = "INDEX-or-NAME")]
|
#[arg(name = "INDEX-or-NAME", value_parser = either_parser::<NonZeroUsize, String>)]
|
||||||
location: String,
|
location: Either<NonZeroUsize, String>,
|
||||||
|
|
||||||
/// A Specific field to get.
|
/// A Specific field to get.
|
||||||
///
|
///
|
||||||
/// Can be [name, username, password, service, note, totp_secret, totp_code,
|
/// Can be [name, username, password, service, note, totp_secret, totp_code,
|
||||||
|
@ -109,7 +112,7 @@ pub struct Get {
|
||||||
impl LprsCommand for Get {
|
impl LprsCommand for Get {
|
||||||
fn run(self, mut vault_manager: Vaults) -> LprsResult<()> {
|
fn run(self, mut vault_manager: Vaults) -> LprsResult<()> {
|
||||||
let (index, vault) =
|
let (index, vault) =
|
||||||
utils::vault_by_index_or_name(self.location.trim(), &mut vault_manager.vaults)?;
|
utils::vault_by_index_or_name(self.location, &mut vault_manager.vaults)?;
|
||||||
|
|
||||||
if let Some(field) = self.field {
|
if let Some(field) = self.field {
|
||||||
if field == VaultGetField::Index {
|
if field == VaultGetField::Index {
|
||||||
|
|
|
@ -14,16 +14,19 @@
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
||||||
|
|
||||||
use clap::Args;
|
use std::num::NonZeroUsize;
|
||||||
|
|
||||||
use crate::{utils, vault::Vaults, LprsCommand, LprsResult};
|
use clap::Args;
|
||||||
|
use either::Either;
|
||||||
|
|
||||||
|
use crate::{clap_parsers::either_parser, utils, vault::Vaults, LprsCommand, LprsResult};
|
||||||
|
|
||||||
#[derive(Debug, Args)]
|
#[derive(Debug, Args)]
|
||||||
/// Remove command, used to remove a vault from the vaults file
|
/// Remove command, used to remove a vault from the vaults file
|
||||||
pub struct Remove {
|
pub struct Remove {
|
||||||
/// The vault to remove, index or name
|
/// The vault to remove, index or name
|
||||||
#[arg(name = "INDEX-or-NAME")]
|
#[arg(name = "INDEX-or-NAME", value_parser = either_parser::<NonZeroUsize, String>)]
|
||||||
location: String,
|
location: Either<NonZeroUsize, String>,
|
||||||
|
|
||||||
/// Force remove, will not return error if there is no vault with the given
|
/// Force remove, will not return error if there is no vault with the given
|
||||||
/// index or name
|
/// index or name
|
||||||
|
@ -33,8 +36,7 @@ pub struct Remove {
|
||||||
|
|
||||||
impl LprsCommand for Remove {
|
impl LprsCommand for Remove {
|
||||||
fn run(self, mut vault_manager: Vaults) -> LprsResult<()> {
|
fn run(self, mut vault_manager: Vaults) -> LprsResult<()> {
|
||||||
let index =
|
let index = match utils::vault_by_index_or_name(self.location, &mut vault_manager.vaults) {
|
||||||
match utils::vault_by_index_or_name(self.location.trim(), &mut vault_manager.vaults) {
|
|
||||||
Ok((idx, _)) => idx,
|
Ok((idx, _)) => idx,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
if self.force {
|
if self.force {
|
||||||
|
|
50
src/utils.rs
50
src/utils.rs
|
@ -15,8 +15,10 @@
|
||||||
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
use std::num::NonZeroUsize;
|
||||||
use std::{fs, path::PathBuf};
|
use std::{fs, path::PathBuf};
|
||||||
|
|
||||||
|
use either::Either;
|
||||||
use inquire::{validator::Validation, Password, PasswordDisplayMode};
|
use inquire::{validator::Validation, Password, PasswordDisplayMode};
|
||||||
use passwords::{analyzer, scorer};
|
use passwords::{analyzer, scorer};
|
||||||
#[cfg(feature = "update-notify")]
|
#[cfg(feature = "update-notify")]
|
||||||
|
@ -231,35 +233,31 @@ pub fn prompt_custom(
|
||||||
Ok(new_fields)
|
Ok(new_fields)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the vault with its index by its index or name
|
/// Returns the vault with its index by either its index or name
|
||||||
///
|
///
|
||||||
/// ## Errors
|
/// ## Errors
|
||||||
/// - If there is no vault with the given index or name
|
/// - If there is no vault with the given index or name
|
||||||
pub fn vault_by_index_or_name<'a>(
|
pub fn vault_by_index_or_name(
|
||||||
index_or_name: &str,
|
location: Either<NonZeroUsize, String>,
|
||||||
vaults: &'a mut [Vault],
|
vaults: &mut [Vault],
|
||||||
) -> LprsResult<(usize, &'a mut Vault)> {
|
) -> LprsResult<(usize, &mut Vault)> {
|
||||||
let parsed_index = index_or_name.parse::<usize>();
|
let idx = location
|
||||||
|
.map_right(|name| {
|
||||||
let Some((index, vault)) = (if let Ok(index) = parsed_index {
|
|
||||||
index
|
|
||||||
.checked_sub(1)
|
|
||||||
.and_then(|zeroindex| vaults.get_mut(zeroindex).map(|v| (index, v)))
|
|
||||||
} else {
|
|
||||||
vaults
|
vaults
|
||||||
.iter_mut()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.find(|(_, v)| v.name == index_or_name)
|
.find_map(|(idx, v)| (v.name == name).then_some(idx))
|
||||||
}) else {
|
.ok_or_else(|| {
|
||||||
return Err(LprsError::Other(format!(
|
LprsError::Other(format!("There is no vault with the given name `{name}`"))
|
||||||
"There is no vault with the given {} `{}`",
|
})
|
||||||
if parsed_index.is_ok() {
|
})
|
||||||
"index"
|
.map_left(|idx| LprsResult::Ok(idx.get() - 1))
|
||||||
} else {
|
.into_inner()?;
|
||||||
"name"
|
|
||||||
},
|
Ok((
|
||||||
index_or_name,
|
idx,
|
||||||
)));
|
vaults.get_mut(idx).ok_or_else(|| {
|
||||||
};
|
LprsError::Other(format!("There is no vault with the given index `{idx}`"))
|
||||||
Ok((index, vault))
|
})?,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue