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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use pest::{iterators::Pair, Parser};
use crate::error::Error;
#[derive(Parser)]
#[grammar = "../peg/patch.peg"]
pub struct PatchProcessor {
text: Vec<String>,
patch: Patch,
}
#[allow(dead_code)]
pub struct Patch {
pub input: String,
pub output: String,
pub contexts: Vec<Context>,
}
pub type PatchResult<T> = Result<T, Error>;
pub struct Context {
pub header: ContextHeader,
pub data: Vec<PatchLine>,
}
#[derive(Default)]
pub struct ContextHeader {
pub file1_l: usize,
pub file1_s: usize,
pub file2_l: usize,
pub file2_s: usize,
}
pub enum PatchLine {
Context(String),
Insert(String),
Delete(String),
}
impl PatchProcessor {
pub fn converted(text: Vec<String>, patch: &str) -> PatchResult<Self> {
Ok(Self {
text,
patch: Self::convert(patch)?,
})
}
pub fn process(&self) -> PatchResult<Vec<String>> {
let mut file2_text = Vec::new();
let mut file1_ptr: usize = 0;
for context in &self.patch.contexts {
for i in file1_ptr..context.header.file1_l {
file2_text.push(
self.text
.get(i)
.ok_or_else(|| Error::AbruptInput(i))?
.to_owned(),
);
}
file1_ptr = context.header.file1_l;
for line in &context.data {
match line {
PatchLine::Context(ref data) => {
if self
.text
.get(file1_ptr)
.ok_or_else(|| Error::AbruptInput(file1_ptr))?
!= data
{
return Err(Error::PatchInputMismatch(file1_ptr));
}
file2_text.push(data.to_owned());
file1_ptr += 1;
}
PatchLine::Delete(ref data) => {
if self
.text
.get(file1_ptr)
.ok_or_else(|| Error::AbruptInput(file1_ptr))?
!= data
{
return Err(Error::PatchInputMismatch(file1_ptr));
}
file1_ptr += 1;
}
PatchLine::Insert(ref data) => {
file2_text.push(data.to_owned());
}
}
}
}
for i in file1_ptr..self.text.len() {
file2_text.push(
self.text
.get(i)
.ok_or_else(|| Error::AbruptInput(i))?
.to_owned(),
);
}
Ok(file2_text)
}
pub fn convert(patch: &str) -> PatchResult<Patch> {
let peg_patch = Self::parse(Rule::patch, patch)?
.next()
.ok_or(Error::NotFound("patch"))?;
let mut contexts = Vec::new();
let mut input = None;
let mut output = None;
for patch_element in peg_patch.into_inner() {
match patch_element.as_rule() {
Rule::file1_header => {
for header_element in patch_element.into_inner() {
if let Rule::path = header_element.as_rule() {
input = Some(header_element.as_span().as_str().to_owned());
}
}
}
Rule::file2_header => {
for header_element in patch_element.into_inner() {
if let Rule::path = header_element.as_rule() {
output = Some(header_element.as_span().as_str().to_owned());
}
}
}
Rule::context => {
let mut peg_context = patch_element.into_inner();
let context_header = peg_context
.next()
.ok_or(Error::NotFound("context_header"))?;
let context_header = if let Rule::context_header = context_header.as_rule() {
Self::get_context_header(context_header)?
} else {
return Err(Error::MalformedPatch(
"Context header is not at the start of a context",
));
};
let mut context = Context {
header: context_header,
data: Vec::new(),
};
for line in peg_context {
match line.as_rule() {
Rule::line_context => context
.data
.push(PatchLine::Context(line.as_span().as_str().to_owned())),
Rule::line_deleted => context
.data
.push(PatchLine::Delete(line.as_span().as_str().to_owned())),
Rule::line_inserted => context
.data
.push(PatchLine::Insert(line.as_span().as_str().to_owned())),
_ => {}
}
}
contexts.push(context);
}
_ => {}
}
}
let input = input.ok_or_else(|| Error::NotFound("path (input)"))?;
let output = output.ok_or_else(|| Error::NotFound("path (output)"))?;
let patch = Patch {
input,
output,
contexts,
};
Ok(patch)
}
fn get_context_header(header: Pair<'_, Rule>) -> PatchResult<ContextHeader> {
let mut output = ContextHeader::default();
for header_element in header.into_inner() {
match header_element.as_rule() {
Rule::file1_l => output.file1_l = header_element.as_span().as_str().parse()?,
Rule::file1_s => output.file1_s = header_element.as_span().as_str().parse()?,
Rule::file2_l => output.file2_l = header_element.as_span().as_str().parse()?,
Rule::file2_s => output.file2_s = header_element.as_span().as_str().parse()?,
_ => {}
}
}
output.file1_l -= 1;
output.file2_l -= 1;
Ok(output)
}
}