diff options
Diffstat (limited to 'src/finder.rs')
-rw-r--r-- | src/finder.rs | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/src/finder.rs b/src/finder.rs index da88beb..688009c 100644 --- a/src/finder.rs +++ b/src/finder.rs @@ -1,15 +1,55 @@ -use std::ops::Deref; +use std::{ + io::Write, + ops::Deref, + path::PathBuf, + process::{Child, Command, Stdio}, +}; pub use config::Config; +pub use error::{Error, Result}; mod config; +mod error; -pub struct Finder(Config); +pub struct Finder { + pub(crate) child: Child, +} impl Deref for Finder { - type Target = Config; + type Target = Child; fn deref(&self) -> &Self::Target { - &self.0 + &self.child + } +} + +impl Finder { + pub(crate) fn new(config: &Config) -> Result<Self> { + Ok(Finder { + child: Command::new(&config.program) + .args(&config.args) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn()?, + }) + } + + pub(crate) fn run<V>(&mut self, directories: V) -> Result<()> + where + V: IntoIterator<Item = PathBuf>, + { + let stdin = directories + .into_iter() + .map(|p: PathBuf| p.to_string_lossy().into()) + .collect::<Vec<String>>() + .join("\n"); + + self.child + .stdin + .as_mut() + .ok_or(Error::Stdin)? + .deref() + .write_all(stdin.as_bytes()) + .map_err(Into::into) } } |