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, Clone)]
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, Clone)]
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 snapshot(&self) -> Self {
142 match self {
143 #[cfg(feature = "linebased")]
144 ParsedWatchFile::LineBased(wf) => ParsedWatchFile::LineBased(wf.snapshot()),
145 #[cfg(feature = "deb822")]
146 ParsedWatchFile::Deb822(wf) => ParsedWatchFile::Deb822(wf.snapshot()),
147 }
148 }
149
150 pub fn tree_eq(&self, other: &Self) -> bool {
155 match (self, other) {
156 #[cfg(feature = "linebased")]
157 (ParsedWatchFile::LineBased(a), ParsedWatchFile::LineBased(b)) => a.tree_eq(b),
158 #[cfg(feature = "deb822")]
159 (ParsedWatchFile::Deb822(a), ParsedWatchFile::Deb822(b)) => a.tree_eq(b),
160 #[allow(unreachable_patterns)]
161 _ => false,
162 }
163 }
164
165 pub fn new(version: u32) -> Result<Self, ParseError> {
182 match version {
183 #[cfg(feature = "deb822")]
184 5 => Ok(ParsedWatchFile::Deb822(crate::deb822::WatchFile::new())),
185 #[cfg(not(feature = "deb822"))]
186 5 => Err(ParseError::FeatureNotEnabled(
187 "deb822 feature required for v5 format".to_string(),
188 )),
189 #[cfg(feature = "linebased")]
190 v @ 1..=4 => Ok(ParsedWatchFile::LineBased(
191 crate::linebased::WatchFile::new(Some(v)),
192 )),
193 #[cfg(not(feature = "linebased"))]
194 v @ 1..=4 => Err(ParseError::FeatureNotEnabled(format!(
195 "linebased feature required for v{} format",
196 v
197 ))),
198 v => Err(ParseError::FeatureNotEnabled(format!(
199 "unsupported watch file version: {}",
200 v
201 ))),
202 }
203 }
204
205 pub fn version(&self) -> u32 {
207 match self {
208 #[cfg(feature = "linebased")]
209 ParsedWatchFile::LineBased(wf) => wf.version(),
210 #[cfg(feature = "deb822")]
211 ParsedWatchFile::Deb822(wf) => wf.version(),
212 }
213 }
214
215 pub fn entries(&self) -> impl Iterator<Item = ParsedEntry> + '_ {
217 let entries: Vec<_> = match self {
219 #[cfg(feature = "linebased")]
220 ParsedWatchFile::LineBased(wf) => wf.entries().map(ParsedEntry::LineBased).collect(),
221 #[cfg(feature = "deb822")]
222 ParsedWatchFile::Deb822(wf) => wf.entries().map(ParsedEntry::Deb822).collect(),
223 };
224 entries.into_iter()
225 }
226
227 pub fn add_entry(&mut self, source: &str, matching_pattern: &str) -> ParsedEntry {
248 match self {
249 #[cfg(feature = "linebased")]
250 ParsedWatchFile::LineBased(wf) => {
251 let entry = crate::linebased::EntryBuilder::new(source)
252 .matching_pattern(matching_pattern)
253 .build();
254 let added_entry = wf.add_entry(entry);
255 ParsedEntry::LineBased(added_entry)
256 }
257 #[cfg(feature = "deb822")]
258 ParsedWatchFile::Deb822(wf) => {
259 let added_entry = wf.add_entry(source, matching_pattern);
260 ParsedEntry::Deb822(added_entry)
261 }
262 }
263 }
264
265 pub fn version_range(&self) -> Option<rowan::TextRange> {
272 match self {
273 #[cfg(feature = "linebased")]
274 ParsedWatchFile::LineBased(wf) => wf.version_node().map(|v| v.text_range()),
275 #[cfg(feature = "deb822")]
276 ParsedWatchFile::Deb822(wf) => {
277 let first = wf.as_deb822().paragraphs().next()?;
280 first.get_entry("Version").map(|e| e.text_range())
281 }
282 }
283 }
284}
285
286impl ParsedEntry {
287 pub fn url(&self) -> String {
289 match self {
290 #[cfg(feature = "linebased")]
291 ParsedEntry::LineBased(e) => e.url(),
292 #[cfg(feature = "deb822")]
293 ParsedEntry::Deb822(e) => e.source().unwrap_or(None).unwrap_or_default(),
294 }
295 }
296
297 pub fn matching_pattern(&self) -> Option<String> {
299 match self {
300 #[cfg(feature = "linebased")]
301 ParsedEntry::LineBased(e) => e.matching_pattern(),
302 #[cfg(feature = "deb822")]
303 ParsedEntry::Deb822(e) => e.matching_pattern().unwrap_or(None),
304 }
305 }
306
307 pub fn get_option(&self, key: &str) -> Option<String> {
313 match self {
314 #[cfg(feature = "linebased")]
315 ParsedEntry::LineBased(e) => e.get_option(key),
316 #[cfg(feature = "deb822")]
317 ParsedEntry::Deb822(e) => {
318 e.get_field(key).or_else(|| {
320 let mut chars = key.chars();
321 if let Some(first) = chars.next() {
322 let capitalized = first.to_uppercase().chain(chars).collect::<String>();
323 e.get_field(&capitalized)
324 } else {
325 None
326 }
327 })
328 }
329 }
330 }
331
332 pub fn has_option(&self, key: &str) -> bool {
334 self.get_option(key).is_some()
335 }
336
337 pub fn url_range(&self) -> Option<rowan::TextRange> {
344 match self {
345 #[cfg(feature = "linebased")]
346 ParsedEntry::LineBased(e) => e.url_node().map(|n| n.text_range()),
347 #[cfg(feature = "deb822")]
348 ParsedEntry::Deb822(e) => deb822_field_range(e.as_deb822(), &["Source", "URL"]),
349 }
350 }
351
352 pub fn matching_pattern_range(&self) -> Option<rowan::TextRange> {
357 match self {
358 #[cfg(feature = "linebased")]
359 ParsedEntry::LineBased(e) => e.matching_pattern_node().map(|n| n.text_range()),
360 #[cfg(feature = "deb822")]
361 ParsedEntry::Deb822(e) => deb822_field_range(e.as_deb822(), &["Matching-Pattern"]),
362 }
363 }
364
365 pub fn option_range(&self, key: &str) -> Option<rowan::TextRange> {
371 match self {
372 #[cfg(feature = "linebased")]
373 ParsedEntry::LineBased(e) => {
374 let list = e.option_list()?;
375 let opt = list.find_option(key)?;
376 Some(opt.text_range())
377 }
378 #[cfg(feature = "deb822")]
379 ParsedEntry::Deb822(e) => {
380 if let Some(r) = deb822_field_range(e.as_deb822(), &[key]) {
385 return Some(r);
386 }
387 let mut chars = key.chars();
388 if let Some(first) = chars.next() {
389 let capitalized = first.to_uppercase().chain(chars).collect::<String>();
390 deb822_field_range(e.as_deb822(), &[capitalized.as_str()])
391 } else {
392 None
393 }
394 }
395 }
396 }
397
398 pub fn version_policy_range(&self) -> Option<rowan::TextRange> {
404 match self {
405 #[cfg(feature = "linebased")]
406 ParsedEntry::LineBased(e) => e.version_node().map(|n| n.text_range()),
407 #[cfg(feature = "deb822")]
408 ParsedEntry::Deb822(_) => None,
409 }
410 }
411
412 pub fn template_range(&self) -> Option<rowan::TextRange> {
416 match self {
417 #[cfg(feature = "linebased")]
418 ParsedEntry::LineBased(_) => None,
419 #[cfg(feature = "deb822")]
420 ParsedEntry::Deb822(e) => deb822_field_range(e.as_deb822(), &["Template"]),
421 }
422 }
423
424 pub fn template_kind(&self) -> Option<String> {
428 match self {
429 #[cfg(feature = "linebased")]
430 ParsedEntry::LineBased(_) => None,
431 #[cfg(feature = "deb822")]
432 ParsedEntry::Deb822(e) => e.as_deb822().get("Template"),
433 }
434 }
435
436 pub fn script(&self) -> Option<String> {
438 self.get_option("script")
439 }
440
441 pub fn component(&self) -> Option<String> {
443 self.get_option("component")
444 }
445
446 pub fn format_url(
448 &self,
449 package: impl FnOnce() -> String,
450 component: impl FnOnce() -> String,
451 ) -> Result<url::Url, url::ParseError> {
452 crate::subst::subst(&self.url(), package, component).parse()
453 }
454
455 pub fn user_agent(&self) -> Option<String> {
457 self.get_option("user-agent")
458 }
459
460 pub fn pagemangle(&self) -> Option<String> {
462 self.get_option("pagemangle")
463 }
464
465 pub fn uversionmangle(&self) -> Option<String> {
467 self.get_option("uversionmangle")
468 }
469
470 pub fn downloadurlmangle(&self) -> Option<String> {
472 self.get_option("downloadurlmangle")
473 }
474
475 pub fn pgpsigurlmangle(&self) -> Option<String> {
477 self.get_option("pgpsigurlmangle")
478 }
479
480 pub fn filenamemangle(&self) -> Option<String> {
482 self.get_option("filenamemangle")
483 }
484
485 pub fn oversionmangle(&self) -> Option<String> {
487 self.get_option("oversionmangle")
488 }
489
490 pub fn searchmode(&self) -> crate::types::SearchMode {
492 self.get_option("searchmode")
493 .and_then(|s| s.parse().ok())
494 .unwrap_or_default()
495 }
496
497 pub fn set_option(&mut self, option: crate::types::WatchOption) {
519 match self {
520 #[cfg(feature = "linebased")]
521 ParsedEntry::LineBased(e) => {
522 e.set_option(option);
523 }
524 #[cfg(feature = "deb822")]
525 ParsedEntry::Deb822(e) => {
526 e.set_option(option);
527 }
528 }
529 }
530
531 pub fn set_url(&mut self, url: &str) {
547 match self {
548 #[cfg(feature = "linebased")]
549 ParsedEntry::LineBased(e) => e.set_url(url),
550 #[cfg(feature = "deb822")]
551 ParsedEntry::Deb822(e) => e.set_source(url),
552 }
553 }
554
555 pub fn set_matching_pattern(&mut self, pattern: &str) {
571 match self {
572 #[cfg(feature = "linebased")]
573 ParsedEntry::LineBased(e) => e.set_matching_pattern(pattern),
574 #[cfg(feature = "deb822")]
575 ParsedEntry::Deb822(e) => e.set_matching_pattern(pattern),
576 }
577 }
578
579 pub fn line(&self) -> usize {
599 match self {
600 #[cfg(feature = "linebased")]
601 ParsedEntry::LineBased(e) => e.line(),
602 #[cfg(feature = "deb822")]
603 ParsedEntry::Deb822(e) => e.line(),
604 }
605 }
606
607 pub fn remove_option(&mut self, option: crate::types::WatchOption) {
630 match self {
631 #[cfg(feature = "linebased")]
632 ParsedEntry::LineBased(e) => e.del_opt(option),
633 #[cfg(feature = "deb822")]
634 ParsedEntry::Deb822(e) => e.delete_option(option),
635 }
636 }
637
638 pub fn mode(&self) -> Result<crate::types::Mode, crate::types::ParseError> {
663 match self {
664 #[cfg(feature = "linebased")]
665 ParsedEntry::LineBased(e) => e.try_mode(),
666 #[cfg(feature = "deb822")]
667 ParsedEntry::Deb822(e) => e.mode(),
668 }
669 }
670}
671
672#[cfg(feature = "deb822")]
677fn deb822_field_range(
678 paragraph: &deb822_lossless::Paragraph,
679 names: &[&str],
680) -> Option<rowan::TextRange> {
681 for name in names {
682 if let Some(entry) = paragraph.get_entry(name) {
683 return Some(entry.text_range());
684 }
685 }
686 None
687}
688
689impl std::fmt::Display for ParsedWatchFile {
690 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
691 match self {
692 #[cfg(feature = "linebased")]
693 ParsedWatchFile::LineBased(wf) => write!(f, "{}", wf),
694 #[cfg(feature = "deb822")]
695 ParsedWatchFile::Deb822(wf) => write!(f, "{}", wf),
696 }
697 }
698}
699
700pub fn parse(content: &str) -> Result<ParsedWatchFile, ParseError> {
719 let version = detect_version(content).ok_or(ParseError::UnknownVersion)?;
720
721 match version {
722 #[cfg(feature = "linebased")]
723 WatchFileVersion::LineBased(_v) => {
724 let wf: crate::linebased::WatchFile = content.parse().map_err(ParseError::LineBased)?;
725 Ok(ParsedWatchFile::LineBased(wf))
726 }
727 #[cfg(not(feature = "linebased"))]
728 WatchFileVersion::LineBased(_v) => Err(ParseError::FeatureNotEnabled(
729 "linebased feature required for v1-4 formats".to_string(),
730 )),
731 #[cfg(feature = "deb822")]
732 WatchFileVersion::Deb822 => {
733 let wf: crate::deb822::WatchFile = content.parse().map_err(ParseError::Deb822)?;
734 Ok(ParsedWatchFile::Deb822(wf))
735 }
736 #[cfg(not(feature = "deb822"))]
737 WatchFileVersion::Deb822 => Err(ParseError::FeatureNotEnabled(
738 "deb822 feature required for v5 format".to_string(),
739 )),
740 }
741}
742
743#[cfg(test)]
744mod tests {
745 use super::*;
746
747 #[test]
748 fn test_detect_version_v1_default() {
749 let content = "https://example.com/ .*.tar.gz";
750 assert_eq!(
751 detect_version(content),
752 Some(WatchFileVersion::LineBased(1))
753 );
754 }
755
756 #[test]
757 fn test_detect_version_v4() {
758 let content = "version=4\nhttps://example.com/ .*.tar.gz";
759 assert_eq!(
760 detect_version(content),
761 Some(WatchFileVersion::LineBased(4))
762 );
763 }
764
765 #[test]
766 fn test_detect_version_v4_with_spaces() {
767 let content = "version = 4\nhttps://example.com/ .*.tar.gz";
768 assert_eq!(
769 detect_version(content),
770 Some(WatchFileVersion::LineBased(4))
771 );
772 }
773
774 #[test]
775 fn test_detect_version_v5() {
776 let content = "Version: 5\n\nSource: https://example.com/";
777 assert_eq!(detect_version(content), Some(WatchFileVersion::Deb822));
778 }
779
780 #[test]
781 fn test_detect_version_v5_lowercase() {
782 let content = "version: 5\n\nSource: https://example.com/";
783 assert_eq!(detect_version(content), Some(WatchFileVersion::Deb822));
784 }
785
786 #[test]
787 fn test_detect_version_with_leading_comments() {
788 let content = "# This is a comment\nversion=4\nhttps://example.com/ .*.tar.gz";
789 assert_eq!(
790 detect_version(content),
791 Some(WatchFileVersion::LineBased(4))
792 );
793 }
794
795 #[test]
796 fn test_detect_version_with_leading_whitespace() {
797 let content = " \n version=3\nhttps://example.com/ .*.tar.gz";
798 assert_eq!(
799 detect_version(content),
800 Some(WatchFileVersion::LineBased(3))
801 );
802 }
803
804 #[test]
805 fn test_detect_version_v2() {
806 let content = "version=2\nhttps://example.com/ .*.tar.gz";
807 assert_eq!(
808 detect_version(content),
809 Some(WatchFileVersion::LineBased(2))
810 );
811 }
812
813 #[cfg(feature = "linebased")]
814 #[test]
815 fn test_parse_linebased() {
816 let content = "version=4\nhttps://example.com/ .*.tar.gz";
817 let parsed = parse(content).unwrap();
818 assert_eq!(parsed.version(), 4);
819 }
820
821 #[cfg(feature = "deb822")]
822 #[test]
823 fn test_parse_deb822() {
824 let content = "Version: 5\n\nSource: https://example.com/\nMatching-Pattern: .*.tar.gz";
825 let parsed = parse(content).unwrap();
826 assert_eq!(parsed.version(), 5);
827 }
828
829 #[cfg(all(feature = "linebased", feature = "deb822"))]
830 #[test]
831 fn test_parse_both_formats() {
832 let v4_content = "version=4\nhttps://example.com/ .*.tar.gz";
834 let v4_parsed = parse(v4_content).unwrap();
835 assert_eq!(v4_parsed.version(), 4);
836
837 let v5_content = "Version: 5\n\nSource: https://example.com/\nMatching-Pattern: .*.tar.gz";
839 let v5_parsed = parse(v5_content).unwrap();
840 assert_eq!(v5_parsed.version(), 5);
841 }
842
843 #[cfg(feature = "linebased")]
844 #[test]
845 fn test_parse_roundtrip() {
846 let content = "version=4\n# Comment\nhttps://example.com/ .*.tar.gz";
847 let parsed = parse(content).unwrap();
848 let output = parsed.to_string();
849
850 let reparsed = parse(&output).unwrap();
852 assert_eq!(reparsed.version(), 4);
853 }
854
855 #[cfg(feature = "deb822")]
856 #[test]
857 fn test_parsed_watch_file_new_v5() {
858 let wf = ParsedWatchFile::new(5).unwrap();
859 assert_eq!(wf.version(), 5);
860 assert_eq!(wf.entries().count(), 0);
861 }
862
863 #[cfg(feature = "linebased")]
864 #[test]
865 fn test_parsed_watch_file_new_v4() {
866 let wf = ParsedWatchFile::new(4).unwrap();
867 assert_eq!(wf.version(), 4);
868 assert_eq!(wf.entries().count(), 0);
869 }
870
871 #[cfg(feature = "deb822")]
872 #[test]
873 fn test_parsed_watch_file_add_entry_v5() {
874 let mut wf = ParsedWatchFile::new(5).unwrap();
875 let mut entry = wf.add_entry("https://github.com/foo/bar/tags", r".*/v?([\d.]+)\.tar\.gz");
876
877 assert_eq!(wf.entries().count(), 1);
878 assert_eq!(entry.url(), "https://github.com/foo/bar/tags");
879 assert_eq!(
880 entry.matching_pattern(),
881 Some(r".*/v?([\d.]+)\.tar\.gz".to_string())
882 );
883
884 entry.set_option(crate::types::WatchOption::Component("upstream".to_string()));
886 entry.set_option(crate::types::WatchOption::Compression(
887 crate::types::Compression::Xz,
888 ));
889
890 assert_eq!(entry.get_option("Component"), Some("upstream".to_string()));
891 assert_eq!(entry.get_option("Compression"), Some("xz".to_string()));
892 }
893
894 #[cfg(feature = "linebased")]
895 #[test]
896 fn test_parsed_watch_file_add_entry_v4() {
897 let mut wf = ParsedWatchFile::new(4).unwrap();
898 let entry = wf.add_entry("https://github.com/foo/bar/tags", r".*/v?([\d.]+)\.tar\.gz");
899
900 assert_eq!(wf.entries().count(), 1);
901 assert_eq!(entry.url(), "https://github.com/foo/bar/tags");
902 assert_eq!(
903 entry.matching_pattern(),
904 Some(r".*/v?([\d.]+)\.tar\.gz".to_string())
905 );
906 }
907
908 #[cfg(feature = "deb822")]
909 #[test]
910 fn test_parsed_watch_file_roundtrip_with_add_entry() {
911 let mut wf = ParsedWatchFile::new(5).unwrap();
912 let mut entry = wf.add_entry(
913 "https://github.com/owner/repo/tags",
914 r".*/v?([\d.]+)\.tar\.gz",
915 );
916 entry.set_option(crate::types::WatchOption::Compression(
917 crate::types::Compression::Xz,
918 ));
919
920 let output = wf.to_string();
921
922 let reparsed = parse(&output).unwrap();
924 assert_eq!(reparsed.version(), 5);
925
926 let entries: Vec<_> = reparsed.entries().collect();
927 assert_eq!(entries.len(), 1);
928 assert_eq!(entries[0].url(), "https://github.com/owner/repo/tags");
929 assert_eq!(entries[0].get_option("Compression"), Some("xz".to_string()));
930 }
931
932 #[cfg(feature = "linebased")]
933 #[test]
934 fn test_parsed_entry_set_url_v4() {
935 let mut wf = ParsedWatchFile::new(4).unwrap();
936 let mut entry = wf.add_entry("https://github.com/foo/bar/tags", r".*/v?([\d.]+)\.tar\.gz");
937
938 assert_eq!(entry.url(), "https://github.com/foo/bar/tags");
939
940 entry.set_url("https://github.com/foo/bar/releases");
941 assert_eq!(entry.url(), "https://github.com/foo/bar/releases");
942 }
943
944 #[cfg(feature = "deb822")]
945 #[test]
946 fn test_parsed_entry_set_url_v5() {
947 let mut wf = ParsedWatchFile::new(5).unwrap();
948 let mut entry = wf.add_entry("https://github.com/foo/bar/tags", r".*/v?([\d.]+)\.tar\.gz");
949
950 assert_eq!(entry.url(), "https://github.com/foo/bar/tags");
951
952 entry.set_url("https://github.com/foo/bar/releases");
953 assert_eq!(entry.url(), "https://github.com/foo/bar/releases");
954 }
955
956 #[cfg(feature = "linebased")]
957 #[test]
958 fn test_parsed_entry_set_matching_pattern_v4() {
959 let mut wf = ParsedWatchFile::new(4).unwrap();
960 let mut entry = wf.add_entry("https://github.com/foo/bar/tags", r".*/v?([\d.]+)\.tar\.gz");
961
962 assert_eq!(
963 entry.matching_pattern(),
964 Some(r".*/v?([\d.]+)\.tar\.gz".to_string())
965 );
966
967 entry.set_matching_pattern(r".*/release-([\d.]+)\.tar\.gz");
968 assert_eq!(
969 entry.matching_pattern(),
970 Some(r".*/release-([\d.]+)\.tar\.gz".to_string())
971 );
972 }
973
974 #[cfg(feature = "deb822")]
975 #[test]
976 fn test_parsed_entry_set_matching_pattern_v5() {
977 let mut wf = ParsedWatchFile::new(5).unwrap();
978 let mut entry = wf.add_entry("https://github.com/foo/bar/tags", r".*/v?([\d.]+)\.tar\.gz");
979
980 assert_eq!(
981 entry.matching_pattern(),
982 Some(r".*/v?([\d.]+)\.tar\.gz".to_string())
983 );
984
985 entry.set_matching_pattern(r".*/release-([\d.]+)\.tar\.gz");
986 assert_eq!(
987 entry.matching_pattern(),
988 Some(r".*/release-([\d.]+)\.tar\.gz".to_string())
989 );
990 }
991
992 #[cfg(feature = "linebased")]
993 #[test]
994 fn test_parsed_entry_line_v4() {
995 let content = "version=4\nhttps://example.com/ .*.tar.gz\nhttps://example2.com/ .*.tar.gz";
996 let wf = parse(content).unwrap();
997 let entries: Vec<_> = wf.entries().collect();
998
999 assert_eq!(entries[0].line(), 1); assert_eq!(entries[1].line(), 2); }
1002
1003 #[cfg(feature = "deb822")]
1004 #[test]
1005 fn test_parsed_entry_line_v5() {
1006 let content = r#"Version: 5
1007
1008Source: https://example.com/repo1
1009Matching-Pattern: .*\.tar\.gz
1010
1011Source: https://example.com/repo2
1012Matching-Pattern: .*\.tar\.xz
1013"#;
1014 let wf = parse(content).unwrap();
1015 let entries: Vec<_> = wf.entries().collect();
1016
1017 assert_eq!(entries[0].line(), 2); assert_eq!(entries[1].line(), 5); }
1020
1021 #[cfg(feature = "linebased")]
1022 #[test]
1023 fn test_url_range_linebased() {
1024 let content = "version=4\nhttps://example.com/ .*-([\\d.]+)\\.tar\\.gz\n";
1025 let wf = parse(content).unwrap();
1026 let entry = wf.entries().next().unwrap();
1027 let range = entry.url_range().expect("entry has url");
1028 let start: usize = range.start().into();
1029 let end: usize = range.end().into();
1030 assert_eq!(&content[start..end], "https://example.com/");
1031 }
1032
1033 #[cfg(feature = "linebased")]
1034 #[test]
1035 fn test_matching_pattern_range_linebased() {
1036 let content = "version=4\nhttps://example.com/ .*-([\\d.]+)\\.tar\\.gz\n";
1037 let wf = parse(content).unwrap();
1038 let entry = wf.entries().next().unwrap();
1039 let range = entry.matching_pattern_range().expect("has pattern");
1040 let start: usize = range.start().into();
1041 let end: usize = range.end().into();
1042 assert_eq!(&content[start..end], ".*-([\\d.]+)\\.tar\\.gz");
1043 }
1044
1045 #[cfg(feature = "linebased")]
1046 #[test]
1047 fn test_option_range_linebased() {
1048 let content = "version=4\nopts=mode=git,pretty=raw https://example.com/ .*\n";
1049 let wf = parse(content).unwrap();
1050 let entry = wf.entries().next().unwrap();
1051 let mode = entry.option_range("mode").expect("mode option");
1052 let start: usize = mode.start().into();
1053 let end: usize = mode.end().into();
1054 assert_eq!(&content[start..end], "mode=git");
1055
1056 let pretty = entry.option_range("pretty").expect("pretty option");
1057 let start: usize = pretty.start().into();
1058 let end: usize = pretty.end().into();
1059 assert_eq!(&content[start..end], "pretty=raw");
1060
1061 assert!(entry.option_range("not-a-real-option").is_none());
1062 }
1063
1064 #[cfg(feature = "linebased")]
1065 #[test]
1066 fn test_version_range_linebased() {
1067 let content = "version=4\nhttps://example.com/ .*\n";
1068 let wf = parse(content).unwrap();
1069 let range = wf.version_range().expect("has version");
1070 let start: usize = range.start().into();
1071 let end: usize = range.end().into();
1072 assert_eq!(&content[start..end], "version=4\n");
1073 }
1074
1075 #[cfg(feature = "deb822")]
1076 #[test]
1077 fn test_url_range_deb822() {
1078 let content =
1079 "Version: 5\n\nSource: https://example.com/foo\nMatching-Pattern: .*\\.tar\\.gz\n";
1080 let wf = parse(content).unwrap();
1081 let entry = wf.entries().next().unwrap();
1082 let range = entry.url_range().expect("has source");
1083 let start: usize = range.start().into();
1084 let end: usize = range.end().into();
1085 assert_eq!(&content[start..end], "Source: https://example.com/foo\n");
1088 }
1089
1090 #[cfg(feature = "deb822")]
1091 #[test]
1092 fn test_matching_pattern_range_deb822() {
1093 let content =
1094 "Version: 5\n\nSource: https://example.com/foo\nMatching-Pattern: v(.+)\\.tar\\.gz\n";
1095 let wf = parse(content).unwrap();
1096 let entry = wf.entries().next().unwrap();
1097 let range = entry.matching_pattern_range().expect("has pattern");
1098 let start: usize = range.start().into();
1099 let end: usize = range.end().into();
1100 assert_eq!(&content[start..end], "Matching-Pattern: v(.+)\\.tar\\.gz\n");
1101 }
1102
1103 #[cfg(feature = "deb822")]
1104 #[test]
1105 fn test_option_range_deb822_lookup_capitalises_key() {
1106 let content =
1111 "Version: 5\n\nSource: https://example.com/foo\nMatching-Pattern: x\nMode: git\n";
1112 let wf = parse(content).unwrap();
1113 let entry = wf.entries().next().unwrap();
1114 let range = entry.option_range("mode").expect("mode field");
1115 let start: usize = range.start().into();
1116 let end: usize = range.end().into();
1117 assert_eq!(&content[start..end], "Mode: git\n");
1118 }
1119
1120 #[cfg(feature = "deb822")]
1121 #[test]
1122 fn test_version_range_deb822() {
1123 let content = "Version: 5\n\nSource: https://example.com/foo\nMatching-Pattern: x\n";
1124 let wf = parse(content).unwrap();
1125 let range = wf.version_range().expect("has version");
1126 let start: usize = range.start().into();
1127 let end: usize = range.end().into();
1128 assert_eq!(&content[start..end], "Version: 5\n");
1129 }
1130
1131 #[cfg(feature = "deb822")]
1132 #[test]
1133 fn test_template_range_deb822() {
1134 let content = "Version: 5\n\nSource: https://github.com/foo/bar\nTemplate: GitHub\n";
1135 let wf = parse(content).unwrap();
1136 let entry = wf.entries().next().unwrap();
1137 let range = entry.template_range().expect("has template");
1138 let start: usize = range.start().into();
1139 let end: usize = range.end().into();
1140 assert_eq!(&content[start..end], "Template: GitHub\n");
1141 assert_eq!(entry.template_kind(), Some("GitHub".to_string()));
1142 }
1143}
1144
1145#[derive(Clone, PartialEq, Eq)]
1151pub struct Parse {
1152 inner: ParseInner,
1153}
1154
1155#[derive(Clone, PartialEq, Eq)]
1156enum ParseInner {
1157 #[cfg(feature = "linebased")]
1158 LineBased(crate::linebased::Parse<crate::linebased::WatchFile>),
1159 #[cfg(feature = "deb822")]
1160 Deb822(deb822_lossless::Parse<deb822_lossless::Deb822>),
1161}
1162
1163impl Parse {
1164 pub fn parse(text: &str) -> Self {
1166 let version = detect_version(text);
1167
1168 let inner = match version {
1169 #[cfg(feature = "linebased")]
1170 Some(WatchFileVersion::LineBased(_)) => {
1171 ParseInner::LineBased(crate::linebased::parse_watch_file(text))
1172 }
1173 #[cfg(feature = "deb822")]
1174 Some(WatchFileVersion::Deb822) => {
1175 ParseInner::Deb822(deb822_lossless::Deb822::parse(text))
1176 }
1177 #[cfg(not(feature = "linebased"))]
1178 Some(WatchFileVersion::LineBased(_)) => {
1179 #[cfg(feature = "deb822")]
1181 {
1182 ParseInner::Deb822(deb822_lossless::Deb822::parse(text))
1183 }
1184 #[cfg(not(feature = "deb822"))]
1185 {
1186 panic!("No watch file parsing features enabled")
1187 }
1188 }
1189 #[cfg(not(feature = "deb822"))]
1190 Some(WatchFileVersion::Deb822) => {
1191 #[cfg(feature = "linebased")]
1193 {
1194 ParseInner::LineBased(crate::linebased::parse_watch_file(text))
1195 }
1196 #[cfg(not(feature = "linebased"))]
1197 {
1198 panic!("No watch file parsing features enabled")
1199 }
1200 }
1201 None => {
1202 #[cfg(feature = "linebased")]
1204 {
1205 ParseInner::LineBased(crate::linebased::parse_watch_file(text))
1206 }
1207 #[cfg(not(feature = "linebased"))]
1208 #[cfg(feature = "deb822")]
1209 {
1210 ParseInner::Deb822(deb822_lossless::Deb822::parse(text))
1211 }
1212 #[cfg(not(any(feature = "linebased", feature = "deb822")))]
1213 {
1214 panic!("No watch file parsing features enabled")
1215 }
1216 }
1217 };
1218
1219 Parse { inner }
1220 }
1221
1222 pub fn to_watch_file(&self) -> ParsedWatchFile {
1224 match &self.inner {
1225 #[cfg(feature = "linebased")]
1226 ParseInner::LineBased(parse) => ParsedWatchFile::LineBased(parse.tree()),
1227 #[cfg(feature = "deb822")]
1228 ParseInner::Deb822(parse) => {
1229 let deb822 = parse.tree();
1230 ParsedWatchFile::Deb822(crate::deb822::WatchFile::from_deb822(deb822))
1231 }
1232 }
1233 }
1234
1235 pub fn version(&self) -> u32 {
1237 match &self.inner {
1238 #[cfg(feature = "linebased")]
1239 ParseInner::LineBased(parse) => parse.tree().version(),
1240 #[cfg(feature = "deb822")]
1241 ParseInner::Deb822(_) => 5,
1242 }
1243 }
1244}
1245
1246unsafe impl Send for Parse {}
1249unsafe impl Sync for Parse {}