diff --git a/Cargo.lock b/Cargo.lock index e71823d..a066466 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1514,6 +1514,18 @@ dependencies = [ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "patch-rs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", + "failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "failure_derive 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "pest 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "pest_derive 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "percent-encoding" version = "1.0.1" @@ -2576,6 +2588,7 @@ dependencies = [ "log-panics 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "mime_guess 1.8.6 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.10.15 (registry+https://github.com/rust-lang/crates.io-index)", + "patch-rs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "rlua 0.15.3 (registry+https://github.com/rust-lang/crates.io-index)", "rlua_serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3162,6 +3175,7 @@ dependencies = [ "checksum owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49a4b8ea2179e6a2e27411d3bca09ca6dd630821cf6894c6c7c8467a8ee7ef13" "checksum parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0802bff09003b291ba756dc7e79313e51cc31667e94afbe847def490424cde5" "checksum parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad7f7e6ebdc79edff6fdcb87a55b620174f7a989e3eb31b65231f4af57f00b8c" +"checksum patch-rs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "36b17c85e29958514636d3071b17538241a2631c32fed7e6ff3b431d2bda4a1a" "checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" "checksum pest 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0fce5d8b5cc33983fc74f78ad552b5522ab41442c4ca91606e4236eb4b5ceefc" "checksum pest 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a677051ad923732bb5c70f2d45f8985a96e3eee2e2bff86697e3b11b0c3fcfde" diff --git a/Cargo.toml b/Cargo.toml index 4443eae..8985f0b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,6 +59,7 @@ blake2 = "0.8.0" tar = "0.4.20" xz2 = "0.1.6" unidiff = { git = "https://github.com/foundpatterns/difflib" } +patch-rs = "0.3" [dev-dependencies] diff --git a/src/bindings/app/mod.rs b/src/bindings/app/mod.rs index b7a3b1c..b53528d 100644 --- a/src/bindings/app/mod.rs +++ b/src/bindings/app/mod.rs @@ -3,6 +3,7 @@ pub mod log; pub mod markdown; pub mod tera; pub mod diff; +pub mod patch; use rlua::prelude::*; @@ -22,6 +23,7 @@ pub fn init(lua: &Lua) -> ::Result<()> { tantivy::init(&lua)?; tera::init(&lua)?; diff::init(&lua)?; + patch::init(&lua)?; Ok(()) } diff --git a/src/bindings/app/patch.rs b/src/bindings/app/patch.rs new file mode 100644 index 0000000..97d83b4 --- /dev/null +++ b/src/bindings/app/patch.rs @@ -0,0 +1,24 @@ +use rlua::{Lua, UserData, UserDataMethods}; + +struct PatchParser(patch_rs::PatchParser); + +impl UserData for PatchParser { + fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) { + methods.add_method("process", |_, this, _: ()| { + this.0.process().map_err(rlua::Error::external) + }); + } +} + +pub fn init(lua: &Lua) -> Result<(), rlua::Error> { + let module = lua.create_table()?; + module.set( + "new", + lua.create_function(|_, (text, patch): (Vec, String)| { + Ok(PatchParser(patch_rs::PatchParser::new(text, patch))) + })?, + )?; + let g = lua.globals(); + g.set("patch_parser", module)?; + Ok(()) +} diff --git a/src/bindings/string/stringset.rs b/src/bindings/string/stringset.rs index da98f03..af005f6 100644 --- a/src/bindings/string/stringset.rs +++ b/src/bindings/string/stringset.rs @@ -223,4 +223,4 @@ mod tests { symmetric(a, b) "#, None).unwrap(); } -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index 090a017..e09493f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,6 +38,7 @@ extern crate tar; extern crate xz2; extern crate unidiff; extern crate blake2; +extern crate patch_rs; #[cfg(feature = "tantivy_bindings")] extern crate tantivy;