use std::path::{Path, PathBuf};
use notmuch::{Database, DatabaseMode};
use shellexpand_utils::shellexpand_path;
#[doc(inline)]
pub use super::{Error, Result};
#[derive(Debug, Default, Clone, Eq, PartialEq)]
#[cfg_attr(
feature = "derive",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "kebab-case")
)]
pub struct NotmuchConfig {
#[cfg_attr(feature = "derive", serde(alias = "db-path"))]
pub database_path: Option<PathBuf>,
pub maildir_path: Option<PathBuf>,
pub config_path: Option<PathBuf>,
pub profile: Option<String>,
#[cfg_attr(feature = "derive", serde(default))]
pub maildirpp: bool,
}
impl NotmuchConfig {
pub fn get_default_database_path() -> Result<PathBuf> {
Ok(Database::open_with_config(
None::<PathBuf>,
DatabaseMode::ReadOnly,
None::<PathBuf>,
None,
)
.map_err(Error::OpenDatabaseError)?
.path()
.to_owned())
}
pub fn try_get_database_path(&self) -> Result<PathBuf> {
match self.database_path.as_ref() {
Some(path) => Ok(shellexpand_path(path)),
None => Self::get_default_database_path(),
}
}
pub fn try_get_maildir_path(&self) -> Result<PathBuf> {
match self.maildir_path.as_ref() {
Some(path) => Ok(shellexpand_path(path)),
None => self.try_get_database_path(),
}
}
pub fn find_config_path(&self) -> Option<&Path> {
self.config_path.as_ref().map(AsRef::as_ref)
}
pub fn find_profile(&self) -> Option<&str> {
self.profile.as_deref()
}
}