| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use std::collections::HashSet; |
| 6 | |
| 7 | const KEY: &str = "Co-authored-by"; |
| 8 | |
| 9 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 10 | pub struct CoAuthor { |
| 11 | pub name: String, |
| 12 | pub email: String, |
| 13 | } |
| 14 | |
| 15 | |
| 16 | pub fn co_authors(message: &str) -> Vec<CoAuthor> { |
| 17 | let lines = lines(message); |
| 18 | let mut seen = HashSet::new(); |
| 19 | lines[last_paragraph_start(&lines)..] |
| 20 | .iter() |
| 21 | .filter_map(|line| parse(line)) |
| 22 | .filter(|author| seen.insert(author.email.to_lowercase())) |
| 23 | .collect() |
| 24 | } |
| 25 | |
| 26 | |
| 27 | pub fn strip_co_authors(message: &str) -> String { |
| 28 | let lines = lines(message); |
| 29 | let (head, tail) = lines.split_at(last_paragraph_start(&lines)); |
| 30 | let kept: Vec<&str> = head |
| 31 | .iter() |
| 32 | .copied() |
| 33 | .chain( |
| 34 | tail.iter() |
| 35 | .copied() |
| 36 | .filter(|line| trailer_value(line).is_none()), |
| 37 | ) |
| 38 | .collect(); |
| 39 | kept.join("\n").trim_end().to_owned() |
| 40 | } |
| 41 | |
| 42 | |
| 43 | fn lines(message: &str) -> Vec<&str> { |
| 44 | message.trim_end().lines().collect() |
| 45 | } |
| 46 | |
| 47 | |
| 48 | fn last_paragraph_start(lines: &[&str]) -> usize { |
| 49 | lines |
| 50 | .iter() |
| 51 | .rposition(|line| line.trim().is_empty()) |
| 52 | .map_or(0, |i| i + 1) |
| 53 | } |
| 54 | |
| 55 | fn trailer_value(line: &str) -> Option<&str> { |
| 56 | let (key, value) = line.split_once(':')?; |
| 57 | key.trim().eq_ignore_ascii_case(KEY).then_some(value) |
| 58 | } |
| 59 | |
| 60 | |
| 61 | fn parse(line: &str) -> Option<CoAuthor> { |
| 62 | let value = trailer_value(line.trim())?.trim(); |
| 63 | let open = value.find('<')?; |
| 64 | let close = open + value[open..].find('>')?; |
| 65 | let email = value[open + 1..close].trim(); |
| 66 | if email.is_empty() { |
| 67 | return None; |
| 68 | } |
| 69 | let name = value[..open].trim(); |
| 70 | Some(CoAuthor { |
| 71 | name: if name.is_empty() { email } else { name }.to_owned(), |
| 72 | email: email.to_owned(), |
| 73 | }) |
| 74 | } |
| 75 | |
| 76 | #[cfg(test)] |
| 77 | mod tests { |
| 78 | use super::*; |
| 79 | |
| 80 | #[test] |
| 81 | fn co_authors_from_the_trailer_block_only() { |
| 82 | let msg = "Co-authored-by: Old <old@x.io>\n\nSubject\n\nBody\n\n\ |
| 83 | co-authored-by: Ann <ANN@x.io>\n\ |
| 84 | CO-AUTHORED-BY: Bob <bob@x.io> \n\ |
| 85 | Co-authored-by: Ann Again <ann@x.io>\n\ |
| 86 | Co-authored-by: No Email\n\ |
| 87 | Co-authored-by: Empty <>\n"; |
| 88 | let got = co_authors(msg); |
| 89 | assert_eq!( |
| 90 | got, |
| 91 | vec![ |
| 92 | CoAuthor { |
| 93 | name: "Ann".into(), |
| 94 | email: "ANN@x.io".into() |
| 95 | }, |
| 96 | CoAuthor { |
| 97 | name: "Bob".into(), |
| 98 | email: "bob@x.io".into() |
| 99 | }, |
| 100 | ] |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn strip_removes_trailers_in_last_paragraph() { |
| 106 | let msg = "Subject\n\nCo-authored-by: Mid <mid@x.io>\nKept\n\nSigned-off-by: Me\nCo-authored-by: Ann <ann@x.io>\n\n\n"; |
| 107 | assert_eq!( |
| 108 | strip_co_authors(msg), |
| 109 | "Subject\n\nCo-authored-by: Mid <mid@x.io>\nKept\n\nSigned-off-by: Me" |
| 110 | ); |
| 111 | } |
| 112 | } |