Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diff null sources #256

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 22 additions & 3 deletions src/bindings/text/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Local>) -> String {
d.format("%Y-%m-%d %H:%M:%S.%f %z").to_string()
}
Expand Down Expand Up @@ -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 {
Vec::new()
hedgar2017 marked this conversation as resolved.
Show resolved Hide resolved
} else {
read_file(&right).map_err(LuaError::external)?
};

let diff = diff(&left, &right, 3).map_err(LuaError::external)?;

Expand All @@ -69,4 +89,3 @@ pub fn init(lua: &Lua) -> crate::Result<()> {

Ok(())
}

45 changes: 45 additions & 0 deletions src/bindings/text/interdiff.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
2 changes: 2 additions & 0 deletions src/bindings/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -16,6 +17,7 @@ pub fn init(lua: &Lua) -> crate::Result<()> {
diff::init(&lua)?;
patch::init(&lua)?;
splitdiff::init(&lua)?;
interdiff::init(&lua)?;

Ok(())
}