txtpp/core/execute/
config.rs

1use crate::core::verbs;
2use std::path::PathBuf;
3
4/// Config for running txtpp
5///
6/// Use this to configure txtpp when calling it from the library
7/// # Example
8/// ```
9/// use txtpp::{Txtpp, Config, Verbosity};
10///
11/// // Use the default config
12/// let mut cfg = Config::default();
13/// // Change verbosity to verbose
14/// cfg.verbosity = Verbosity::Verbose;
15/// Txtpp::run(cfg).unwrap();
16/// ```
17#[derive(Debug, Clone)]
18pub struct Config {
19    /// Base directory for resolving paths in input. This is usually the current directory.
20    pub base_dir: PathBuf,
21    /// The shell command to use. (e.g. `bash -c`). Empty string for platform-specific default shell
22    pub shell_cmd: String,
23    /// The input file/directories
24    pub inputs: Vec<String>,
25    /// Whether to recursively process directories
26    pub recursive: bool,
27    /// The number of threads to use
28    pub num_threads: usize,
29    /// The mode. See [`Mode]
30    pub mode: Mode,
31    /// The verbosity. See [`Verbosity`]
32    pub verbosity: Verbosity,
33    /// If the output files should have trailing newline
34    pub trailing_newline: bool,
35}
36
37impl Default for Config {
38    /// Get the default config.
39    ///
40    /// This means:
41    /// - Running from the current directory
42    /// - Using the platform-specific default shell
43    /// - Processing the current directory
44    /// - Not recursively processing directories
45    /// - Using 4 threads
46    /// - Building output files
47    /// - Regular verbosity
48    /// - Output files have trailing newline
49    fn default() -> Self {
50        Self {
51            base_dir: PathBuf::from("."),
52            shell_cmd: "".to_string(),
53            inputs: vec![".".to_string()],
54            recursive: false,
55            num_threads: 4,
56            mode: Mode::Build,
57            verbosity: Verbosity::Normal,
58            trailing_newline: true,
59        }
60    }
61}
62
63/// The verbosity config options
64#[derive(Debug, PartialEq, Clone)]
65pub enum Verbosity {
66    Quiet,
67    Normal,
68    Verbose,
69}
70
71/// The mode config options
72#[derive(Debug, PartialEq, Clone)]
73pub enum Mode {
74    /// Build output files
75    ///
76    /// This is the default mode if no subcommand is specified from the command line.
77    /// For every `.txtpp` file found from the inputs, it will build the output file and any temporary file
78    /// the `.txtpp` may produce. Dependencies in `include` directives will be built automatically as well, even if
79    /// not specified in the inputs.
80    ///
81    /// Note that the temporary files are be rebuilt in the process in order to generate the fresh output.
82    /// However, temporary files that already have the same content will not be re-written to avoid changing the modification time.
83    Build,
84    /// Build output files with the --needed flag
85    InMemoryBuild,
86    /// Delete output files
87    ///
88    /// Remove the output file and any temporary file the `.txtpp` input files may produce.
89    /// Dependency is not automatically cleaned if they are not specified in the inputs.
90    Clean,
91    /// Verify output files are up to date
92    ///
93    /// In this mode, the output files will be compared against output from a fresh run.
94    /// The run will fail if any output is different from the fresh output.
95    ///
96    /// Note that the temporary files are rebuilt in the process in order to generate the fresh output.
97    /// However, temporary files that already have the same content will not be re-written to avoid changing the modification time.
98    Verify,
99}
100
101impl Mode {
102    pub fn processing_verb(&self) -> &'static str {
103        match self {
104            Self::Build | Self::InMemoryBuild => verbs::PROCESSING,
105            Self::Clean => verbs::CLEANING,
106            Self::Verify => verbs::VERIFYING,
107        }
108    }
109    pub fn processed_verb(&self) -> &'static str {
110        match self {
111            Self::Build | Self::InMemoryBuild => verbs::PROCESSED,
112            Self::Clean => verbs::CLEANED,
113            Self::Verify => verbs::VERIFIED,
114        }
115    }
116}