pub fn parse_single_patch(content: &str) -> Result<Patch, SingleParseError>Expand description
Parses a string containing a diff and returns a single Patch object.
This is a convenience function that wraps parse_auto() but enforces that the
input content results in exactly one Patch. It is useful when you expect
a diff for a single file and want to handle the “zero or many” cases as an error.
§Arguments
content- A string slice containing the text to parse. This can be a raw Unified Diff, a Markdown block, or a set of Conflict Markers.
§Errors
Returns a SingleParseError if:
- The underlying parsing fails (e.g., a diff block is missing a file header).
- No patches are found in the content.
- More than one patch is found in the content.
§Example
// --- Success Case ---
let diff_content = r#"
```diff
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,3 @@
fn main() {
- println!("Hello, world!");
+ println!("Hello, mpatch!");
}
```
"#;
let patch = parse_single_patch(diff_content).unwrap();
assert_eq!(patch.file_path.to_str(), Some("src/main.rs"));
// --- Error Case (Multiple Patches) ---
let multi_file_diff = r#"
```diff
--- a/file1.txt
+++ b/file1.txt
@@ -1 +1 @@
-a
+b
--- a/file2.txt
+++ b/file2.txt
@@ -1 +1 @@
-c
+d
```
"#;
let result = parse_single_patch(multi_file_diff);
assert!(matches!(result, Err(SingleParseError::MultiplePatchesFound(2))));