(patch: &str, mode: ParseMode)
| 152 | } |
| 153 | |
| 154 | fn parse_patch_text(patch: &str, mode: ParseMode) -> Result<ApplyPatchArgs, ParseError> { |
| 155 | let lines: Vec<&str> = patch.trim().lines().collect(); |
| 156 | let lines: &[&str] = match check_patch_boundaries_strict(&lines) { |
| 157 | Ok(()) => &lines, |
| 158 | Err(e) => match mode { |
| 159 | ParseMode::Strict => { |
| 160 | return Err(e); |
| 161 | } |
| 162 | ParseMode::Lenient => check_patch_boundaries_lenient(&lines, e)?, |
| 163 | }, |
| 164 | }; |
| 165 | |
| 166 | let mut hunks: Vec<Hunk> = Vec::new(); |
| 167 | // The above checks ensure that lines.len() >= 2. |
| 168 | let last_line_index = lines.len().saturating_sub(1); |
| 169 | let mut remaining_lines = &lines[1..last_line_index]; |
| 170 | let mut line_number = 2; |
| 171 | while !remaining_lines.is_empty() { |
| 172 | let (hunk, hunk_lines) = parse_one_hunk(remaining_lines, line_number)?; |
| 173 | hunks.push(hunk); |
| 174 | line_number += hunk_lines; |
| 175 | remaining_lines = &remaining_lines[hunk_lines..] |
| 176 | } |
| 177 | let patch = lines.join("\n"); |
| 178 | Ok(ApplyPatchArgs { hunks, patch }) |
| 179 | } |
| 180 | |
| 181 | /// Checks the start and end lines of the patch text for `apply_patch`, |
| 182 | /// returning an error if they do not match the expected markers. |
no test coverage detected