Parse a unified diff into hunks
(diff: &str)
| 21 | |
| 22 | /// Parse a unified diff into hunks |
| 23 | fn parse_hunks(diff: &str) -> Result<Vec<Hunk>, String> { |
| 24 | let mut hunks = Vec::new(); |
| 25 | let lines: Vec<&str> = diff.lines().collect(); |
| 26 | let mut i = 0; |
| 27 | |
| 28 | while i < lines.len() { |
| 29 | let line = lines[i]; |
| 30 | |
| 31 | // Look for @@ header |
| 32 | if line.starts_with("@@") { |
| 33 | let hunk = parse_single_hunk(&lines, &mut i)?; |
| 34 | hunks.push(hunk); |
| 35 | } else { |
| 36 | i += 1; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | if hunks.is_empty() { |
| 41 | return Err("No @@ hunk headers found in diff".to_string()); |
| 42 | } |
| 43 | |
| 44 | Ok(hunks) |
| 45 | } |
| 46 | |
| 47 | fn parse_single_hunk(lines: &[&str], i: &mut usize) -> Result<Hunk, String> { |
| 48 | let header = lines[*i]; |