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 patchkitrust-analyzer cargo patchkit 0.3.3 crate/::unifiedrust-analyzer cargo patchkit 0.3.3 unified/::{parse_patchesrust-analyzer cargo patchkit 0.3.3 unified/parse_patches()., PlainOrBinaryPatchrust-analyzer cargo patchkit 0.3.3 unified/PlainOrBinaryPatch#};
28
29 /// An error returned while parsing a patch file into a header and a diff body.
30 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
31 pub enum Errorrust-analyzer cargo dep3 0.2.4 patchkit/Error# {
32 /// The DEP-3 header could not be parsed as a deb822 paragraph.
33 #[cfg(feature = "lossless")]
34 Headerrust-analyzer cargo dep3 0.2.4 patchkit/Error#Header#(deb822_losslessrust-analyzer cargo deb822-lossless 0.5.17 crate/::ParseErrorrust-analyzer cargo deb822-lossless 0.5.17 lossless/ParseError#),
35 /// The unified diff body could not be parsed.
36 Diffrust-analyzer cargo dep3 0.2.4 patchkit/Error#Diff#(patchkitrust-analyzer cargo patchkit 0.3.3 crate/::unifiedrust-analyzer cargo patchkit 0.3.3 unified/::Errorrust-analyzer cargo patchkit 0.3.3 unified/Error#),
37 }
38
39 #[cfg(feature = "lossless")]
40 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<deb822_losslessrust-analyzer cargo deb822-lossless 0.5.17 crate/::ParseErrorrust-analyzer cargo deb822-lossless 0.5.17 lossless/ParseError#> for Errorrust-analyzer cargo dep3 0.2.4 patchkit/Error# {
41 fn fromrust-analyzer cargo dep3 0.2.4 patchkit/impl#[Error][`From<deb822_lossless::ParseError>`]from().(e: deb822_losslessrust-analyzer cargo deb822-lossless 0.5.17 crate/::ParseErrorrust-analyzer cargo deb822-lossless 0.5.17 lossless/ParseError#) -> Selfrust-analyzer cargo dep3 0.2.4 patchkit/impl#[Error][`From<deb822_lossless::ParseError>`] {
42 Errorrust-analyzer cargo dep3 0.2.4 patchkit/Error#::Headerrust-analyzer cargo dep3 0.2.4 patchkit/Error#Header#(e)
43 }
44 }
45
46 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<patchkitrust-analyzer cargo patchkit 0.3.3 crate/::unifiedrust-analyzer cargo patchkit 0.3.3 unified/::Errorrust-analyzer cargo patchkit 0.3.3 unified/Error#> for Errorrust-analyzer cargo dep3 0.2.4 patchkit/Error# {
47 fn fromrust-analyzer cargo dep3 0.2.4 patchkit/impl#[Error][`From<patchkit::unified::Error>`]from().(e: patchkitrust-analyzer cargo patchkit 0.3.3 crate/::unifiedrust-analyzer cargo patchkit 0.3.3 unified/::Errorrust-analyzer cargo patchkit 0.3.3 unified/Error#) -> Selfrust-analyzer cargo dep3 0.2.4 patchkit/impl#[Error][`From<patchkit::unified::Error>`] {
48 Errorrust-analyzer cargo dep3 0.2.4 patchkit/Error#::Diffrust-analyzer cargo dep3 0.2.4 patchkit/Error#Diff#(e)
49 }
50 }
51
52 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Errorrust-analyzer cargo dep3 0.2.4 patchkit/Error# {
53 fn fmtrust-analyzer cargo dep3 0.2.4 patchkit/impl#[Error][Display]fmt().(&self, f: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
54 match self {
55 #[cfg(feature = "lossless")]
56 Errorrust-analyzer cargo dep3 0.2.4 patchkit/Error#::Headerrust-analyzer cargo dep3 0.2.4 patchkit/Error#Header#(e) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "header: {}", e),
57 Errorrust-analyzer cargo dep3 0.2.4 patchkit/Error#::Diffrust-analyzer cargo dep3 0.2.4 patchkit/Error#Diff#(e) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "diff: {}", e),
58 }
59 }
60 }
61
62 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for Errorrust-analyzer cargo dep3 0.2.4 patchkit/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_diffrust-analyzer cargo dep3 0.2.4 patchkit/parse_diff().(content: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<PlainOrBinaryPatchrust-analyzer cargo patchkit 0.3.3 unified/PlainOrBinaryPatch#>, patchkitrust-analyzer cargo patchkit 0.3.3 crate/::unifiedrust-analyzer cargo patchkit 0.3.3 unified/::Errorrust-analyzer cargo patchkit 0.3.3 unified/Error#> {
70 let end = craterust-analyzer cargo dep3 0.2.4 crate/::header_endrust-analyzer cargo dep3 0.2.4 header_end().(content);
71 let body = &content[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().end..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
72 parse_patchesrust-analyzer cargo patchkit 0.3.3 unified/parse_patches().(body.split_inclusiverust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_inclusive().('\n').maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|l| l.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().().to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().())).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#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_diffrust-analyzer cargo dep3 0.2.4 patchkit/parse_with_diff().(
81 content: &str,
82 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(craterust-analyzer cargo dep3 0.2.4 crate/::losslessrust-analyzer cargo dep3 0.2.4 lossless/::PatchHeaderrust-analyzer cargo dep3 0.2.4 lossless/PatchHeader#, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<PlainOrBinaryPatchrust-analyzer cargo patchkit 0.3.3 unified/PlainOrBinaryPatch#>), Errorrust-analyzer cargo dep3 0.2.4 patchkit/Error#> {
83 let (header, _) = craterust-analyzer cargo dep3 0.2.4 crate/::losslessrust-analyzer cargo dep3 0.2.4 lossless/::PatchHeaderrust-analyzer cargo dep3 0.2.4 lossless/PatchHeader#::parse_relaxedrust-analyzer cargo dep3 0.2.4 lossless/impl#[PatchHeader]parse_relaxed().(content)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
84 let patches = parse_diffrust-analyzer cargo dep3 0.2.4 patchkit/parse_diff().(content)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
85 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((header, patches))
86 }
87
88 #[cfg(test)]
89 mod testsrust-analyzer cargo dep3 0.2.4 patchkit/tests/ {
90 use superrust-analyzer cargo dep3 0.2.4 patchkit/::*;
91
92 const PATCHrust-analyzer cargo dep3 0.2.4 patchkit/tests/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 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
101 fn parse_diff_returns_single_patchrust-analyzer cargo dep3 0.2.4 patchkit/tests/parse_diff_returns_single_patch().() {
102 let patches = parse_diffrust-analyzer cargo dep3 0.2.4 patchkit/parse_diff().(PATCHrust-analyzer cargo dep3 0.2.4 patchkit/tests/PATCH.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
103 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(patches.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), 1);
104 match &patches[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). {
105 PlainOrBinaryPatchrust-analyzer cargo patchkit 0.3.3 unified/PlainOrBinaryPatch#::Plainrust-analyzer cargo patchkit 0.3.3 unified/PlainOrBinaryPatch#Plain#(p) => {
106 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p.orig_namerust-analyzer cargo patchkit 0.3.3 unified/UnifiedPatch#orig_name., b"a/foo");
107 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p.mod_namerust-analyzer cargo patchkit 0.3.3 unified/UnifiedPatch#mod_name., b"b/foo");
108 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p.hunksrust-analyzer cargo patchkit 0.3.3 unified/UnifiedPatch#hunks..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), 1);
109 }
110 PlainOrBinaryPatchrust-analyzer cargo patchkit 0.3.3 unified/PlainOrBinaryPatch#::Binaryrust-analyzer cargo patchkit 0.3.3 unified/PlainOrBinaryPatch#Binary#(_) => panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("expected a plain patch"),
111 }
112 }
113
114 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
115 fn parse_diff_header_only_is_emptyrust-analyzer cargo dep3 0.2.4 patchkit/tests/parse_diff_header_only_is_empty().() {
116 let patches = parse_diffrust-analyzer cargo dep3 0.2.4 patchkit/parse_diff().("Author: alice\nDescription: bla\n").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
117 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(patches.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().());
118 }
119
120 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
121 fn parse_diff_multiple_filesrust-analyzer cargo dep3 0.2.4 patchkit/tests/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_diffrust-analyzer cargo dep3 0.2.4 patchkit/parse_diff().(content).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
134 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(patches.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), 2);
135 }
136
137 #[cfg(feature = "lossless")]
138 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
139 fn parse_with_diff_returns_header_and_patchesrust-analyzer cargo dep3 0.2.4 patchkit/tests/parse_with_diff_returns_header_and_patches().() {
140 let (header, patches) = parse_with_diffrust-analyzer cargo dep3 0.2.4 patchkit/parse_with_diff().(PATCHrust-analyzer cargo dep3 0.2.4 patchkit/tests/PATCH.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
141 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(header.authorrust-analyzer cargo dep3 0.2.4 lossless/impl#[PatchHeader]author().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("alice".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()));
142 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(header.descriptionrust-analyzer cargo dep3 0.2.4 lossless/impl#[PatchHeader]description().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("fix a thing".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()));
143 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(patches.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), 1);
144 }
145 }