Skip to main content

smart_patcher/
lib.rs

1//! Patcher based on rules.
2//!
3//! Main features:
4//!
5//! - `check` command to test patch (featured by `tests`, enabled by default)
6//! - rules to select files and patch areas
7//! - file content encoders/decoders support
8//! - regex support
9//! - Python, Lua and Rhai scripts support (featured by `python`, `lua` and `rhai`)
10//!
11//! See [`PatchFile`] for further details.
12
13#![deny(warnings, clippy::todo, clippy::unimplemented)]
14
15pub(crate) mod cmd;
16pub(crate) mod interactive;
17pub(crate) mod patch;
18
19pub(crate) mod utils;
20
21pub use crate::cmd::*;
22pub use crate::patch::{AreaRule, DecodeBy, EncodeBy, FilePath, Patch, PatchFile, Replacer};
23pub use crate::utils::RegexIR;
24
25#[cfg(feature = "generate-examples")]
26pub use crate::patch::generate_examples;
27
28pub fn cli_exec(args: crate::cmd::Cli) -> anyhow::Result<()> {
29  match args.r#type {
30    SmartPatcherExecType::Apply(params) => {
31      let file = std::fs::File::open(params.patch)?;
32      let buf = std::io::BufReader::new(file);
33      let patches: PatchFile = serde_json::from_reader(buf)?;
34
35      let times = patches.patch(&params.work_at, &params.work_at)?;
36      println!("Patch applied {times} time(s).");
37    }
38    SmartPatcherExecType::Test(params) => {
39      let file = std::fs::File::open(params.patch)?;
40      let buf = std::io::BufReader::new(file);
41      let patches: PatchFile = serde_json::from_reader(buf)?;
42
43      let times = patches.test(&params.work_at, &params.work_at)?;
44      println!("Patch will be applied {times} time(s).");
45    }
46    #[cfg(feature = "generate-examples")]
47    SmartPatcherExecType::GenerateExamples => {
48      generate_examples()?;
49    }
50  }
51
52  Ok(())
53}