| Crates.io | smart-patcher |
| lib.rs | smart-patcher |
| version | 0.5.2 |
| created_at | 2025-07-20 11:10:06.644444+00 |
| updated_at | 2026-01-05 09:18:12.673154+00 |
| description | Patcher based on rules |
| homepage | |
| repository | https://github.com/impulse-sw/smart-patcher |
| max_upload_size | |
| id | 1760992 |
| size | 99,072 |
Patcher based on rules. Written for Deployer.
Features:
Install the Deployer and run:
depl run
Or build by cargo:
cargo install --path .
See the patch examples in examples folder. You can run tests with them by Deployer.
The usage is easy:
smart-patcher test {patch-file.json} {folder-to-patch} # to test if the patch is correct
smart-patcher apply {patch-file.json} {folder-to-patch} # to apply the patch
If you use any scripts, make sure that the path to the script files is relative to the patch file.
Also you can use smart-patcher as library and even specify needed features:
[dependencies]
smart-patcher = { version = "0.5", features = ["lua", "rhai"] }
To write a patch you must define:
Typical patch file looks like this:
{
"patches": [
{
"files": [...],
"decoder": ...,
"encoder": ...,
"patch_area": [...],
"replace": ...,
"insert": ...
},..
]
}
There are two types of select rules: just and re:
[
{
"just": "test_v5.docx"
},
{
"re": ".*\\.docx"
}
]
For match just rule, file path has to end with just value (e.g., tests/test_v5.docx). For re rule, file path has to match with Rust regular expression (read regex crate documentation).
There are several rule types to cut text sections step by step:
contains - if file isn't contain the text described by contains value's regular expression, the patch is not applied to itnot_contains - nearly the samebefore and after - cuts the patchable area before and after specified regular expression matchescursor_at_begin and cursor_at_end - moves the inplace cursor at begin or at end of the text areafind_by_{lua,sh,rhai} - cuts the section by Lua/Rhai or shell scriptThis is how a step by step set looks like:
{
"patch_area": [
{
"contains": "file must contain this text to apply the patch"
},
{
"before": "we select all before this text..."
},
{
"after": "...but after this."
},
{
"find_by_sh": "some_script_afterall.py"
}
]
}
Script examples can be found at tests folder.
replace and insert content{
"replace": {
"from_to": [
"some text to be replaced",
"some resulting text"
]
},
"insert": "some text at cursor"
}
insert value will be inserted at begin or end of selected text area. If the last file section rule was before, cursor will be at the end of section; otherwise it will be at the begin of section, if you're not using the scripts. Any script can specify cursot_at_end = True|False as third return value of the find function (e.g. see tests/test_v4.py and examples/patch4.json).
replace value will perform the replacement only and only if selected text area isn't empty. Simple from_to form just replace all matches of a first text with a second text. regex_to form use regular expressions to capture text and groups and replace with another text. But you can specify by_{lua,python,rhai} form:
{
"replace": {
"by_rhai": "some_script.rhai"
}
}
For example, see tests/test_v8.py.
decoder and encoder contentSee the tests/test_v5.py example (and tests/test_v5.docx file). This is simple example how you can transform your Microsoft Word document - and any other type of files - to text and vice versa to patch the content you need.
{
"decoder": {
"sh": "tests/test_v5.py"
},
"encoder": {
"sh": "tests/test_v5.py"
},
}
You can also specify lua and rhai values instead of sh.
There is an examples folder with example patches and tests folder with scripts and test files. Run any example in test mode with command:
smart-patcher test examples/patch4.json .
If you need some verbose information about patch, set SMPTCHVERBOSE environment variable to 1:
SMPTCHVERBOSE=1 smart-patcher test examples/patch4.json .