| 1 | //! A library for parsing and generating Debian patch headers. |
| 2 | //! |
| 3 | //! # Examples |
| 4 | //! |
| 5 | //! ```rust |
| 6 | //! use dep3::lossy::PatchHeader; |
| 7 | //! use std::str::FromStr; |
| 8 | //! let text = r#"From: John Doe <john.doe@example> |
| 9 | //! Date: Mon, 1 Jan 2000 00:00:00 +0000 |
| 10 | //! Subject: [PATCH] fix a bug |
| 11 | //! Bug-Debian: https://bugs.debian.org/123456 |
| 12 | //! Bug: https://bugzilla.example.com/bug.cgi?id=123456 |
| 13 | //! Forwarded: not-needed |
| 14 | //! "#; |
| 15 | //! |
| 16 | //! let patch_header = PatchHeader::from_str(text).unwrap(); |
| 17 | //! assert_eq!(patch_header.description, Some("[PATCH] fix a bug".to_string())); |
| 18 | //! assert_eq!(patch_header.bug_debian, Some("https://bugs.debian.org/123456".parse().unwrap())); |
| 19 | //! ``` |
| 20 | use crate::fields::*; |
| 21 | use deb822_fast::{FromDeb822, FromDeb822Paragraph, Paragraph, ToDeb822, ToDeb822Paragraph}; |
| 22 | |
| 23 | fn deserialize_date(s: &str) -> Result<chrono::NaiveDate, String> { |
| 24 | chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d").map_err(|e| e.to_string()) |
| 25 | } |
| 26 | |
| 27 | fn serialize_date(date: &chrono::NaiveDate) -> String { |
| 28 | date.format("%Y-%m-%d").to_string() |
| 29 | } |
| 30 | |
| 31 | fn deserialize_origin(s: &str) -> Result<(Option<OriginCategory>, Origin), String> { |
| 32 | Ok(crate::fields::parse_origin(s)) |
| 33 | } |
| 34 | |
| 35 | fn serialize_origin((category, origin): &(Option<OriginCategory>, Origin)) -> String { |
| 36 | crate::fields::format_origin(category, origin) |
| 37 | } |
| 38 | |
| 39 | /// A patch header. |
| 40 | #[derive(Debug, Clone, PartialEq, FromDeb822, ToDeb822)] |
| 41 | pub struct PatchHeader { |
| 42 | #[deb822(field = "Origin", serialize_with = serialize_origin, deserialize_with = deserialize_origin)] |
| 43 | /// The origin of the patch. |
| 44 | pub origin: Option<(Option<OriginCategory>, Origin)>, |
| 45 | |
| 46 | #[deb822(field = "Forwarded")] |
| 47 | /// Whether the patch has been forwarded upstream. |
| 48 | pub forwarded: Option<Forwarded>, |
| 49 | |
| 50 | #[deb822(field = "Author")] |
| 51 | /// The author of the patch. |
| 52 | pub author: Option<String>, |
| 53 | |
| 54 | #[deb822(field = "Reviewed-by")] |
| 55 | /// The person who reviewed the patch. |
| 56 | pub reviewed_by: Option<String>, |
| 57 | |
| 58 | #[deb822(field = "Bug-Debian")] |
| 59 | /// The URL of the Debian bug report. |
| 60 | pub bug_debian: Option<url::Url>, |
| 61 | |
| 62 | #[deb822(field = "Last-Update", deserialize_with = deserialize_date, serialize_with = serialize_date)] |
| 63 | /// The date of the last update. |
| 64 | pub last_update: Option<chrono::NaiveDate>, |
| 65 | |
| 66 | #[deb822(field = "Applied-Upstream")] |
| 67 | /// Whether the patch has been applied upstream. |
| 68 | pub applied_upstream: Option<AppliedUpstream>, |
| 69 | |
| 70 | #[deb822(field = "Bug")] |
| 71 | /// The URL of the upstream bug report. |
| 72 | pub bug: Option<url::Url>, |
| 73 | |
| 74 | #[deb822(field = "Description")] |
| 75 | /// The description of the patch. |
| 76 | pub description: Option<String>, |
| 77 | } |
| 78 | |
| 79 | impl PatchHeader { |
| 80 | /// Create a new patch header. |
| 81 | pub fn vendor_bugs(&self, vendor: &str) -> Option<&str> { |
| 82 | match vendor { |
| 83 | "Debian" => self.bug_debian.as_ref().map(|u| u.as_str()), |
| 84 | _ => None, |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// The patch synopsis: the first line of the `Description` (or `Subject`) |
| 89 | /// field. Per DEP-3 the field carries a one-line synopsis optionally followed |
| 90 | /// by a longer description; this returns just the synopsis. |
| 91 | pub fn synopsis(&self) -> Option<&str> { |
| 92 | self.description |
| 93 | .as_deref() |
| 94 | .map(|d| d.split('\n').next().unwrap_or(d).trim_end()) |
| 95 | } |
| 96 | |
| 97 | /// The long description: everything after the synopsis line of the |
| 98 | /// `Description` (or `Subject`) field, or `None` when there is only a |
| 99 | /// synopsis. |
| 100 | /// |
| 101 | /// A lone `.` line (the deb822 blank-line marker) is decoded back to an empty |
| 102 | /// line, so paragraph breaks read naturally. |
| 103 | pub fn long_description(&self) -> Option<String> { |
| 104 | let rest = self.description.as_deref()?.split_once('\n').map(|x| x.1)?; |
| 105 | let trimmed = rest.trim_matches('\n'); |
| 106 | if trimmed.is_empty() { |
| 107 | return None; |
| 108 | } |
| 109 | let decoded = trimmed |
| 110 | .split('\n') |
| 111 | .map(|line| if line == "." { "" } else { line }) |
| 112 | .collect::<Vec<_>>() |
| 113 | .join("\n"); |
| 114 | Some(decoded) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | impl std::fmt::Display for PatchHeader { |
| 119 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 120 | let paragraph: deb822_fast::Paragraph = self.to_paragraph(); |
| 121 | paragraph.fmt(f) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | impl std::str::FromStr for PatchHeader { |
| 126 | type Err = String; |
| 127 | |
| 128 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 129 | let paragraph = Paragraph::from_str(s).map_err(|e| e.to_string())?; |
| 130 | let mut header = PatchHeader::from_paragraph(¶graph)?; |
| 131 | if header.author.is_none() { |
| 132 | header.author = paragraph.get("From").map(ToString::to_string); |
| 133 | } |
| 134 | if header.description.is_none() { |
| 135 | header.description = paragraph.get("Subject").map(ToString::to_string); |
| 136 | } |
| 137 | Ok(header) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | #[cfg(test)] |
| 142 | mod tests { |
| 143 | use super::PatchHeader; |
| 144 | use std::borrow::Cow; |
| 145 | |
| 146 | #[test] |
| 147 | fn test_upstream() { |
| 148 | let text = r#"From: Ulrich Drepper <[email protected]> |
| 149 | Subject: Fix regex problems with some multi-bytes characters |
| 150 | |
| 151 | * posix/bug-regex17.c: Add testcases. |
| 152 | * posix/regcomp.c (re_compile_fastmap_iter): Rewrite COMPLEX_BRACKET |
| 153 | handling. |
| 154 | |
| 155 | Origin: upstream, http://sourceware.org/git/?p=glibc.git;a=commitdiff;h=bdb56bac |
| 156 | Bug: http://sourceware.org/bugzilla/show_bug.cgi?id=9697 |
| 157 | Bug-Debian: http://bugs.debian.org/510219 |
| 158 | "#; |
| 159 | |
| 160 | let header: PatchHeader = text.parse().unwrap(); |
| 161 | |
| 162 | assert_eq!( |
| 163 | header.origin, |
| 164 | Some(( |
| 165 | Some(super::OriginCategory::Upstream), |
| 166 | super::Origin::Other(Cow::Borrowed( |
| 167 | "http://sourceware.org/git/?p=glibc.git;a=commitdiff;h=bdb56bac" |
| 168 | )) |
| 169 | )) |
| 170 | ); |
| 171 | assert_eq!(header.forwarded, None); |
| 172 | assert_eq!( |
| 173 | header.author, |
| 174 | Some("Ulrich Drepper <[email protected]>".to_string()) |
| 175 | ); |
| 176 | assert_eq!(header.reviewed_by, None); |
| 177 | assert_eq!(header.last_update, None); |
| 178 | assert_eq!(header.applied_upstream, None); |
| 179 | assert_eq!( |
| 180 | header.bug, |
| 181 | "http://sourceware.org/bugzilla/show_bug.cgi?id=9697" |
| 182 | .parse() |
| 183 | .ok() |
| 184 | ); |
| 185 | assert_eq!( |
| 186 | header.bug_debian, |
| 187 | "http://bugs.debian.org/510219".parse().ok() |
| 188 | ); |
| 189 | assert_eq!( |
| 190 | header.description, |
| 191 | Some("Fix regex problems with some multi-bytes characters\n\n* posix/bug-regex17.c: Add testcases.\n* posix/regcomp.c (re_compile_fastmap_iter): Rewrite COMPLEX_BRACKET\nhandling.\n".to_string()) |
| 192 | ); |
| 193 | } |
| 194 | |
| 195 | #[test] |
| 196 | fn test_forwarded() { |
| 197 | let text = r#"Description: Use FHS compliant paths by default |
| 198 | Upstream is not interested in switching to those paths. |
| 199 | . |
| 200 | But we will continue using them in Debian nevertheless to comply with |
| 201 | our policy. |
| 202 | Forwarded: http://lists.example.com/oct-2006/1234.html |
| 203 | Author: John Doe <[email protected]> |
| 204 | Last-Update: 2006-12-21 |
| 205 | "#; |
| 206 | let header: PatchHeader = text.parse().unwrap(); |
| 207 | |
| 208 | assert_eq!(header.origin, None); |
| 209 | assert_eq!( |
| 210 | header.forwarded, |
| 211 | Some(super::Forwarded::Yes(Cow::Borrowed( |
| 212 | "http://lists.example.com/oct-2006/1234.html" |
| 213 | ))) |
| 214 | ); |
| 215 | assert_eq!( |
| 216 | header.author, |
| 217 | Some("John Doe <[email protected]>".to_string()) |
| 218 | ); |
| 219 | assert_eq!(header.reviewed_by, None); |
| 220 | assert_eq!( |
| 221 | header.last_update, |
| 222 | Some(chrono::NaiveDate::from_ymd_opt(2006, 12, 21).unwrap()) |
| 223 | ); |
| 224 | assert_eq!(header.applied_upstream, None); |
| 225 | assert_eq!( |
| 226 | header.description, |
| 227 | Some("Use FHS compliant paths by default\nUpstream is not interested in switching to those paths.\n.\nBut we will continue using them in Debian nevertheless to comply with\nour policy.".to_string()) |
| 228 | ); |
| 229 | } |
| 230 | |
| 231 | #[test] |
| 232 | fn test_synopsis_and_long_description() { |
| 233 | let text = r#"Description: Use FHS compliant paths by default |
| 234 | Upstream is not interested in switching to those paths. |
| 235 | . |
| 236 | But we will continue using them in Debian nevertheless. |
| 237 | Author: John Doe <[email protected]> |
| 238 | "#; |
| 239 | let header: PatchHeader = text.parse().unwrap(); |
| 240 | assert_eq!( |
| 241 | header.synopsis(), |
| 242 | Some("Use FHS compliant paths by default") |
| 243 | ); |
| 244 | assert_eq!( |
| 245 | header.long_description().as_deref(), |
| 246 | Some( |
| 247 | "Upstream is not interested in switching to those paths.\n\nBut we will continue using them in Debian nevertheless." |
| 248 | ) |
| 249 | ); |
| 250 | } |
| 251 | |
| 252 | #[test] |
| 253 | fn test_synopsis_only_has_no_long_description() { |
| 254 | let text = "Description: A one-line synopsis only\nAuthor: X <[email protected]>\n"; |
| 255 | let header: PatchHeader = text.parse().unwrap(); |
| 256 | assert_eq!(header.synopsis(), Some("A one-line synopsis only")); |
| 257 | assert_eq!(header.long_description(), None); |
| 258 | } |
| 259 | |
| 260 | #[test] |
| 261 | fn test_synopsis_from_subject() { |
| 262 | // The git-style `Subject` aliases `Description`, so synopsis works too. |
| 263 | let text = "Subject: Fix the thing\n Details here.\nFrom: X <[email protected]>\n"; |
| 264 | let header: PatchHeader = text.parse().unwrap(); |
| 265 | assert_eq!(header.synopsis(), Some("Fix the thing")); |
| 266 | assert_eq!(header.long_description().as_deref(), Some("Details here.")); |
| 267 | } |
| 268 | |
| 269 | #[test] |
| 270 | fn test_not_forwarded() { |
| 271 | let text = r#"Description: Workaround for broken symbol resolving on mips/mipsel |
| 272 | The correct fix will be done in etch and it will require toolchain |
| 273 | fixes. |
| 274 | Forwarded: not-needed |
| 275 | Origin: vendor, http://bugs.debian.org/cgi-bin/bugreport.cgi?msg=80;bug=265678 |
| 276 | Bug-Debian: http://bugs.debian.org/265678 |
| 277 | Author: Thiemo Seufer <[email protected]> |
| 278 | "#; |
| 279 | |
| 280 | let header: PatchHeader = text.parse().unwrap(); |
| 281 | |
| 282 | assert_eq!( |
| 283 | header.origin, |
| 284 | Some(( |
| 285 | Some(super::OriginCategory::Vendor), |
| 286 | super::Origin::Other(Cow::Borrowed( |
| 287 | "http://bugs.debian.org/cgi-bin/bugreport.cgi?msg=80;bug=265678" |
| 288 | )) |
| 289 | )) |
| 290 | ); |
| 291 | assert_eq!(header.forwarded, Some(super::Forwarded::NotNeeded)); |
| 292 | assert_eq!( |
| 293 | header.author, |
| 294 | Some("Thiemo Seufer <[email protected]>".to_string()) |
| 295 | ); |
| 296 | assert_eq!(header.reviewed_by, None); |
| 297 | assert_eq!(header.last_update, None); |
| 298 | assert_eq!(header.applied_upstream, None); |
| 299 | assert_eq!( |
| 300 | header.bug_debian, |
| 301 | "http://bugs.debian.org/265678".parse().ok() |
| 302 | ); |
| 303 | |
| 304 | assert_eq!( |
| 305 | header.description, |
| 306 | Some( |
| 307 | "Workaround for broken symbol resolving on mips/mipsel |
| 308 | The correct fix will be done in etch and it will require toolchain |
| 309 | fixes." |
| 310 | .to_string() |
| 311 | ) |
| 312 | ); |
| 313 | } |
| 314 | |
| 315 | #[test] |
| 316 | fn test_applied_upstream() { |
| 317 | let text = r#"Description: Fix widget frobnication speeds |
| 318 | Frobnicating widgets too quickly tended to cause explosions. |
| 319 | Forwarded: http://lists.example.com/2010/03/1234.html |
| 320 | Author: John Doe <[email protected]> |
| 321 | Applied-Upstream: 1.2, http://bzr.example.com/frobnicator/trunk/revision/123 |
| 322 | Last-Update: 2010-03-29 |
| 323 | "#; |
| 324 | let header: PatchHeader = text.parse().unwrap(); |
| 325 | |
| 326 | assert_eq!(header.origin, None); |
| 327 | assert_eq!( |
| 328 | header.forwarded, |
| 329 | Some(super::Forwarded::Yes(Cow::Borrowed( |
| 330 | "http://lists.example.com/2010/03/1234.html" |
| 331 | ))) |
| 332 | ); |
| 333 | assert_eq!( |
| 334 | header.author, |
| 335 | Some("John Doe <[email protected]>".to_string()) |
| 336 | ); |
| 337 | assert_eq!(header.reviewed_by, None); |
| 338 | assert_eq!( |
| 339 | header.last_update, |
| 340 | Some(chrono::NaiveDate::from_ymd_opt(2010, 3, 29).unwrap()) |
| 341 | ); |
| 342 | assert_eq!( |
| 343 | header.applied_upstream, |
| 344 | Some(super::AppliedUpstream::Other(Cow::Borrowed( |
| 345 | "1.2, http://bzr.example.com/frobnicator/trunk/revision/123" |
| 346 | ))) |
| 347 | ); |
| 348 | assert_eq!( |
| 349 | header.description, |
| 350 | Some("Fix widget frobnication speeds\nFrobnicating widgets too quickly tended to cause explosions.".to_string()) |
| 351 | ); |
| 352 | } |
| 353 | } |