-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommon.rs
More file actions
125 lines (118 loc) · 4.23 KB
/
common.rs
File metadata and controls
125 lines (118 loc) · 4.23 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
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
use parsepatch::{BinaryHunk, FileMode, FileOp};
use pyo3::prelude::*;
use pyo3::types::PyDict;
use pyo3::types::{PyByteArray, PyBytes, PyString};
use pyo3::Python;
pub(crate) enum Bytes<'a> {
Slice(&'a [u8]),
Vec(Vec<u8>),
}
#[inline(always)]
pub fn create_mode(old: Option<u32>, new: Option<u32>, py: &Python) -> Py<PyAny> {
let dict = PyDict::new(*py);
if let Some(old) = old {
dict.set_item("old", old).unwrap();
}
if let Some(new) = new {
dict.set_item("new", new).unwrap();
}
dict.into_any().unbind()
}
#[inline(always)]
pub fn create_file_mode(modes: Option<FileMode>, py: &Python) -> Py<PyAny> {
let dict = PyDict::new(*py);
if let Some(modes) = modes {
dict.set_item("old", modes.old).unwrap();
dict.set_item("new", modes.new).unwrap();
}
dict.into_any().unbind()
}
#[inline(always)]
pub fn create_bin_size(h: BinaryHunk, py: &Python) -> Py<PyAny> {
let x = match h {
BinaryHunk::Literal(s) => ("literal", s),
BinaryHunk::Delta(s) => ("delta", s),
};
(x.0, x.1).into_pyobject(*py).unwrap().into_any().unbind()
}
#[inline(always)]
pub fn set_info(
diff: &Bound<'_, PyDict>,
old_name: &str,
new_name: &str,
op: FileOp,
binary_sizes: Option<Vec<BinaryHunk>>,
file_mode: Option<FileMode>,
py: &Python,
) {
match op {
FileOp::New(m) => {
diff.set_item("new", true).unwrap();
diff.set_item("deleted", false).unwrap();
diff.set_item("copied_from", py.None()).unwrap();
diff.set_item("renamed_from", py.None()).unwrap();
diff.set_item("filename", new_name).unwrap();
diff.set_item("modes", create_mode(None, Some(m), py))
.unwrap();
}
FileOp::Deleted(m) => {
diff.set_item("new", false).unwrap();
diff.set_item("deleted", true).unwrap();
diff.set_item("copied_from", py.None()).unwrap();
diff.set_item("renamed_from", py.None()).unwrap();
diff.set_item("filename", old_name).unwrap();
diff.set_item("modes", create_mode(Some(m), None, py))
.unwrap();
}
FileOp::Renamed => {
diff.set_item("new", false).unwrap();
diff.set_item("deleted", false).unwrap();
diff.set_item("copied_from", py.None()).unwrap();
diff.set_item("renamed_from", old_name).unwrap();
diff.set_item("filename", new_name).unwrap();
diff.set_item("modes", create_file_mode(file_mode, py))
.unwrap();
}
FileOp::Copied => {
diff.set_item("new", false).unwrap();
diff.set_item("deleted", false).unwrap();
diff.set_item("copied_from", old_name).unwrap();
diff.set_item("renamed_from", py.None()).unwrap();
diff.set_item("filename", new_name).unwrap();
diff.set_item("modes", create_file_mode(file_mode, py))
.unwrap();
}
FileOp::None => {
diff.set_item("new", false).unwrap();
diff.set_item("deleted", false).unwrap();
diff.set_item("copied_from", py.None()).unwrap();
diff.set_item("renamed_from", py.None()).unwrap();
diff.set_item("filename", new_name).unwrap();
diff.set_item("modes", create_file_mode(file_mode, py))
.unwrap();
}
}
if let Some(mut binary_sizes) = binary_sizes {
diff.set_item("binary", true).unwrap();
let sizes: Vec<Py<PyAny>> = binary_sizes
.drain(..)
.map(move |x| create_bin_size(x, py))
.collect();
diff.set_item("binary_hunk_size", sizes).unwrap();
} else {
diff.set_item("binary", false).unwrap();
}
}
#[inline(always)]
pub(crate) fn get_bytes<'a>(py: Python<'a>, bytes: &'a Py<PyAny>) -> Option<Bytes<'a>> {
let bytes = bytes.bind(py);
if let Ok(bytes) = bytes.cast::<PyBytes>() {
Some(Bytes::Slice(bytes.as_bytes()))
} else if let Ok(bytes) = bytes.cast::<PyString>() {
Some(Bytes::Slice(bytes.to_str().unwrap().as_bytes()))
} else if let Ok(bytes) = bytes.cast::<PyByteArray>() {
Some(Bytes::Vec(bytes.to_vec()))
} else {
None
}
}