Skip to main content

pipi/generator/executer/
mod.rs

1//! This module defines error handling and the [`Executer`] trait
2
3use crate::settings::Settings;
4mod filesystem;
5mod inmem;
6use std::path::{Path, PathBuf};
7
8pub use filesystem::FileSystem;
9pub use inmem::Inmem;
10#[cfg(test)]
11use mockall::{automock, predicate::*};
12
13pub type Result<T> = std::result::Result<T, Error>;
14
15#[derive(thiserror::Error, Debug)]
16pub enum Error {
17    #[error("{0}")]
18    Message(String),
19
20    #[error(transparent)]
21    TemplateEngine(#[from] Box<rhai::EvalAltResult>),
22
23    #[error(transparent)]
24    FS(#[from] fs_extra::error::Error),
25
26    #[error(transparent)]
27    Template(#[from] tera::Error),
28}
29impl Error {
30    /// Creates a new error with a custom message.
31    pub fn msg<S: Into<String>>(msg: S) -> Self {
32        Self::Message(msg.into())
33    }
34}
35
36#[cfg_attr(test, automock)]
37pub trait Executer: Send + Sync {
38    /// Copies a single file from the specified path.
39    ///
40    /// # Errors
41    ///
42    /// Returns an error if the file cannot be copied, such as if the path is
43    /// invalid or if a file system error occurs.
44    fn copy_file(&self, path: &Path) -> Result<PathBuf>;
45
46    /// Copies a single file from the specified path.
47    ///
48    /// # Errors
49    ///
50    /// Returns an error if the file cannot be copied, such as if the path is
51    /// invalid or if a file system error occurs.
52    fn create_file(&self, path: &Path, content: String) -> Result<PathBuf>;
53
54    /// Copies an entire directory from the specified path.
55    ///
56    /// # Errors
57    ///
58    /// Returns an error if the directory cannot be copied, such as if the path
59    /// is invalid or if a file system error occurs.
60    fn copy_dir(&self, path: &Path) -> Result<()>;
61
62    /// Copies a template file from the specified path, applying settings.
63    ///
64    /// # Errors
65    ///
66    /// Returns an error if the template cannot be copied or if any
67    /// settings-related error occurs.
68    fn copy_template(&self, path: &Path, data: &Settings) -> Result<()>;
69
70    /// Copies an entire template directory from the specified path, applying
71    /// settings.
72    ///
73    /// # Errors
74    ///
75    /// Returns an error if the template directory cannot be copied or if any
76    /// settings-related error occurs.
77    fn copy_template_dir(&self, path: &Path, data: &Settings) -> Result<()>;
78}