| 1 | #![deny(missing_docs)] |
| 2 | //! A library for parsing and generating Debian patch headers. |
| 3 | //! |
| 4 | //! # Examples |
| 5 | //! |
| 6 | //! ```rust |
| 7 | //! use dep3::PatchHeader; |
| 8 | //! use std::str::FromStr; |
| 9 | //! let text = r#"From: John Doe <john.doe@example> |
| 10 | //! Date: Mon, 1 Jan 2000 00:00:00 +0000 |
| 11 | //! Subject: [PATCH] fix a bug |
| 12 | //! Bug-Debian: https://bugs.debian.org/123456 |
| 13 | //! Bug: https://bugzilla.example.com/bug.cgi?id=123456 |
| 14 | //! Forwarded: not-needed |
| 15 | //! "#; |
| 16 | //! |
| 17 | //! let patch_header = PatchHeader::from_str(text).unwrap(); |
| 18 | //! assert_eq!(patch_header.description, Some("[PATCH] fix a bug".to_string())); |
| 19 | //! assert_eq!(patch_header.vendor_bugs("Debian"), Some("https://bugs.debian.org/123456")); |
| 20 | //! ``` |
| 21 | mod fields; |
| 22 | pub use fields::*; |
| 23 | #[cfg(feature = "lossless")] |
| 24 | pub mod lossless; |
| 25 | pub mod lossy; |
| 26 | #[cfg(feature = "patchkit")] |
| 27 | pub mod patchkit; |
| 28 | |
| 29 | pub use lossy::PatchHeader; |
| 30 | |
| 31 | /// Find the byte offset where the DEP-3 header in `content` ends, i.e. |
| 32 | /// the start of the first `---` / `diff ` / `Index:` line. Returns |
| 33 | /// `content.len()` if the file is header-only (no diff body). |
| 34 | /// |
| 35 | /// This lets callers split a complete patch file into its header (a |
| 36 | /// deb822 paragraph) and its unified diff (which DEP-3 leaves |
| 37 | /// unspecified). Both [`lossless::PatchHeader::parse_relaxed`] and the |
| 38 | /// lossy [`PatchHeader`] use this internally; it's exposed for callers |
| 39 | /// that need to map source ranges back into the original file. |
| 40 | pub fn header_end(content: &str) -> usize { |
| 41 | let mut offset = 0; |
| 42 | for line in content.split_inclusive('\n') { |
| 43 | let trimmed = line.trim_end_matches(['\r', '\n']); |
| 44 | if trimmed.starts_with("---") |
| 45 | || trimmed.starts_with("diff ") |
| 46 | || trimmed.starts_with("Index:") |
| 47 | { |
| 48 | return offset; |
| 49 | } |
| 50 | offset += line.len(); |
| 51 | } |
| 52 | content.len() |
| 53 | } |