From 1fe361920809dfd4e51929e5a3e0376f46078422 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sun, 4 Feb 2024 14:47:22 +0200 Subject: [PATCH] conf: Make conf validation recognize AccountSettings extra keys AccountSettings extra keys like `vcard_folder` were not taken into account when validating a config. This commit introduces an AccountSettings::validate_config() method that checks for the presence and validity of this key value. Fixes #349 https://git.meli-email.org/meli/meli/issues/349 Signed-off-by: Manos Pitsidianakis --- meli/src/conf.rs | 1 + melib/src/conf.rs | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/meli/src/conf.rs b/meli/src/conf.rs index ec81a2e8..35ddc2ac 100644 --- a/meli/src/conf.rs +++ b/meli/src/conf.rs @@ -555,6 +555,7 @@ This is required so that you don't accidentally start meli and find out later th .collect(), extra: extra.into_iter().collect(), }; + s.validate_config()?; backends.validate_config(&lowercase_format, &mut s)?; if !s.extra.is_empty() { return Err(Error::new(format!( diff --git a/melib/src/conf.rs b/melib/src/conf.rs index df07b3ec..9bf44236 100644 --- a/melib/src/conf.rs +++ b/melib/src/conf.rs @@ -21,14 +21,14 @@ //! Basic mail account configuration to use with //! [`backends`](./backends/index.html) -use std::collections::HashMap; +use std::{collections::HashMap, path::Path}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::{ backends::SpecialUsageMailbox, email::Address, - error::{Error, Result}, + error::{Error, ErrorKind, Result}, }; pub use crate::{SortField, SortOrder}; @@ -100,6 +100,34 @@ impl AccountSettings { )) } } + + pub fn validate_config(&mut self) -> Result<()> { + #[cfg(feature = "vcard")] + { + if let Some(folder) = self.extra.remove("vcard_folder") { + let path = Path::new(&folder); + + if !matches!(path.try_exists(), Ok(true)) { + return Err(Error::new(format!( + "`vcard_folder` path {} does not exist", + path.display() + )) + .set_details("`vcard_folder` must be a path of a folder containing .vcf files") + .set_kind(ErrorKind::Configuration)); + } + if !path.is_dir() { + return Err(Error::new(format!( + "`vcard_folder` path {} is not a directory", + path.display() + )) + .set_details("`vcard_folder` must be a path of a folder containing .vcf files") + .set_kind(ErrorKind::Configuration)); + } + } + } + + Ok(()) + } } #[derive(Clone, Debug, Deserialize, Serialize)]