diff options
author | Toby Vincent <tobyv13@gmail.com> | 2022-11-03 16:47:57 -0500 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2022-11-03 16:47:57 -0500 |
commit | c81eb9a1a542cc058960cf4eab12fc70bc2a81eb (patch) | |
tree | 8deff281230dbdedea29299060e214f1bb685cf7 /src/finder.rs | |
parent | 53d0cefcb1ca3527935479cc07a6f5fe5b1f8051 (diff) |
feat: impl finder
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) } } |