use std::{ io::Write, ops::{Deref, DerefMut}, os::unix::prelude::OsStrExt, path::PathBuf, process::{Child, Command, Output, Stdio}, }; pub use config::Config; pub use error::{Error, Result}; mod config; mod error; pub struct Finder(Child); impl Finder { pub fn new(config: &Config) -> Result { Command::new(&config.program) .args(&config.args) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .spawn() .map(Into::into) .map_err(Into::into) } pub fn into_inner(self) -> Child { self.0 } pub fn write_path_buf_vectored(&mut self, directories: V) -> Result<()> where V: IntoIterator, { let stdin = self.stdin.as_mut().ok_or(Error::Stdin)?; directories.into_iter().try_for_each(|path_buf| { stdin .write_all(path_buf.into_os_string().as_bytes()) .map_err(From::from) }) } pub fn wait_with_output(self) -> Result { self.into_inner().wait_with_output().map_err(From::from) } } impl Deref for Finder { type Target = Child; fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for Finder { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl From for Finder { fn from(value: Child) -> Self { Self(value) } } impl From for Child { fn from(value: Finder) -> Child { value.0 } }