-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcounts.rs
More file actions
95 lines (83 loc) · 2.06 KB
/
counts.rs
File metadata and controls
95 lines (83 loc) · 2.06 KB
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
use parsepatch::{BinaryHunk, Diff, FileMode, FileOp, Patch};
use pyo3::prelude::PyDictMethods;
use pyo3::types::PyDict;
use pyo3::{Bound, IntoPyObject, Py, PyAny, PyResult, Python};
pub struct PyDiff<'a> {
py: Python<'a>,
diff: Bound<'a, PyDict>,
add: u32,
del: u32,
}
impl<'a> PyDiff<'a> {
fn new(py: Python<'a>) -> Self {
PyDiff {
py,
diff: PyDict::new(py),
add: 0,
del: 0,
}
}
}
pub struct PyPatch<'a> {
py: Python<'a>,
diffs: Vec<PyDiff<'a>>,
}
impl<'a> PyPatch<'a> {
pub fn new(py: Python) -> PyPatch {
PyPatch {
py,
diffs: Vec::new(),
}
}
}
impl<'a> Patch<PyDiff<'a>> for PyPatch<'a> {
fn new_diff(&mut self) -> &mut PyDiff<'a> {
self.diffs.push(PyDiff::new(self.py));
self.diffs.last_mut().unwrap()
}
fn close(&mut self) {}
}
impl<'a> PyPatch<'a> {
pub fn get_result(mut self) -> PyResult<Py<PyAny>> {
let py = self.py;
let diffs: Vec<Py<PyAny>> = self
.diffs
.drain(..)
.map(move |x| {
x.diff.set_item("added_lines", x.add).unwrap();
x.diff.set_item("deleted_lines", x.del).unwrap();
x.diff.into_any().unbind()
})
.collect();
Ok(diffs.into_pyobject(py)?.into_any().unbind())
}
}
impl<'a> Diff for PyDiff<'a> {
fn set_info(
&mut self,
old_name: &str,
new_name: &str,
op: FileOp,
binary_sizes: Option<Vec<BinaryHunk>>,
file_mode: Option<FileMode>,
) {
crate::common::set_info(
&self.diff,
old_name,
new_name,
op,
binary_sizes,
file_mode,
&self.py,
);
}
fn add_line(&mut self, old_line: u32, new_line: u32, _line: &[u8]) {
if old_line == 0 {
self.add += 1;
} else if new_line == 0 {
self.del += 1;
}
}
fn close(&mut self) {}
fn new_hunk(&mut self) {}
}