From 00128a795739e444f549854970fc36f4082523b8 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Sat, 11 May 2024 13:00:32 +0300 Subject: [PATCH] chore: Add a reserved custom fields prefix --- src/cli/add_command.rs | 7 +++++++ src/cli/edit_command.rs | 7 +++++++ src/cli/import_command.rs | 8 ++++++++ src/errors.rs | 5 +++++ src/main.rs | 2 ++ src/vault/mod.rs | 7 ++++++- 6 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/cli/add_command.rs b/src/cli/add_command.rs index 28ec52a..d712b1d 100644 --- a/src/cli/add_command.rs +++ b/src/cli/add_command.rs @@ -72,6 +72,13 @@ impl LprsCommand for Add { ))); } } + if self + .custom_fields + .iter() + .any(|(k, _)| k.starts_with(crate::RESERVED_FIELD_PREFIX)) + { + return Err(LprsError::ReservedPrefix(crate::RESERVED_FIELD_PREFIX)); + } Ok(()) } diff --git a/src/cli/edit_command.rs b/src/cli/edit_command.rs index e360e65..21e67a2 100644 --- a/src/cli/edit_command.rs +++ b/src/cli/edit_command.rs @@ -109,6 +109,13 @@ impl LprsCommand for Edit { ))); } } + if self + .custom_fields + .iter() + .any(|(k, _)| k.starts_with(crate::RESERVED_FIELD_PREFIX)) + { + return Err(LprsError::ReservedPrefix(crate::RESERVED_FIELD_PREFIX)); + } Ok(()) } diff --git a/src/cli/import_command.rs b/src/cli/import_command.rs index 6466829..4c11e8f 100644 --- a/src/cli/import_command.rs +++ b/src/cli/import_command.rs @@ -71,6 +71,14 @@ impl LprsCommand for Import { .unwrap_or(&vault_manager.master_password), &fs::read(self.path)?, )?; + + if vaults.iter().any(|v| { + v.custom_fields + .iter() + .any(|(k, _)| k.starts_with(crate::RESERVED_FIELD_PREFIX)) + }) { + return Err(LprsError::ReservedPrefix(crate::RESERVED_FIELD_PREFIX)); + } let vaults_len = vaults.len(); vault_manager.vaults.extend(vaults); diff --git a/src/errors.rs b/src/errors.rs index f02bde3..15225fe 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -39,6 +39,11 @@ pub enum Error { InvalidVaultIndex(String), #[error("{0}")] ArgParse(String), + #[error( + "Reserved Prefix Error: Sorry, but the following prefix is reserved and cannot be used in \ + custom fields {0}" + )] + ReservedPrefix(&'static str), #[error("{0}")] Other(String), diff --git a/src/main.rs b/src/main.rs index 9e15e53..782fceb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,8 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION"); #[cfg(feature = "update-notify")] /// The last version check file. Used to store the last version check time. pub const LAST_VERSION_CHECK_FILE: &str = ".last_version_check"; +/// The prefix of the reserved custom fields +const RESERVED_FIELD_PREFIX: &str = ".lprsfield."; fn main() -> ExitCode { let lprs_cli = cli::Cli::parse(); diff --git a/src/vault/mod.rs b/src/vault/mod.rs index 7c0c022..8e62884 100644 --- a/src/vault/mod.rs +++ b/src/vault/mod.rs @@ -267,7 +267,12 @@ impl fmt::Display for Vault { write!(f, "\nNote:\n{note}")?; } for (key, value) in &self.custom_fields { - write!(f, "\n{key}: {value}")?; + write!( + f, + "\n{}: {value}", + key.strip_prefix(crate::RESERVED_FIELD_PREFIX) + .unwrap_or(key) + )?; } Ok(())