From aee33f819cd17bc4b8db9420f0b41e191b31184b Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Tue, 20 Aug 2024 14:16:21 +0000
Subject: [PATCH] chore: Pass refrence of either to
`utils::vault_by_index_or_name` function
Signed-off-by: Awiteb
---
src/cli/edit_command.rs | 2 +-
src/cli/get_command.rs | 2 +-
src/utils.rs | 11 ++++++-----
3 files changed, 8 insertions(+), 7 deletions(-)
diff --git a/src/cli/edit_command.rs b/src/cli/edit_command.rs
index caa43a1..d2dd61e 100644
--- a/src/cli/edit_command.rs
+++ b/src/cli/edit_command.rs
@@ -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 {
diff --git a/src/cli/get_command.rs b/src/cli/get_command.rs
index 93d5498..641f040 100644
--- a/src/cli/get_command.rs
+++ b/src/cli/get_command.rs
@@ -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 {
diff --git a/src/utils.rs b/src/utils.rs
index 84c504d..944dfd9 100644
--- a/src/utils.rs
+++ b/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,
- vaults: &mut [Vault],
-) -> LprsResult<(usize, &mut Vault)> {
+pub fn vault_by_index_or_name<'v>(
+ location: &Either,
+ 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}`"))
})