diff --git a/Cargo.lock b/Cargo.lock index 2aca437..67425ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1146,6 +1146,17 @@ name = "indexmap" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "interdiff-rs" +version = "0.2.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)", + "env_logger 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "patch-rs 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "iovec" version = "0.1.2" @@ -1711,11 +1722,13 @@ dependencies = [ [[package]] name = "patch-rs" -version = "0.5.5" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "pest 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "pest_derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2842,7 +2855,7 @@ dependencies = [ [[package]] name = "torchbear" -version = "0.11.9" +version = "0.11.10" dependencies = [ "actix 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", "actix-lua 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2865,11 +2878,12 @@ dependencies = [ "globwalk 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "human-panic 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "interdiff-rs 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "libm 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (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.16 (registry+https://github.com/rust-lang/crates.io-index)", - "patch-rs 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "patch-rs 0.6.2 (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)", @@ -3504,6 +3518,7 @@ dependencies = [ "checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" "checksum ignore 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ad03ca67dc12474ecd91fdb94d758cbd20cb4e7a78ebe831df26a9b7511e1162" "checksum indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d" +"checksum interdiff-rs 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "791bdfe28740c768a1f56b77fcf0a7489ba38e60ba34e7f9dd606ae89271893d" "checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08" "checksum ipconfig 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "08f7eadeaf4b52700de180d147c4805f199854600b36faa963d91114827b2ffc" "checksum itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b8467d9c1cebe26feb08c640139247fac215782d35371ade9a2136ed6085358" @@ -3568,7 +3583,7 @@ dependencies = [ "checksum parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337" "checksum parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad7f7e6ebdc79edff6fdcb87a55b620174f7a989e3eb31b65231f4af57f00b8c" "checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9" -"checksum patch-rs 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c3deb7dd96f6d65adf50fef5f6f3a1ceb61ea9fa97fb33ccbf14aa256220de2c" +"checksum patch-rs 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "acb08234420136e64b501853c784206c1e2f29c8f3240c6fc342c199642dcff1" "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.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "54f0c72a98d8ab3c99560bfd16df8059cc10e1f9a8e83e6e3b97718dd766e9c3" diff --git a/Cargo.toml b/Cargo.toml index 786a1a1..c2f5471 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,8 +51,9 @@ comrak = "0.4" serde_json = "1.0" serde_derive = "1.0" diff-rs = "0.2" -patch-rs = "0.5" +patch-rs = "0.6" splitdiff-rs = "0.4" +interdiff-rs = "0.2" select = "0.4" scl = { git = "https://github.com/foundpatterns/scl" } # string diff --git a/src/bindings/text/diff.rs b/src/bindings/text/diff.rs index b0b1668..bc89496 100644 --- a/src/bindings/text/diff.rs +++ b/src/bindings/text/diff.rs @@ -6,6 +6,11 @@ use std::{ use chrono::{DateTime, Local}; use diff_rs::diff; +#[cfg(target_os = "windows")] +const NULL_SOURCE: &str = "nul"; +#[cfg(target_os = "linux")] +const NULL_SOURCE: &str = "/dev/null"; + fn time_format(d: &DateTime) -> String { d.format("%Y-%m-%d %H:%M:%S.%f %z").to_string() } @@ -53,8 +58,23 @@ pub fn init(lua: &Lua) -> crate::Result<()> { format!("+++ {}\t{}", &right, time_format(&mtime(&right).map_err(LuaError::external)?)) ]; - let left = read_file(&left).map_err(LuaError::external)?; - let right = read_file(&right).map_err(LuaError::external)?; + if left == NULL_SOURCE && right == NULL_SOURCE { + return Err(rlua::Error::external(io::Error::new( + io::ErrorKind::InvalidInput, + "Both files cannot be null" + ))); + } + + let left = if left != NULL_SOURCE { + read_file(&left).map_err(LuaError::external)? + } else { + Vec::new() + }; + let right = if right != NULL_SOURCE { + read_file(&right).map_err(LuaError::external)? + } else { + Vec::new() + }; let diff = diff(&left, &right, 3).map_err(LuaError::external)?; @@ -69,4 +89,3 @@ pub fn init(lua: &Lua) -> crate::Result<()> { Ok(()) } - diff --git a/src/bindings/text/interdiff.rs b/src/bindings/text/interdiff.rs new file mode 100644 index 0000000..7f3c78e --- /dev/null +++ b/src/bindings/text/interdiff.rs @@ -0,0 +1,45 @@ +use rlua::Lua; + +use std::{io, fs}; +use patch_rs::{Patch, PatchProcessor}; + +#[cfg(target_os = "windows")] +const NULL_SOURCE: &str = "nul"; +#[cfg(target_os = "linux")] +const NULL_SOURCE: &str = "/dev/null"; + +pub fn init(lua: &Lua) -> crate::Result<()> { + let module = lua.create_table()?; + + module.set( + "interdiff", + lua.create_function(|_, (patch_1, patch_2): (String, String)| { + if patch_1 == NULL_SOURCE && patch_2 == NULL_SOURCE { + return Err(rlua::Error::external(io::Error::new( + io::ErrorKind::InvalidInput, + "Both patches cannot be null" + ))); + } + + let patch_1 = if patch_1 != NULL_SOURCE { + let patch_1 = fs::read_to_string(patch_1).map_err(rlua::Error::external)?; + PatchProcessor::convert(&patch_1).map_err(rlua::Error::external)? + } else { + Patch::default() + }; + + let patch_2 = if patch_2 != NULL_SOURCE { + let patch_2 = fs::read_to_string(patch_2).map_err(rlua::Error::external)?; + PatchProcessor::convert(&patch_2).map_err(rlua::Error::external)? + } else { + Patch::default() + }; + + Ok(interdiff_rs::interdiff(patch_1, patch_2, 3).to_string()) + })?, + )?; + + lua.globals().set("interdiff", module)?; + + Ok(()) +} diff --git a/src/bindings/text/mod.rs b/src/bindings/text/mod.rs index 54605df..45ca164 100644 --- a/src/bindings/text/mod.rs +++ b/src/bindings/text/mod.rs @@ -4,6 +4,7 @@ pub mod patch; pub mod scl; pub mod select; pub mod splitdiff; +pub mod interdiff; pub mod yaml; use rlua::prelude::*; @@ -16,6 +17,7 @@ pub fn init(lua: &Lua) -> crate::Result<()> { diff::init(&lua)?; patch::init(&lua)?; splitdiff::init(&lua)?; + interdiff::init(&lua)?; Ok(()) }