melib/notmuch: show error if account directory does not contain ".notmuch" subdirectory

Bug reported by user on mailing list.
pull/144/head
Manos Pitsidianakis 2022-08-30 12:23:25 +03:00
parent 29042aba59
commit 480000ebbb
2 changed files with 59 additions and 24 deletions

View File

@ -314,14 +314,32 @@ impl NotmuchDb {
#[cfg(target_os = "macos")]
let dlpath = "libnotmuch.5.dylib";
let lib = Arc::new(unsafe { libloading::Library::new(dlpath)? });
let path = Path::new(s.root_mailbox.as_str()).expand();
let mut path = Path::new(s.root_mailbox.as_str()).expand();
if !path.exists() {
return Err(MeliError::new(format!(
"\"root_mailbox\" {} for account {} is not a valid path.",
"Notmuch `root_mailbox` {} for account {} does not exist.",
s.root_mailbox.as_str(),
s.name()
)));
))
.set_kind(ErrorKind::Configuration));
}
if !path.is_dir() {
return Err(MeliError::new(format!(
"Notmuch `root_mailbox` {} for account {} is not a directory.",
s.root_mailbox.as_str(),
s.name()
))
.set_kind(ErrorKind::Configuration));
}
path.push(".notmuch");
if !path.exists() || !path.is_dir() {
return Err(MeliError::new(format!(
"Notmuch `root_mailbox` {} for account {} does not contain a `.notmuch` subdirectory.",
s.root_mailbox.as_str(),
s.name()
)).set_kind(ErrorKind::Configuration));
}
path.pop();
let mut mailboxes = HashMap::default();
for (k, f) in s.mailboxes.iter() {
@ -347,9 +365,11 @@ impl NotmuchDb {
);
} else {
return Err(MeliError::new(format!(
"notmuch mailbox configuration entry \"{}\" should have a \"query\" value set.",
k
)));
"notmuch mailbox configuration entry `{}` for account {} should have a `query` value set.",
k,
s.name(),
))
.set_kind(ErrorKind::Configuration));
}
}
@ -375,20 +395,42 @@ impl NotmuchDb {
}
pub fn validate_config(s: &mut AccountSettings) -> Result<()> {
let path = Path::new(s.root_mailbox.as_str()).expand();
let mut path = Path::new(s.root_mailbox.as_str()).expand();
if !path.exists() {
return Err(MeliError::new(format!(
"\"root_mailbox\" {} for account {} is not a valid path.",
"Notmuch `root_mailbox` {} for account {} does not exist.",
s.root_mailbox.as_str(),
s.name()
)));
))
.set_kind(ErrorKind::Configuration));
}
if !path.is_dir() {
return Err(MeliError::new(format!(
"Notmuch `root_mailbox` {} for account {} is not a directory.",
s.root_mailbox.as_str(),
s.name()
))
.set_kind(ErrorKind::Configuration));
}
path.push(".notmuch");
if !path.exists() || !path.is_dir() {
return Err(MeliError::new(format!(
"Notmuch `root_mailbox` {} for account {} does not contain a `.notmuch` subdirectory.",
s.root_mailbox.as_str(),
s.name()
)).set_kind(ErrorKind::Configuration));
}
path.pop();
let account_name = s.name().to_string();
for (k, f) in s.mailboxes.iter_mut() {
if f.extra.remove("query").is_none() {
return Err(MeliError::new(format!(
"notmuch mailbox configuration entry \"{}\" should have a \"query\" value set.",
k
)));
"notmuch mailbox configuration entry `{}` for account {} should have a `query` value set.",
k,
account_name,
))
.set_kind(ErrorKind::Configuration));
}
}
Ok(())

View File

@ -39,6 +39,7 @@ pub enum ErrorKind {
None,
External,
Authentication,
Configuration,
Bug,
Network,
Timeout,
@ -60,6 +61,7 @@ impl fmt::Display for ErrorKind {
ErrorKind::Network => "Network",
ErrorKind::Timeout => "Timeout",
ErrorKind::OSError => "OS Error",
ErrorKind::Configuration => "Configuration",
ErrorKind::NotImplemented => "Not implemented",
ErrorKind::NotSupported => "Not supported",
}
@ -69,24 +71,15 @@ impl fmt::Display for ErrorKind {
impl ErrorKind {
pub fn is_network(&self) -> bool {
match self {
ErrorKind::Network => true,
_ => false,
}
matches!(self, ErrorKind::Network)
}
pub fn is_timeout(&self) -> bool {
match self {
ErrorKind::Timeout => true,
_ => false,
}
matches!(self, ErrorKind::Timeout)
}
pub fn is_authentication(&self) -> bool {
match self {
ErrorKind::Authentication => true,
_ => false,
}
matches!(self, ErrorKind::Authentication)
}
}