From 2f0531fbc997f38f910dc0cd5b6273d3d96ce52e Mon Sep 17 00:00:00 2001 From: Awiteb Date: Sat, 17 Aug 2024 14:50:38 +0000 Subject: [PATCH] chore: Util to ask the user for the value of a key without value Signed-off-by: Awiteb --- src/utils.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index eb5ed46..74eccba 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -181,7 +181,7 @@ pub fn lprs_version() -> LprsResult> { } /// Returns the duplicated field from the custom field (unprocessed fields) -pub fn get_duplicated_field(fields: &[(String, String)]) -> Option<&str> { +pub fn get_duplicated_field(fields: &[(String, Option)]) -> Option<&str> { fields.iter().find_map(|(key, _)| { if fields.iter().filter(|(k, _)| key == k).count() > 1 { return Some(key.as_str()); @@ -210,6 +210,27 @@ pub fn apply_custom_fields( } } +/// Make sure all custom field values are there, if not, ask the user for it +/// +/// ## Errors +/// - If can't read the user input +pub fn prompt_custom( + custom_fields: Vec<(String, Option)>, +) -> LprsResult> { + let mut new_fields = Vec::new(); + + for (key, value) in custom_fields { + if let Some(value) = value { + new_fields.push((key, value)); + } else { + let value = inquire::Text::new(&format!("Value of `{key}`:")).prompt()?; + new_fields.push((key, value)); + } + } + + Ok(new_fields) +} + /// Returns the vault with its index by its index or name /// /// ## Errors