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 {
|
impl LprsCommand for Edit {
|
||||||
fn run(self, mut vault_manager: Vaults) -> LprsResult<()> {
|
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,
|
Ok((_, v)) => v,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
if self.force {
|
if self.force {
|
||||||
|
|
|
@ -112,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, &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 {
|
||||||
|
|
11
src/utils.rs
11
src/utils.rs
|
@ -244,16 +244,17 @@ pub fn prompt_custom(
|
||||||
///
|
///
|
||||||
/// ## 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(
|
pub fn vault_by_index_or_name<'v>(
|
||||||
location: Either<NonZeroUsize, String>,
|
location: &Either<NonZeroUsize, String>,
|
||||||
vaults: &mut [Vault],
|
vaults: &'v mut [Vault],
|
||||||
) -> LprsResult<(usize, &mut Vault)> {
|
) -> LprsResult<(usize, &'v mut Vault)> {
|
||||||
let idx = location
|
let idx = location
|
||||||
|
.as_ref()
|
||||||
.map_right(|name| {
|
.map_right(|name| {
|
||||||
vaults
|
vaults
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.find_map(|(idx, v)| (v.name == name).then_some(idx))
|
.find_map(|(idx, v)| (&v.name == name).then_some(idx))
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
LprsError::Other(format!("There is no vault with the given name `{name}`"))
|
LprsError::Other(format!("There is no vault with the given name `{name}`"))
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue