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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! patch-rs is a parser library for [Unified Format]
//! (https://www.gnu.org/software/diffutils/manual/html_node/Unified-Format.html#Unified-Format)
//! diffs.
//!
//! GVR also honed down the spec a bit more:
//! http://www.artima.com/weblogs/viewpost.jsp?thread=164293

#[macro_use]
extern crate nom;

use std::error::Error;
use nom::*;

#[derive(Debug, Eq, PartialEq)]
pub struct Patch {
    pub old: String,
    pub new: String,
}

#[derive(Debug)]
pub enum PatchError {
    ParseError,
}

impl std::fmt::Display for PatchError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            PatchError::ParseError =>
                write!(f, "Error while parsing"),
        }
    }
}

impl Error for PatchError {
    fn description(&self) -> &str {
        match *self {
            PatchError::ParseError =>
                "parse error",
        }
    }
}

pub fn parse(diff: &str) -> Result<Patch, PatchError> {
    match patch(diff.as_bytes()) {
        IResult::Done(_, (old, new)) =>
            Ok(Patch { old: old, new: new }),
        IResult::Incomplete(x) => {
            println!("incomplete {:?}", x);
            Err(PatchError::ParseError)
        },
        IResult::Error(x) => {
            if let Err::Position(_, chrs) = x {
                println!("chrs {:?}", std::str::from_utf8(chrs));
            }
            // println!("{:?}", x);
            Err(PatchError::ParseError)
        },
    }
}


named!(non_escape<char>,
    none_of!("\\\"\0\n\r\t")
);

named!(escape<char>,
    chain!(
        tag!("\\") ~
        c: one_of!("\\\"0nrtb"),
        || c
    )
);

named!(unescape<String>,
    map!(
        many1!( alt!( non_escape | escape ) ),
        |chars: Vec<char>| chars.into_iter().collect::<String>()
    )
);

#[test]
fn test_unescape() {
    assert_eq!(unescape("file \\\"name\\\"".as_bytes()),
        IResult::Done("".as_bytes(), "file \"name\"".to_string()));
}

named!(quoted<String>,
    chain!(
        tag!("\"") ~
        unescaped: unescape ~
        tag!("\"") ,
        || unescaped
    )
);

#[test]
fn test_quoted() {
    assert_eq!(quoted("\"file name\"".as_bytes()),
        IResult::Done("".as_bytes(), "file name".to_string()));
}


named!(bare<String>,
    map_res!(
        map_res!(
            is_not!(" \n"),
            std::str::from_utf8
        ),
        std::str::FromStr::from_str
    )
);

#[test]
fn test_bare() {
    assert_eq!(bare("file-name ".as_bytes()),
        IResult::Done(" ".as_bytes(), "file-name".to_string()));

    assert_eq!(bare("file-name\n".as_bytes()),
        IResult::Done("\n".as_bytes(), "file-name".to_string()));
}

named!(fname<String>, alt!(quoted | bare));

#[test]
fn test_fname() {
    assert_eq!(fname("asdf ".as_bytes()),
        IResult::Done(" ".as_bytes(), "asdf".to_string()));

    assert_eq!(fname("\"asdf\" ".as_bytes()),
        IResult::Done(" ".as_bytes(), "asdf".to_string()));

    assert_eq!(fname("\"a s\\\"df\" ".as_bytes()),
        IResult::Done(" ".as_bytes(), "a s\"df".to_string()));
}


named!(header_line_content<String>,
    chain!(
        filename: fname ~
        take_until!("\n") ,

        || filename
    )
);

#[test]
fn test_header_line_contents() {
    assert_eq!(header_line_content("lao 2002-02-21 23:30:39.942229878 -0800\n".as_bytes()),
        IResult::Done("\n".as_bytes(), "lao".to_string()));
}

named!(headers<(String, String)>,
    chain!(
            tag!("---") ~
            space ~
        oldfile: header_line_content ~
            newline ~
            tag!("+++") ~
            space ~
        newfile: header_line_content ~
            newline ,

        || (oldfile, newfile)
    )
);

#[test]
fn test_headers() {
    let sample = "\
--- lao 2002-02-21 23:30:39.942229878 -0800
+++ tzu 2002-02-21 23:30:50.442260588 -0800\n".as_bytes();
    assert_eq!(headers(sample),
        IResult::Done("".as_bytes(), ("lao".to_string(), "tzu".to_string())));

    let sample2 = "\
--- lao
+++ tzu\n".as_bytes();
    assert_eq!(headers(sample2),
        IResult::Done("".as_bytes(), ("lao".to_string(), "tzu".to_string())));
}

named!(range<u8>,
    chain!(
        digit ~
        opt!(chain!(
            tag!(",") ~
            digit ,
            || 0
        )) ,
        || 0
    )
);

named!(chunk_intro<u8>,
    chain!(
        tag!("@@ -") ~
        range ~
        tag!(" +") ~
        range ~
        tag!(" @@") ~
        newline ,
        || 0
    )
);

#[test]
fn test_chunk_intro() {
    let sample = "@@ -1,7 +1,6 @@\n".as_bytes();
    assert_eq!(chunk_intro(sample),
        IResult::Done("".as_bytes(), 0))
}

named!(chunk_line<u8>,
    chain!(
        one_of!(" +-") ~
        take_until_and_consume!("\n") ,
        || 0
    )
);

named!(chunk<u8>,
    chain!(
        chunk_intro ~
        many1!(chunk_line) ,
        || 0
    )
);

#[test]
fn test_chunk() {
    let sample = "\
@@ -1,7 +1,6 @@
-The Way that can be told of is not the eternal Way;
-The name that can be named is not the eternal name.
 The Nameless is the origin of Heaven and Earth;
-The Named is the mother of all things.
+The named is the mother of all things.
+
 Therefore let there always be non-being,
   so we may see their subtlety,
 And let there always be being,\n".as_bytes();
    assert_eq!(chunk(sample),
        IResult::Done("".as_bytes(), 0))
}

named!(chunks<Vec<u8> >, many0!(chunk));

named!(patch<(String, String)>,
    chain!(
        filenames: headers ~
        chunks ,
        || filenames
    )
);

#[test]
fn test_patch() {
    // https://www.gnu.org/software/diffutils/manual/html_node/Example-Unified.html
    let sample = "\
--- lao 2002-02-21 23:30:39.942229878 -0800
+++ tzu 2002-02-21 23:30:50.442260588 -0800
@@ -1,7 +1,6 @@
-The Way that can be told of is not the eternal Way;
-The name that can be named is not the eternal name.
 The Nameless is the origin of Heaven and Earth;
-The Named is the mother of all things.
+The named is the mother of all things.
+
 Therefore let there always be non-being,
   so we may see their subtlety,
 And let there always be being,
@@ -9,3 +8,6 @@
 The two are the same,
 But after they are produced,
   they have different names.
+They both may be called deep and profound.
+Deeper and more profound,
+The door of all subtleties!\n";

    assert_eq!(patch(sample.as_bytes()),
        IResult::Done("".as_bytes(), ("lao".to_string(), "tzu".to_string())));
}