1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use std::any::Any;
use std::path::Path;
use super::itertools::*;
use super::diff;
use diff::Result::*;
use super::libloading::{self, Library, Symbol};
use super::{ExtensionPoint, Provenance};
use Provenance::*;
pub trait Language: ExtensionPoint {
fn diff(&self, old: String, new: String) -> Vec<(String, Provenance)>;
}
impl<L: Language + ?Sized> Language for Box<L> {
fn diff(&self, old: String, new: String) -> Vec<(String, Provenance)> { self.as_ref().diff(old, new) }
}
#[derive(Eq,Ord,PartialEq,PartialOrd,Hash,Clone,Copy,Default,Debug)]
pub struct SimpleLinewise;
impl ExtensionPoint for SimpleLinewise {
fn name(&self) -> String { "simple-linewise".to_string() }
fn description(&self) -> String { "A “language” that implements a naïve line-by-line diff".to_string() }
}
impl Language for SimpleLinewise {
fn diff(&self, old: String, new: String) -> Vec<(String, Provenance)> {
diff::lines(&old, &new)
.into_iter()
.map(|cr| match cr {
Left(x) => (x.to_string() + "\n", Old),
Right(x) => (x.to_string() + "\n", New),
Both(x, _) => (x.to_string() + "\n", Shared),
})
.coalesce(|(x, sx), (y, sy)| if sx == sy { Ok((x + &y, sx)) } else { Err(((x, sx), (y, sy))) })
.collect()
}
}
#[derive(Eq,Ord,PartialEq,PartialOrd,Hash,Clone,Copy,Default,Debug)]
pub struct SimpleCharwise;
impl ExtensionPoint for SimpleCharwise {
fn name(&self) -> String { "simple-charwise".to_string() }
fn description(&self) -> String { "A “language” that implements a naïve character-by-character diff".to_string() }
}
impl Language for SimpleCharwise {
fn diff(&self, old: String, new: String) -> Vec<(String, Provenance)> {
use diff::Result::*;
use Provenance::*;
diff::chars(&old, &new)
.into_iter()
.map(|cr| match cr {
Left(x) => (x.to_string(), Old),
Right(x) => (x.to_string(), New),
Both(x, _) => (x.to_string(), Shared),
})
.coalesce(|(x, sx), (y, sy)| if sx == sy { Ok((x + &y, sx)) } else { Err(((x, sx), (y, sy))) })
.collect()
}
}
#[derive(Debug)]
pub struct RawPluginLanguage {
lib: Library,
metadata: Box<Any>,
}
impl RawPluginLanguage {
pub fn load(path: &Path) -> libloading::Result<RawPluginLanguage> {
Library::new(path).map(|lib| {
let data: Box<Any>;
if let Ok(raw_fn) = unsafe { lib.get::<fn() -> Box<Any>>(b"deng_plugin_initialize") } {
data = raw_fn();
} else {
data = Box::new(());
}
RawPluginLanguage { lib: lib, metadata: data }
})
}
}
impl Drop for RawPluginLanguage {
fn drop(&mut self) {
if let Ok(raw_fn) = unsafe { self.lib.get::<fn(&Any)>(b"deng_plugin_deinitialize") } {
raw_fn(self.metadata.as_ref());
}
}
}
impl ExtensionPoint for RawPluginLanguage {
fn name(&self) -> String {
let raw_fn: Symbol<fn(&Any) -> String> = unsafe { self.lib.get(b"deng_plugin_name") }.expect("error in loading raw plugin");
return raw_fn(self.metadata.as_ref());
}
fn description(&self) -> String {
let raw_fn: Symbol<fn(&Any) -> String> = unsafe { self.lib.get(b"deng_plugin_description") }.expect("error in loading raw plugin");
return raw_fn(self.metadata.as_ref());
}
}
impl Language for RawPluginLanguage {
fn diff(&self, old: String, new: String) -> Vec<(String, Provenance)> {
let raw_fn: Symbol<fn(&Any, &str, &str) -> Vec<(String, Provenance)>> = unsafe { self.lib.get(b"deng_plugin_diff") }
.expect("error in loading raw plugin");
return raw_fn(self.metadata.as_ref(), &old, &new);
}
}