summaryrefslogtreecommitdiffstats
path: root/src/finder.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2022-11-04 18:48:18 -0500
committerToby Vincent <tobyv13@gmail.com>2022-11-04 18:48:18 -0500
commitc8681e50194855a2ad36bf984866a30cfcfb8ec9 (patch)
tree491608d44b0806d558dd5abf42bc53de9e9b7b0b /src/finder.rs
parentc81eb9a1a542cc058960cf4eab12fc70bc2a81eb (diff)
refactor: move run to main.rs and impl output from Finder
Diffstat (limited to 'src/finder.rs')
-rw-r--r--src/finder.rs80
1 files changed, 49 insertions, 31 deletions
diff --git a/src/finder.rs b/src/finder.rs
index 688009c..db92c83 100644
--- a/src/finder.rs
+++ b/src/finder.rs
@@ -1,8 +1,9 @@
use std::{
io::Write,
- ops::Deref,
+ ops::{Deref, DerefMut},
+ os::unix::prelude::OsStrExt,
path::PathBuf,
- process::{Child, Command, Stdio},
+ process::{Child, Command, Output, Stdio},
};
pub use config::Config;
@@ -11,45 +12,62 @@ pub use error::{Error, Result};
mod config;
mod error;
-pub struct Finder {
- pub(crate) child: Child,
+pub struct Finder(Child);
+
+impl Finder {
+ pub fn new(config: &Config) -> Result<Self> {
+ 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<V>(&mut self, directories: V) -> Result<()>
+ where
+ V: IntoIterator<Item = PathBuf>,
+ {
+ 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<Output> {
+ self.into_inner().wait_with_output().map_err(From::from)
+ }
}
impl Deref for Finder {
type Target = Child;
fn deref(&self) -> &Self::Target {
- &self.child
+ &self.0
}
}
-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()?,
- })
+impl DerefMut for Finder {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.0
}
+}
- 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)
+impl From<Child> for Finder {
+ fn from(value: Child) -> Self {
+ Self(value)
+ }
+}
+
+impl From<Finder> for Child {
+ fn from(value: Finder) -> Child {
+ value.0
}
}