1#![cfg(any(feature = "linebased", feature = "deb822"))]
2#[derive(Debug)]
8pub enum ParseError {
9 #[cfg(feature = "linebased")]
11 LineBased(crate::linebased::ParseError),
12 #[cfg(feature = "deb822")]
14 Deb822(crate::deb822::ParseError),
15 UnknownVersion,
17 FeatureNotEnabled(String),
19}
20
21impl std::fmt::Display for ParseError {
22 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
23 match self {
24 #[cfg(feature = "linebased")]
25 ParseError::LineBased(e) => write!(f, "{}", e),
26 #[cfg(feature = "deb822")]
27 ParseError::Deb822(e) => write!(f, "{}", e),
28 ParseError::UnknownVersion => write!(f, "Could not detect watch file version"),
29 ParseError::FeatureNotEnabled(msg) => write!(f, "{}", msg),
30 }
31 }
32}
33
34impl std::error::Error for ParseError {}
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub enum WatchFileVersion {
39 LineBased(u32),
41 Deb822,
43}
44
45pub fn detect_version(content: &str) -> Option<WatchFileVersion> {
66 let trimmed = content.trim_start();
67
68 if trimmed.starts_with("Version:") || trimmed.starts_with("version:") {
70 if let Some(first_line) = trimmed.lines().next() {
72 if let Some(colon_pos) = first_line.find(':') {
73 let version_str = first_line[colon_pos + 1..].trim();
74 if version_str == "5" {
75 return Some(WatchFileVersion::Deb822);
76 }
77 }
78 }
79 }
80
81 for line in trimmed.lines() {
84 let line = line.trim();
85
86 if line.starts_with('#') || line.is_empty() {
88 continue;
89 }
90
91 if line.starts_with("version=") || line.starts_with("version =") {
93 let version_part = if line.starts_with("version=") {
94 &line[8..]
95 } else {
96 &line[9..]
97 };
98
99 if let Ok(version) = version_part.trim().parse::<u32>() {
100 return Some(WatchFileVersion::LineBased(version));
101 }
102 }
103
104 break;
106 }
107
108 Some(WatchFileVersion::LineBased(crate::DEFAULT_VERSION))
110}
111
112#[derive(Debug)]
114pub enum ParsedWatchFile {
115 #[cfg(feature = "linebased")]
117 LineBased(crate::linebased::WatchFile),
118 #[cfg(feature = "deb822")]
120 Deb822(crate::deb822::WatchFile),
121}
122
123#[derive(Debug)]
125pub enum ParsedEntry {
126 #[cfg(feature = "linebased")]
128 LineBased(crate::linebased::Entry),
129 #[cfg(feature = "deb822")]
131 Deb822(crate::deb822::Entry),
132}
133
134impl ParsedWatchFile {
135 pub fn new(version: u32) -> Result<Self, ParseError> {
152 match version {
153 #[cfg(feature = "deb822")]
154 5 => Ok(ParsedWatchFile::Deb822(crate::deb822::WatchFile::new())),
155 #[cfg(not(feature = "deb822"))]
156 5 => Err(ParseError::FeatureNotEnabled(
157 "deb822 feature required for v5 format".to_string(),
158 )),
159 #[cfg(feature = "linebased")]
160 v @ 1..=4 => Ok(ParsedWatchFile::LineBased(
161 crate::linebased::WatchFile::new(Some(v)),
162 )),
163 #[cfg(not(feature = "linebased"))]
164 v @ 1..=4 => Err(ParseError::FeatureNotEnabled(format!(
165 "linebased feature required for v{} format",
166 v
167 ))),
168 v => Err(ParseError::FeatureNotEnabled(format!(
169 "unsupported watch file version: {}",
170 v
171 ))),
172 }
173 }
174
175 pub fn version(&self) -> u32 {
177 match self {
178 #[cfg(feature = "linebased")]
179 ParsedWatchFile::LineBased(wf) => wf.version(),
180 #[cfg(feature = "deb822")]
181 ParsedWatchFile::Deb822(wf) => wf.version(),
182 }
183 }
184
185 pub fn entries(&self) -> impl Iterator<Item = ParsedEntry> + '_ {
187 let entries: Vec<_> = match self {
189 #[cfg(feature = "linebased")]
190 ParsedWatchFile::LineBased(wf) => wf.entries().map(ParsedEntry::LineBased).collect(),
191 #[cfg(feature = "deb822")]
192 ParsedWatchFile::Deb822(wf) => wf.entries().map(ParsedEntry::Deb822).collect(),
193 };
194 entries.into_iter()
195 }
196
197 pub fn add_entry(&mut self, source: &str, matching_pattern: &str) -> ParsedEntry {
218 match self {
219 #[cfg(feature = "linebased")]
220 ParsedWatchFile::LineBased(wf) => {
221 let entry = crate::linebased::EntryBuilder::new(source)
222 .matching_pattern(matching_pattern)
223 .build();
224 let added_entry = wf.add_entry(entry);
225 ParsedEntry::LineBased(added_entry)
226 }
227 #[cfg(feature = "deb822")]
228 ParsedWatchFile::Deb822(wf) => {
229 let added_entry = wf.add_entry(source, matching_pattern);
230 ParsedEntry::Deb822(added_entry)
231 }
232 }
233 }
234}
235
236impl ParsedEntry {
237 pub fn url(&self) -> String {
239 match self {
240 #[cfg(feature = "linebased")]
241 ParsedEntry::LineBased(e) => e.url(),
242 #[cfg(feature = "deb822")]
243 ParsedEntry::Deb822(e) => e.source().unwrap_or_default(),
244 }
245 }
246
247 pub fn matching_pattern(&self) -> Option<String> {
249 match self {
250 #[cfg(feature = "linebased")]
251 ParsedEntry::LineBased(e) => e.matching_pattern(),
252 #[cfg(feature = "deb822")]
253 ParsedEntry::Deb822(e) => e.matching_pattern(),
254 }
255 }
256
257 pub fn get_option(&self, key: &str) -> Option<String> {
263 match self {
264 #[cfg(feature = "linebased")]
265 ParsedEntry::LineBased(e) => e.get_option(key),
266 #[cfg(feature = "deb822")]
267 ParsedEntry::Deb822(e) => {
268 e.get_field(key).or_else(|| {
270 let mut chars = key.chars();
271 if let Some(first) = chars.next() {
272 let capitalized = first.to_uppercase().chain(chars).collect::<String>();
273 e.get_field(&capitalized)
274 } else {
275 None
276 }
277 })
278 }
279 }
280 }
281
282 pub fn has_option(&self, key: &str) -> bool {
284 self.get_option(key).is_some()
285 }
286
287 pub fn script(&self) -> Option<String> {
289 self.get_option("script")
290 }
291
292 pub fn format_url(
294 &self,
295 package: impl FnOnce() -> String,
296 ) -> Result<url::Url, url::ParseError> {
297 crate::subst::subst(&self.url(), package).parse()
298 }
299
300 pub fn user_agent(&self) -> Option<String> {
302 self.get_option("user-agent")
303 }
304
305 pub fn pagemangle(&self) -> Option<String> {
307 self.get_option("pagemangle")
308 }
309
310 pub fn uversionmangle(&self) -> Option<String> {
312 self.get_option("uversionmangle")
313 }
314
315 pub fn downloadurlmangle(&self) -> Option<String> {
317 self.get_option("downloadurlmangle")
318 }
319
320 pub fn pgpsigurlmangle(&self) -> Option<String> {
322 self.get_option("pgpsigurlmangle")
323 }
324
325 pub fn filenamemangle(&self) -> Option<String> {
327 self.get_option("filenamemangle")
328 }
329
330 pub fn oversionmangle(&self) -> Option<String> {
332 self.get_option("oversionmangle")
333 }
334
335 pub fn searchmode(&self) -> crate::types::SearchMode {
337 self.get_option("searchmode")
338 .and_then(|s| s.parse().ok())
339 .unwrap_or_default()
340 }
341
342 pub fn set_option(&mut self, option: crate::types::WatchOption) {
364 match self {
365 #[cfg(feature = "linebased")]
366 ParsedEntry::LineBased(e) => {
367 e.set_option(option);
368 }
369 #[cfg(feature = "deb822")]
370 ParsedEntry::Deb822(e) => {
371 e.set_option(option);
372 }
373 }
374 }
375
376 pub fn set_url(&mut self, url: &str) {
392 match self {
393 #[cfg(feature = "linebased")]
394 ParsedEntry::LineBased(e) => e.set_url(url),
395 #[cfg(feature = "deb822")]
396 ParsedEntry::Deb822(e) => e.set_source(url),
397 }
398 }
399
400 pub fn set_matching_pattern(&mut self, pattern: &str) {
416 match self {
417 #[cfg(feature = "linebased")]
418 ParsedEntry::LineBased(e) => e.set_matching_pattern(pattern),
419 #[cfg(feature = "deb822")]
420 ParsedEntry::Deb822(e) => e.set_matching_pattern(pattern),
421 }
422 }
423
424 pub fn line(&self) -> usize {
444 match self {
445 #[cfg(feature = "linebased")]
446 ParsedEntry::LineBased(e) => e.line(),
447 #[cfg(feature = "deb822")]
448 ParsedEntry::Deb822(e) => e.line(),
449 }
450 }
451
452 pub fn remove_option(&mut self, option: crate::types::WatchOption) {
475 match self {
476 #[cfg(feature = "linebased")]
477 ParsedEntry::LineBased(e) => e.del_opt(option),
478 #[cfg(feature = "deb822")]
479 ParsedEntry::Deb822(e) => e.delete_option(option),
480 }
481 }
482}
483
484impl std::fmt::Display for ParsedWatchFile {
485 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
486 match self {
487 #[cfg(feature = "linebased")]
488 ParsedWatchFile::LineBased(wf) => write!(f, "{}", wf),
489 #[cfg(feature = "deb822")]
490 ParsedWatchFile::Deb822(wf) => write!(f, "{}", wf),
491 }
492 }
493}
494
495pub fn parse(content: &str) -> Result<ParsedWatchFile, ParseError> {
514 let version = detect_version(content).ok_or(ParseError::UnknownVersion)?;
515
516 match version {
517 #[cfg(feature = "linebased")]
518 WatchFileVersion::LineBased(_v) => {
519 let wf: crate::linebased::WatchFile = content.parse().map_err(ParseError::LineBased)?;
520 Ok(ParsedWatchFile::LineBased(wf))
521 }
522 #[cfg(not(feature = "linebased"))]
523 WatchFileVersion::LineBased(_v) => Err(ParseError::FeatureNotEnabled(
524 "linebased feature required for v1-4 formats".to_string(),
525 )),
526 #[cfg(feature = "deb822")]
527 WatchFileVersion::Deb822 => {
528 let wf: crate::deb822::WatchFile = content.parse().map_err(ParseError::Deb822)?;
529 Ok(ParsedWatchFile::Deb822(wf))
530 }
531 #[cfg(not(feature = "deb822"))]
532 WatchFileVersion::Deb822 => Err(ParseError::FeatureNotEnabled(
533 "deb822 feature required for v5 format".to_string(),
534 )),
535 }
536}
537
538#[cfg(test)]
539mod tests {
540 use super::*;
541
542 #[test]
543 fn test_detect_version_v1_default() {
544 let content = "https://example.com/ .*.tar.gz";
545 assert_eq!(
546 detect_version(content),
547 Some(WatchFileVersion::LineBased(1))
548 );
549 }
550
551 #[test]
552 fn test_detect_version_v4() {
553 let content = "version=4\nhttps://example.com/ .*.tar.gz";
554 assert_eq!(
555 detect_version(content),
556 Some(WatchFileVersion::LineBased(4))
557 );
558 }
559
560 #[test]
561 fn test_detect_version_v4_with_spaces() {
562 let content = "version = 4\nhttps://example.com/ .*.tar.gz";
563 assert_eq!(
564 detect_version(content),
565 Some(WatchFileVersion::LineBased(4))
566 );
567 }
568
569 #[test]
570 fn test_detect_version_v5() {
571 let content = "Version: 5\n\nSource: https://example.com/";
572 assert_eq!(detect_version(content), Some(WatchFileVersion::Deb822));
573 }
574
575 #[test]
576 fn test_detect_version_v5_lowercase() {
577 let content = "version: 5\n\nSource: https://example.com/";
578 assert_eq!(detect_version(content), Some(WatchFileVersion::Deb822));
579 }
580
581 #[test]
582 fn test_detect_version_with_leading_comments() {
583 let content = "# This is a comment\nversion=4\nhttps://example.com/ .*.tar.gz";
584 assert_eq!(
585 detect_version(content),
586 Some(WatchFileVersion::LineBased(4))
587 );
588 }
589
590 #[test]
591 fn test_detect_version_with_leading_whitespace() {
592 let content = " \n version=3\nhttps://example.com/ .*.tar.gz";
593 assert_eq!(
594 detect_version(content),
595 Some(WatchFileVersion::LineBased(3))
596 );
597 }
598
599 #[test]
600 fn test_detect_version_v2() {
601 let content = "version=2\nhttps://example.com/ .*.tar.gz";
602 assert_eq!(
603 detect_version(content),
604 Some(WatchFileVersion::LineBased(2))
605 );
606 }
607
608 #[cfg(feature = "linebased")]
609 #[test]
610 fn test_parse_linebased() {
611 let content = "version=4\nhttps://example.com/ .*.tar.gz";
612 let parsed = parse(content).unwrap();
613 assert_eq!(parsed.version(), 4);
614 }
615
616 #[cfg(feature = "deb822")]
617 #[test]
618 fn test_parse_deb822() {
619 let content = "Version: 5\n\nSource: https://example.com/\nMatching-Pattern: .*.tar.gz";
620 let parsed = parse(content).unwrap();
621 assert_eq!(parsed.version(), 5);
622 }
623
624 #[cfg(all(feature = "linebased", feature = "deb822"))]
625 #[test]
626 fn test_parse_both_formats() {
627 let v4_content = "version=4\nhttps://example.com/ .*.tar.gz";
629 let v4_parsed = parse(v4_content).unwrap();
630 assert_eq!(v4_parsed.version(), 4);
631
632 let v5_content = "Version: 5\n\nSource: https://example.com/\nMatching-Pattern: .*.tar.gz";
634 let v5_parsed = parse(v5_content).unwrap();
635 assert_eq!(v5_parsed.version(), 5);
636 }
637
638 #[cfg(feature = "linebased")]
639 #[test]
640 fn test_parse_roundtrip() {
641 let content = "version=4\n# Comment\nhttps://example.com/ .*.tar.gz";
642 let parsed = parse(content).unwrap();
643 let output = parsed.to_string();
644
645 let reparsed = parse(&output).unwrap();
647 assert_eq!(reparsed.version(), 4);
648 }
649
650 #[cfg(feature = "deb822")]
651 #[test]
652 fn test_parsed_watch_file_new_v5() {
653 let wf = ParsedWatchFile::new(5).unwrap();
654 assert_eq!(wf.version(), 5);
655 assert_eq!(wf.entries().count(), 0);
656 }
657
658 #[cfg(feature = "linebased")]
659 #[test]
660 fn test_parsed_watch_file_new_v4() {
661 let wf = ParsedWatchFile::new(4).unwrap();
662 assert_eq!(wf.version(), 4);
663 assert_eq!(wf.entries().count(), 0);
664 }
665
666 #[cfg(feature = "deb822")]
667 #[test]
668 fn test_parsed_watch_file_add_entry_v5() {
669 let mut wf = ParsedWatchFile::new(5).unwrap();
670 let mut entry = wf.add_entry("https://github.com/foo/bar/tags", r".*/v?([\d.]+)\.tar\.gz");
671
672 assert_eq!(wf.entries().count(), 1);
673 assert_eq!(entry.url(), "https://github.com/foo/bar/tags");
674 assert_eq!(
675 entry.matching_pattern(),
676 Some(r".*/v?([\d.]+)\.tar\.gz".to_string())
677 );
678
679 entry.set_option(crate::types::WatchOption::Component("upstream".to_string()));
681 entry.set_option(crate::types::WatchOption::Compression(
682 crate::types::Compression::Xz,
683 ));
684
685 assert_eq!(entry.get_option("Component"), Some("upstream".to_string()));
686 assert_eq!(entry.get_option("Compression"), Some("xz".to_string()));
687 }
688
689 #[cfg(feature = "linebased")]
690 #[test]
691 fn test_parsed_watch_file_add_entry_v4() {
692 let mut wf = ParsedWatchFile::new(4).unwrap();
693 let entry = wf.add_entry("https://github.com/foo/bar/tags", r".*/v?([\d.]+)\.tar\.gz");
694
695 assert_eq!(wf.entries().count(), 1);
696 assert_eq!(entry.url(), "https://github.com/foo/bar/tags");
697 assert_eq!(
698 entry.matching_pattern(),
699 Some(r".*/v?([\d.]+)\.tar\.gz".to_string())
700 );
701 }
702
703 #[cfg(feature = "deb822")]
704 #[test]
705 fn test_parsed_watch_file_roundtrip_with_add_entry() {
706 let mut wf = ParsedWatchFile::new(5).unwrap();
707 let mut entry = wf.add_entry(
708 "https://github.com/owner/repo/tags",
709 r".*/v?([\d.]+)\.tar\.gz",
710 );
711 entry.set_option(crate::types::WatchOption::Compression(
712 crate::types::Compression::Xz,
713 ));
714
715 let output = wf.to_string();
716
717 let reparsed = parse(&output).unwrap();
719 assert_eq!(reparsed.version(), 5);
720
721 let entries: Vec<_> = reparsed.entries().collect();
722 assert_eq!(entries.len(), 1);
723 assert_eq!(entries[0].url(), "https://github.com/owner/repo/tags");
724 assert_eq!(entries[0].get_option("Compression"), Some("xz".to_string()));
725 }
726
727 #[cfg(feature = "linebased")]
728 #[test]
729 fn test_parsed_entry_set_url_v4() {
730 let mut wf = ParsedWatchFile::new(4).unwrap();
731 let mut entry = wf.add_entry("https://github.com/foo/bar/tags", r".*/v?([\d.]+)\.tar\.gz");
732
733 assert_eq!(entry.url(), "https://github.com/foo/bar/tags");
734
735 entry.set_url("https://github.com/foo/bar/releases");
736 assert_eq!(entry.url(), "https://github.com/foo/bar/releases");
737 }
738
739 #[cfg(feature = "deb822")]
740 #[test]
741 fn test_parsed_entry_set_url_v5() {
742 let mut wf = ParsedWatchFile::new(5).unwrap();
743 let mut entry = wf.add_entry("https://github.com/foo/bar/tags", r".*/v?([\d.]+)\.tar\.gz");
744
745 assert_eq!(entry.url(), "https://github.com/foo/bar/tags");
746
747 entry.set_url("https://github.com/foo/bar/releases");
748 assert_eq!(entry.url(), "https://github.com/foo/bar/releases");
749 }
750
751 #[cfg(feature = "linebased")]
752 #[test]
753 fn test_parsed_entry_set_matching_pattern_v4() {
754 let mut wf = ParsedWatchFile::new(4).unwrap();
755 let mut entry = wf.add_entry("https://github.com/foo/bar/tags", r".*/v?([\d.]+)\.tar\.gz");
756
757 assert_eq!(
758 entry.matching_pattern(),
759 Some(r".*/v?([\d.]+)\.tar\.gz".to_string())
760 );
761
762 entry.set_matching_pattern(r".*/release-([\d.]+)\.tar\.gz");
763 assert_eq!(
764 entry.matching_pattern(),
765 Some(r".*/release-([\d.]+)\.tar\.gz".to_string())
766 );
767 }
768
769 #[cfg(feature = "deb822")]
770 #[test]
771 fn test_parsed_entry_set_matching_pattern_v5() {
772 let mut wf = ParsedWatchFile::new(5).unwrap();
773 let mut entry = wf.add_entry("https://github.com/foo/bar/tags", r".*/v?([\d.]+)\.tar\.gz");
774
775 assert_eq!(
776 entry.matching_pattern(),
777 Some(r".*/v?([\d.]+)\.tar\.gz".to_string())
778 );
779
780 entry.set_matching_pattern(r".*/release-([\d.]+)\.tar\.gz");
781 assert_eq!(
782 entry.matching_pattern(),
783 Some(r".*/release-([\d.]+)\.tar\.gz".to_string())
784 );
785 }
786
787 #[cfg(feature = "linebased")]
788 #[test]
789 fn test_parsed_entry_line_v4() {
790 let content = "version=4\nhttps://example.com/ .*.tar.gz\nhttps://example2.com/ .*.tar.gz";
791 let wf = parse(content).unwrap();
792 let entries: Vec<_> = wf.entries().collect();
793
794 assert_eq!(entries[0].line(), 1); assert_eq!(entries[1].line(), 2); }
797
798 #[cfg(feature = "deb822")]
799 #[test]
800 fn test_parsed_entry_line_v5() {
801 let content = r#"Version: 5
802
803Source: https://example.com/repo1
804Matching-Pattern: .*\.tar\.gz
805
806Source: https://example.com/repo2
807Matching-Pattern: .*\.tar\.xz
808"#;
809 let wf = parse(content).unwrap();
810 let entries: Vec<_> = wf.entries().collect();
811
812 assert_eq!(entries[0].line(), 2); assert_eq!(entries[1].line(), 5); }
815}