chore: Pass refrence of either to utils::vault_by_index_or_name
function
Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
parent
ad3dc025a9
commit
aee33f819c
3 changed files with 8 additions and 7 deletions
|
@ -75,7 +75,7 @@ pub struct Edit {
|
|||
|
||||
impl LprsCommand for Edit {
|
||||
fn run(self, mut vault_manager: Vaults) -> LprsResult<()> {
|
||||
let vault = match utils::vault_by_index_or_name(self.location, &mut vault_manager.vaults) {
|
||||
let vault = match utils::vault_by_index_or_name(&self.location, &mut vault_manager.vaults) {
|
||||
Ok((_, v)) => v,
|
||||
Err(err) => {
|
||||
if self.force {
|
||||
|
|
|
@ -112,7 +112,7 @@ pub struct Get {
|
|||
impl LprsCommand for Get {
|
||||
fn run(self, mut vault_manager: Vaults) -> LprsResult<()> {
|
||||
let (index, vault) =
|
||||
utils::vault_by_index_or_name(self.location, &mut vault_manager.vaults)?;
|
||||
utils::vault_by_index_or_name(&self.location, &mut vault_manager.vaults)?;
|
||||
|
||||
if let Some(field) = self.field {
|
||||
if field == VaultGetField::Index {
|
||||
|
|
11
src/utils.rs
11
src/utils.rs
|
@ -244,16 +244,17 @@ pub fn prompt_custom(
|
|||
///
|
||||
/// ## Errors
|
||||
/// - If there is no vault with the given index or name
|
||||
pub fn vault_by_index_or_name(
|
||||
location: Either<NonZeroUsize, String>,
|
||||
vaults: &mut [Vault],
|
||||
) -> LprsResult<(usize, &mut Vault)> {
|
||||
pub fn vault_by_index_or_name<'v>(
|
||||
location: &Either<NonZeroUsize, String>,
|
||||
vaults: &'v mut [Vault],
|
||||
) -> LprsResult<(usize, &'v mut Vault)> {
|
||||
let idx = location
|
||||
.as_ref()
|
||||
.map_right(|name| {
|
||||
vaults
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find_map(|(idx, v)| (v.name == name).then_some(idx))
|
||||
.find_map(|(idx, v)| (&v.name == name).then_some(idx))
|
||||
.ok_or_else(|| {
|
||||
LprsError::Other(format!("There is no vault with the given name `{name}`"))
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue