Skip to content

Commit 3c5ef96

Browse files
committed
Move split_prefix_inclusive function to trait
1 parent cf47220 commit 3c5ef96

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

src/actions/prepare_commit_msg.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::openai::OpenAIClient;
2222
use crate::settings::Settings;
2323
use crate::summarize::SummarizationClient;
2424
use crate::util;
25+
use crate::util::SplitPrefixInclusive;
2526

2627
/// Splits the contents of a git diff by file.
2728
///
@@ -81,7 +82,7 @@ pub(crate) struct PrepareCommitMsgArgs {
8182
}
8283

8384
async fn get_commit_message(client: SummarizationClient, diff_as_input: &str) -> Result<String> {
84-
let file_diffs = util::split_prefix_inclusive(diff_as_input, "\ndiff --git ");
85+
let file_diffs = diff_as_input.split_prefix_inclusive("\ndiff --git ");
8586

8687
let mut set = JoinSet::new();
8788

src/util.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
/// TODO make trait of string
2-
/// Split string by prefix, including the prefix in the result.
3-
pub(crate) fn split_prefix_inclusive<'a>(string: &'a str, prefix: &str) -> Vec<&'a str> {
4-
let matches = string.match_indices(prefix).map(|(idx, _)| idx);
5-
let mut start = 0;
6-
let mut substrings = Vec::new();
7-
for idx in matches {
8-
if idx != start {
9-
substrings.push(&string[start..idx]);
10-
start = idx;
1+
pub(crate) trait SplitPrefixInclusive {
2+
fn split_prefix_inclusive<'a>(&'a self, prefix: &str) -> Vec<&'a str>;
3+
}
4+
5+
impl SplitPrefixInclusive for str {
6+
/// Split string by prefix, including the prefix in the result.
7+
fn split_prefix_inclusive<'a>(&'a self, prefix: &str) -> Vec<&'a str> {
8+
let matches = self.match_indices(prefix).map(|(idx, _)| idx);
9+
let mut start = 0;
10+
let mut substrings = Vec::new();
11+
for idx in matches {
12+
if idx != start {
13+
substrings.push(&self[start..idx]);
14+
start = idx;
15+
}
1116
}
12-
}
13-
substrings.push(&string[start..]);
17+
substrings.push(&self[start..]);
1418

15-
substrings
19+
substrings
20+
}
1621
}
1722

1823
/// Finds the file name from a diff. The diff is expected to be of the form
@@ -33,16 +38,16 @@ mod tests {
3338
fn test_split_prefix_inclusive() {
3439
let string = include_str!("../tests/data/example_1.diff");
3540
let pattern = "diff --git ";
36-
assert_eq!(split_prefix_inclusive(string, pattern).len(), 5);
41+
assert_eq!(string.split_prefix_inclusive(pattern).len(), 5);
3742
}
3843

3944
#[test]
4045
fn test_basic_split_prefix_inclusive() {
4146
let string = "x111x222x333";
4247
let pattern = "x";
43-
assert_eq!(split_prefix_inclusive(string, pattern).len(), 3);
48+
assert_eq!(string.split_prefix_inclusive(pattern).len(), 3);
4449
assert_eq!(
45-
split_prefix_inclusive(string, pattern),
50+
string.split_prefix_inclusive(pattern),
4651
&["x111", "x222", "x333"]
4752
);
4853
}
@@ -51,9 +56,9 @@ mod tests {
5156
fn test_basic_split_prefix_inclusive_2() {
5257
let string = "x111\nx222\nx333";
5358
let pattern = "\nx";
54-
assert_eq!(split_prefix_inclusive(string, pattern).len(), 3);
59+
assert_eq!(string.split_prefix_inclusive(pattern).len(), 3);
5560
assert_eq!(
56-
split_prefix_inclusive(string, pattern),
61+
string.split_prefix_inclusive(pattern),
5762
&["x111", "\nx222", "\nx333"]
5863
);
5964
}

0 commit comments

Comments
 (0)