tyt_material/
dependencies_impl.rs1use crate::{Dependencies, Error, Result};
2use std::{
3 ffi::OsStr,
4 fs,
5 path::{Path, PathBuf},
6};
7
8#[derive(Clone, Copy, Debug, Default)]
9pub struct DependenciesImpl;
10
11impl Dependencies for DependenciesImpl {
12 fn copy_file<P1: AsRef<Path>, P2: AsRef<Path>>(&self, from: P1, to: P2) -> Result<()> {
13 fs::copy(from.as_ref(), to.as_ref())?;
14 Ok(())
15 }
16
17 fn create_temp_dir(&self) -> Result<PathBuf> {
18 Ok(tyt_injection::create_temp_dir()?)
19 }
20
21 fn exec_magick<I, S>(&self, args: I) -> Result<Vec<u8>>
22 where
23 I: IntoIterator<Item = S>,
24 S: AsRef<OsStr>,
25 {
26 tyt_injection::exec_map("magick", args, Error::IO, Error::Magick)
27 }
28
29 fn glob_single_match(&self, pattern: &str) -> Result<PathBuf> {
30 let mut matches = Vec::new();
31 for entry in glob::glob(pattern)
32 .map_err(|e| Error::Glob(format!("invalid glob pattern '{pattern}': {e}")))?
33 {
34 matches
35 .push(entry.map_err(|e| Error::Glob(format!("error reading glob result: {e}")))?);
36 }
37
38 match matches.len() {
39 0 => Err(Error::Glob(format!("missing file matching: {pattern}"))),
40 1 => Ok(matches.into_iter().next().unwrap()),
41 n => {
42 let mut msg = format!("multiple files ({n}) match '{pattern}':");
43 for f in &matches {
44 msg.push_str(&format!("\n {}", f.display()));
45 }
46 Err(Error::Glob(msg))
47 }
48 }
49 }
50
51 fn remove_dir_all<P: AsRef<Path>>(&self, path: P) -> Result<()> {
52 Ok(tyt_injection::remove_dir_all(path.as_ref())?)
53 }
54
55 fn write_stdout(&self, contents: &[u8]) -> Result<()> {
56 Ok(tyt_injection::write_stdout(contents)?)
57 }
58}