(lines: &[&str], i: &mut usize)
| 45 | } |
| 46 | |
| 47 | fn parse_single_hunk(lines: &[&str], i: &mut usize) -> Result<Hunk, String> { |
| 48 | let header = lines[*i]; |
| 49 | let old_start = parse_hunk_header(header)?; |
| 50 | |
| 51 | *i += 1; |
| 52 | |
| 53 | let mut removals = Vec::new(); |
| 54 | let mut additions = Vec::new(); |
| 55 | let mut context = Vec::new(); |
| 56 | let mut old_offset = 0; |
| 57 | |
| 58 | while *i < lines.len() { |
| 59 | let line = lines[*i]; |
| 60 | |
| 61 | if line.starts_with("@@") { |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | if let Some(content) = line.strip_prefix('-') { |
| 66 | removals.push(content.to_string()); |
| 67 | old_offset += 1; |
| 68 | } else if let Some(content) = line.strip_prefix('+') { |
| 69 | additions.push(content.to_string()); |
| 70 | } else if let Some(content) = line.strip_prefix(' ') { |
| 71 | context.push((old_offset, content.to_string())); |
| 72 | old_offset += 1; |
| 73 | } else if line.is_empty() { |
| 74 | // Treat empty line as context |
| 75 | context.push((old_offset, String::new())); |
| 76 | old_offset += 1; |
| 77 | } else { |
| 78 | // Unknown prefix, treat as context |
| 79 | context.push((old_offset, line.to_string())); |
| 80 | old_offset += 1; |
| 81 | } |
| 82 | |
| 83 | *i += 1; |
| 84 | } |
| 85 | |
| 86 | Ok(Hunk { |
| 87 | old_start, |
| 88 | removals, |
| 89 | additions, |
| 90 | context, |
| 91 | }) |
| 92 | } |
| 93 | |
| 94 | /// Parse @@ -old_start,old_count +new_start,new_count @@ line |
| 95 | fn parse_hunk_header(header: &str) -> Result<usize, String> { |
no test coverage detected