chore: Util to ask the user for the value of a key without value

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-08-17 14:50:38 +00:00
parent bbc2a7a944
commit 2f0531fbc9
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F

View file

@ -181,7 +181,7 @@ pub fn lprs_version() -> LprsResult<Option<String>> {
}
/// 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<String>)]) -> 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<String>)>,
) -> LprsResult<Vec<(String, String)>> {
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