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
#[macro_use]
extern crate nom;
extern crate chrono;
use std::error::Error;
use nom::{IResult, Err};
pub use self::parser::{Patch, File, FileMetadata};
use self::parser::{patch};
mod parser;
#[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), hunks, no_newline)) =>
Ok(Patch {
old: old,
new: new,
hunks: hunks,
no_newline: no_newline,
}),
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));
}
Err(PatchError::ParseError)
},
}
}