Debian Codegraph @

src/patchkit.rs

raw symbols SCIP
1 //! Parse the unified diff body of a `debian/patches/*` file into
2 //! [`patchkit`] structures.
3 //!
4 //! DEP-3 only specifies the header of a patch file; everything after it is an
5 //! ordinary (possibly multi-file) unified diff. This module splits a complete
6 //! patch file at [`crate::header_end`] and hands the remaining diff body to
7 //! [`patchkit::unified::parse_patches`], so callers get both the parsed DEP-3
8 //! header and the structured diff in one step.
9 //!
10 //! It is gated behind the `patchkit` feature.
11 //!
12 //! # Examples
13 //!
14 //! ```rust
15 //! let content = "Author: alice\n\
16 //! Description: fix a thing\n\
17 //! --- a/foo\n\
18 //! +++ b/foo\n\
19 //! @@ -1 +1 @@\n\
20 //! -old\n\
21 //! +new\n";
22 //! let (header, patches) = dep3::patchkit::parse_with_diff(content).unwrap();
23 //! assert_eq!(header.author(), Some("alice".to_string()));
24 //! assert_eq!(patches.len(), 1);
25 //! ```
26
27 use patchkit::unified::{parse_patches, PlainOrBinaryPatch};
28
29 /// An error returned while parsing a patch file into a header and a diff body.
30 #[derive(Debug)]
31 pub enum Error {
32 /// The DEP-3 header could not be parsed as a deb822 paragraph.
33 #[cfg(feature = "lossless")]
34 Header(deb822_lossless::ParseError),
35 /// The unified diff body could not be parsed.
36 Diff(patchkit::unified::Error),
37 }
38
39 #[cfg(feature = "lossless")]
40 impl From<deb822_lossless::ParseError> for Error {
41 fn from(e: deb822_lossless::ParseError) -> Self {
42 Error::Header(e)
43 }
44 }
45
46 impl From<patchkit::unified::Error> for Error {
47 fn from(e: patchkit::unified::Error) -> Self {
48 Error::Diff(e)
49 }
50 }
51
52 impl std::fmt::Display for Error {
53 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 match self {
55 #[cfg(feature = "lossless")]
56 Error::Header(e) => write!(f, "header: {}", e),
57 Error::Diff(e) => write!(f, "diff: {}", e),
58 }
59 }
60 }
61
62 impl std::error::Error for Error {}
63
64 /// Parse the unified diff body that follows a DEP-3 header.
65 ///
66 /// `content` is the whole patch file. The header is split off at
67 /// [`crate::header_end`] and discarded; only the diff body is parsed. Returns
68 /// the patches it contains, which may be empty when the file is header-only.
69 pub fn parse_diff(content: &str) -> Result<Vec<PlainOrBinaryPatch>, patchkit::unified::Error> {
70 let end = crate::header_end(content);
71 let body = &content[end..];
72 parse_patches(body.split_inclusive('\n').map(|l| l.as_bytes().to_vec())).collect()
73 }
74
75 /// Parse a complete `debian/patches/*` file into its DEP-3 header and the
76 /// patchkit-parsed diff body.
77 ///
78 /// Requires both the `lossless` and `patchkit` features.
79 #[cfg(feature = "lossless")]
80 pub fn parse_with_diff(
81 content: &str,
82 ) -> Result<(crate::lossless::PatchHeader, Vec<PlainOrBinaryPatch>), Error> {
83 let (header, _) = crate::lossless::PatchHeader::parse_relaxed(content)?;
84 let patches = parse_diff(content)?;
85 Ok((header, patches))
86 }
87
88 #[cfg(test)]
89 mod tests {
90 use super::*;
91
92 const PATCH: &str = "Author: alice\n\
93 Description: fix a thing\n\
94 --- a/foo\n\
95 +++ b/foo\n\
96 @@ -1 +1 @@\n\
97 -old\n\
98 +new\n";
99
100 #[test]
101 fn parse_diff_returns_single_patch() {
102 let patches = parse_diff(PATCH).unwrap();
103 assert_eq!(patches.len(), 1);
104 match &patches[0] {
105 PlainOrBinaryPatch::Plain(p) => {
106 assert_eq!(p.orig_name, b"a/foo");
107 assert_eq!(p.mod_name, b"b/foo");
108 assert_eq!(p.hunks.len(), 1);
109 }
110 PlainOrBinaryPatch::Binary(_) => panic!("expected a plain patch"),
111 }
112 }
113
114 #[test]
115 fn parse_diff_header_only_is_empty() {
116 let patches = parse_diff("Author: alice\nDescription: bla\n").unwrap();
117 assert!(patches.is_empty());
118 }
119
120 #[test]
121 fn parse_diff_multiple_files() {
122 let content = "Description: touch two files\n\
123 --- a/foo\n\
124 +++ b/foo\n\
125 @@ -1 +1 @@\n\
126 -a\n\
127 +b\n\
128 --- a/bar\n\
129 +++ b/bar\n\
130 @@ -1 +1 @@\n\
131 -c\n\
132 +d\n";
133 let patches = parse_diff(content).unwrap();
134 assert_eq!(patches.len(), 2);
135 }
136
137 #[cfg(feature = "lossless")]
138 #[test]
139 fn parse_with_diff_returns_header_and_patches() {
140 let (header, patches) = parse_with_diff(PATCH).unwrap();
141 assert_eq!(header.author(), Some("alice".to_string()));
142 assert_eq!(header.description(), Some("fix a thing".to_string()));
143 assert_eq!(patches.len(), 1);
144 }
145 }