chore: Add a reserved custom fields prefix

This commit is contained in:
Awiteb 2024-05-11 13:00:32 +03:00
parent 47318142b8
commit 00128a7957
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
6 changed files with 35 additions and 1 deletions

View file

@ -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(()) Ok(())
} }

View file

@ -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(()) Ok(())
} }

View file

@ -71,6 +71,14 @@ impl LprsCommand for Import {
.unwrap_or(&vault_manager.master_password), .unwrap_or(&vault_manager.master_password),
&fs::read(self.path)?, &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(); let vaults_len = vaults.len();
vault_manager.vaults.extend(vaults); vault_manager.vaults.extend(vaults);

View file

@ -39,6 +39,11 @@ pub enum Error {
InvalidVaultIndex(String), InvalidVaultIndex(String),
#[error("{0}")] #[error("{0}")]
ArgParse(String), 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}")] #[error("{0}")]
Other(String), Other(String),

View file

@ -48,6 +48,8 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
#[cfg(feature = "update-notify")] #[cfg(feature = "update-notify")]
/// The last version check file. Used to store the last version check time. /// The last version check file. Used to store the last version check time.
pub const LAST_VERSION_CHECK_FILE: &str = ".last_version_check"; 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 { fn main() -> ExitCode {
let lprs_cli = cli::Cli::parse(); let lprs_cli = cli::Cli::parse();

View file

@ -267,7 +267,12 @@ impl fmt::Display for Vault {
write!(f, "\nNote:\n{note}")?; write!(f, "\nNote:\n{note}")?;
} }
for (key, value) in &self.custom_fields { 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(()) Ok(())