diff --git a/notmuch/src/lib.rs b/notmuch/src/lib.rs index 513b598..7626b8f 100644 --- a/notmuch/src/lib.rs +++ b/notmuch/src/lib.rs @@ -206,7 +206,12 @@ //! } //! ``` -use std::{ffi::OsStr, process::Command}; +use std::{ + ffi::OsStr, + io::Result, + path::{Path, PathBuf}, + process::Command, +}; use serde::Deserialize; @@ -378,14 +383,54 @@ pub struct ThreadSummary { // TODO(wathiede): notmuch reply schema -pub fn run_notmuch(args: I) -> std::io::Result> -where - I: IntoIterator, - S: AsRef, -{ - let mut cmd = Command::new("notmuch"); - let cmd = cmd.args(args); - dbg!(&cmd); - let out = cmd.output()?; - Ok(out.stdout) +struct Notmuch { + config_path: PathBuf, +} + +impl Notmuch { + pub fn with_config>(config_path: P) -> Notmuch { + Notmuch { + config_path: config_path.as_ref().into(), + } + } + + pub fn new(&self) -> Result> { + self.run_notmuch(["new"]) + } + + pub fn no_args(&self) -> Result> { + self.run_notmuch(std::iter::empty::<&str>()) + } + + fn run_notmuch(&self, args: I) -> Result> + where + I: IntoIterator, + S: AsRef, + { + let mut cmd = Command::new("notmuch"); + let cmd = cmd.arg("--config").arg(&self.config_path); + let cmd = cmd.args(args); + dbg!(&cmd); + let out = cmd.output()?; + Ok(out.stdout) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn new() -> Result<()> { + let nm = Notmuch::with_config("testdata/notmuch.config"); + nm.new()?; + let output = nm.no_args()?; + let s = String::from_utf8_lossy(&output); + assert!( + s.contains("Notmuch is configured and appears to have a database. Excellent!"), + "output:\n```\n{}```", + s + ); + Ok(()) + } }