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

Bug reported by user on mailing list.
pull/144/head
Manos Pitsidianakis 2 years ago
parent 29042aba59
commit 480000ebbb

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

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

Loading…
Cancel
Save