-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathutil.rs
89 lines (81 loc) · 2.61 KB
/
util.rs
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
pub(crate) static HTTP_USER_AGENT: &str =
concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);
pub(crate) trait SplitPrefixInclusive {
fn split_prefix_inclusive<'a>(&'a self, prefix: &str) -> Vec<&'a str>;
}
impl SplitPrefixInclusive for str {
/// Split string by prefix, including the prefix in the result.
fn split_prefix_inclusive<'a>(&'a self, prefix: &str) -> Vec<&'a str> {
let matches = self.match_indices(prefix).map(|(idx, _)| idx);
let mut start = 0;
let mut substrings = Vec::new();
for idx in matches {
if idx != start {
substrings.push(&self[start..idx]);
start = idx;
}
}
substrings.push(&self[start..]);
substrings
}
}
/// Finds the file name from a diff. The diff is expected to be of the form
/// "diff --git a/file_name b/file_name".
///
/// If the diff is not of the expected form, then None is returned.
pub(crate) fn get_file_name_from_diff(file_diff: &str) -> Option<&str> {
let (_, suffix) = file_diff.split_once("diff --git a/")?;
let (file_name, _) = suffix.split_once(' ')?;
Some(file_name)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_split_prefix_inclusive() {
let string = include_str!("../tests/data/example_1.diff");
let pattern = "diff --git ";
assert_eq!(string.split_prefix_inclusive(pattern).len(), 5);
}
#[test]
fn test_basic_split_prefix_inclusive() {
let string = "x111x222x333";
let pattern = "x";
assert_eq!(string.split_prefix_inclusive(pattern).len(), 3);
assert_eq!(
string.split_prefix_inclusive(pattern),
&["x111", "x222", "x333"]
);
}
#[test]
fn test_basic_split_prefix_inclusive_2() {
let string = "x111\nx222\nx333";
let pattern = "\nx";
assert_eq!(string.split_prefix_inclusive(pattern).len(), 3);
assert_eq!(
string.split_prefix_inclusive(pattern),
&["x111", "\nx222", "\nx333"]
);
}
#[test]
fn test_get_file_name_from_diff() {
assert_eq!(get_file_name_from_diff(""), None);
assert_eq!(get_file_name_from_diff("asdasdas"), None);
assert_eq!(get_file_name_from_diff("diff --git a/"), None);
assert_eq!(get_file_name_from_diff("diff --git b/"), None);
assert_eq!(
get_file_name_from_diff(
&r#"
diff --git a/foo b/foo
new file mode 100644
index 0000000..a51b2a6
--- /dev/null
+++ b/foo
@@ -0,0 +1 @@
+sadasdas
"#[1..]
),
Some("foo")
);
}
}