Parse @@ -old_start,old_count +new_start,new_count @@ line
(header: &str)
| 93 | |
| 94 | /// Parse @@ -old_start,old_count +new_start,new_count @@ line |
| 95 | fn parse_hunk_header(header: &str) -> Result<usize, String> { |
| 96 | // Example: @@ -1,3 +1,3 @@ |
| 97 | let stripped = header |
| 98 | .strip_prefix("@@") |
| 99 | .and_then(|s| s.find("@@").map(|end| &s[..end])) |
| 100 | .ok_or_else(|| format!("Invalid hunk header: {}", header))? |
| 101 | .trim(); |
| 102 | |
| 103 | // Parse -old_start part |
| 104 | let parts: Vec<&str> = stripped.split_whitespace().collect(); |
| 105 | if parts.is_empty() { |
| 106 | return Err(format!("Invalid hunk header: {}", header)); |
| 107 | } |
| 108 | |
| 109 | let old_part = parts[0] |
| 110 | .strip_prefix('-') |
| 111 | .ok_or_else(|| format!("Invalid old range in hunk header: {}", header))?; |
| 112 | |
| 113 | let old_start: usize = old_part |
| 114 | .split(',') |
| 115 | .next() |
| 116 | .unwrap_or("1") |
| 117 | .parse() |
| 118 | .map_err(|_| format!("Invalid old start line in hunk header: {}", header))?; |
| 119 | |
| 120 | Ok(old_start) |
| 121 | } |
| 122 | |
| 123 | /// Apply hunks to file content. Hunks are applied bottom-to-top to preserve line numbers. |
| 124 | fn apply_hunks(content: &str, hunks: &[Hunk]) -> Result<String, String> { |
no test coverage detected