Skip to main content

debian_watch/
parse.rs

1#![cfg(any(feature = "linebased", feature = "deb822"))]
2//! Format detection and parsing for watch files
3//!
4//! This module is only available when at least one of the `linebased` or `deb822` features is enabled.
5
6/// Error type for parsing watch files
7#[derive(Debug)]
8pub enum ParseError {
9    /// Error parsing line-based format (v1-4)
10    #[cfg(feature = "linebased")]
11    LineBased(crate::linebased::ParseError),
12    /// Error parsing deb822 format (v5)
13    #[cfg(feature = "deb822")]
14    Deb822(crate::deb822::ParseError),
15    /// Could not detect version
16    UnknownVersion,
17    /// Feature not enabled
18    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/// Detected watch file format
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub enum WatchFileVersion {
39    /// Line-based format (versions 1-4)
40    LineBased(u32),
41    /// Deb822 format (version 5)
42    Deb822,
43}
44
45/// Detect the version/format of a watch file from its content
46///
47/// This function examines the content to determine if it's a line-based
48/// format (v1-4) or deb822 format (v5).
49///
50/// After detecting the version, you can either:
51/// - Use the `parse()` function to automatically parse and return a `ParsedWatchFile`
52/// - Parse directly: `content.parse::<debian_watch::linebased::WatchFile>()`
53///
54/// # Examples
55///
56/// ```
57/// use debian_watch::parse::{detect_version, WatchFileVersion};
58///
59/// let v4_content = "version=4\nhttps://example.com/ .*.tar.gz";
60/// assert_eq!(detect_version(v4_content), Some(WatchFileVersion::LineBased(4)));
61///
62/// let v5_content = "Version: 5\n\nSource: https://example.com/";
63/// assert_eq!(detect_version(v5_content), Some(WatchFileVersion::Deb822));
64/// ```
65pub fn detect_version(content: &str) -> Option<WatchFileVersion> {
66    let trimmed = content.trim_start();
67
68    // Check if it starts with RFC822-style "Version: 5"
69    if trimmed.starts_with("Version:") || trimmed.starts_with("version:") {
70        // Try to extract the version number
71        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    // Otherwise, it's line-based format
82    // Try to detect the version from "version=N" line
83    for line in trimmed.lines() {
84        let line = line.trim();
85
86        // Skip comments and blank lines
87        if line.starts_with('#') || line.is_empty() {
88            continue;
89        }
90
91        // Check for version=N
92        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        // If we hit a non-comment, non-version line, assume default version
105        break;
106    }
107
108    // Default to version 1 for line-based format
109    Some(WatchFileVersion::LineBased(crate::DEFAULT_VERSION))
110}
111
112/// Parsed watch file that can be either line-based or deb822 format
113#[derive(Debug)]
114pub enum ParsedWatchFile {
115    /// Line-based watch file (v1-4)
116    #[cfg(feature = "linebased")]
117    LineBased(crate::linebased::WatchFile),
118    /// Deb822 watch file (v5)
119    #[cfg(feature = "deb822")]
120    Deb822(crate::deb822::WatchFile),
121}
122
123/// Parsed watch entry that can be either line-based or deb822 format
124#[derive(Debug)]
125pub enum ParsedEntry {
126    /// Line-based entry (v1-4)
127    #[cfg(feature = "linebased")]
128    LineBased(crate::linebased::Entry),
129    /// Deb822 entry (v5)
130    #[cfg(feature = "deb822")]
131    Deb822(crate::deb822::Entry),
132}
133
134impl ParsedWatchFile {
135    /// Create a new empty watch file with the specified version.
136    ///
137    /// - For version 5, creates a deb822-format watch file (requires `deb822` feature)
138    /// - For versions 1-4, creates a line-based watch file (requires `linebased` feature)
139    ///
140    /// # Examples
141    ///
142    /// ```
143    /// # #[cfg(feature = "deb822")]
144    /// # {
145    /// use debian_watch::parse::ParsedWatchFile;
146    ///
147    /// let wf = ParsedWatchFile::new(5).unwrap();
148    /// assert_eq!(wf.version(), 5);
149    /// # }
150    /// ```
151    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    /// Get the version of the watch file
176    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    /// Get an iterator over entries as ParsedEntry enum
186    pub fn entries(&self) -> impl Iterator<Item = ParsedEntry> + '_ {
187        // We need to collect because we can't return different iterator types from match arms
188        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    /// Add a new entry to the watch file and return it.
198    ///
199    /// For v5 (deb822) watch files, this adds a new paragraph with Source and Matching-Pattern fields.
200    /// For v1-4 (line-based) watch files, this adds a new entry line.
201    ///
202    /// Returns a `ParsedEntry` that can be used to query or modify the entry.
203    ///
204    /// # Examples
205    ///
206    /// ```
207    /// # #[cfg(feature = "deb822")]
208    /// # {
209    /// use debian_watch::parse::ParsedWatchFile;
210    /// use debian_watch::WatchOption;
211    ///
212    /// let mut wf = ParsedWatchFile::new(5).unwrap();
213    /// let mut entry = wf.add_entry("https://github.com/foo/bar/tags", ".*/v?([\\d.]+)\\.tar\\.gz");
214    /// entry.set_option(WatchOption::Component("upstream".to_string()));
215    /// # }
216    /// ```
217    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    /// Get the URL/Source of the entry
238    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    /// Get the matching pattern
248    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    /// Get a generic option/field value by key (case-insensitive)
258    ///
259    /// This handles the difference between line-based format (lowercase keys)
260    /// and deb822 format (capitalized keys). It tries the key as-is first,
261    /// then tries with the first letter capitalized.
262    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                // Try exact match first, then try capitalized
269                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    /// Check if an option/field is set (case-insensitive)
283    pub fn has_option(&self, key: &str) -> bool {
284        self.get_option(key).is_some()
285    }
286
287    /// Get the script
288    pub fn script(&self) -> Option<String> {
289        self.get_option("script")
290    }
291
292    /// Format the URL with package substitution
293    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    /// Get the user agent
301    pub fn user_agent(&self) -> Option<String> {
302        self.get_option("user-agent")
303    }
304
305    /// Get the pagemangle option
306    pub fn pagemangle(&self) -> Option<String> {
307        self.get_option("pagemangle")
308    }
309
310    /// Get the uversionmangle option
311    pub fn uversionmangle(&self) -> Option<String> {
312        self.get_option("uversionmangle")
313    }
314
315    /// Get the downloadurlmangle option
316    pub fn downloadurlmangle(&self) -> Option<String> {
317        self.get_option("downloadurlmangle")
318    }
319
320    /// Get the pgpsigurlmangle option
321    pub fn pgpsigurlmangle(&self) -> Option<String> {
322        self.get_option("pgpsigurlmangle")
323    }
324
325    /// Get the filenamemangle option
326    pub fn filenamemangle(&self) -> Option<String> {
327        self.get_option("filenamemangle")
328    }
329
330    /// Get the oversionmangle option
331    pub fn oversionmangle(&self) -> Option<String> {
332        self.get_option("oversionmangle")
333    }
334
335    /// Get the searchmode, with default fallback
336    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    /// Set an option/field value using a WatchOption enum.
343    ///
344    /// For v5 (deb822) entries, this sets a field in the paragraph.
345    /// For v1-4 (line-based) entries, this sets an option in the opts= list.
346    ///
347    /// # Examples
348    ///
349    /// ```
350    /// # #[cfg(feature = "linebased")]
351    /// # {
352    /// use debian_watch::parse::ParsedWatchFile;
353    /// use debian_watch::{WatchOption, Compression};
354    ///
355    /// let mut wf = ParsedWatchFile::new(4).unwrap();
356    /// let mut entry = wf.add_entry("https://github.com/foo/bar/tags", ".*/v?([\\d.]+)\\.tar\\.gz");
357    /// entry.set_option(WatchOption::Component("upstream".to_string()));
358    /// entry.set_option(WatchOption::Compression(Compression::Xz));
359    /// assert_eq!(entry.get_option("component"), Some("upstream".to_string()));
360    /// assert_eq!(entry.get_option("compression"), Some("xz".to_string()));
361    /// # }
362    /// ```
363    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    /// Set the URL/Source of the entry
377    ///
378    /// # Examples
379    ///
380    /// ```
381    /// # #[cfg(feature = "linebased")]
382    /// # {
383    /// use debian_watch::parse::ParsedWatchFile;
384    ///
385    /// let mut wf = ParsedWatchFile::new(4).unwrap();
386    /// let mut entry = wf.add_entry("https://github.com/foo/bar/tags", ".*/v?([\\d.]+)\\.tar\\.gz");
387    /// entry.set_url("https://github.com/foo/bar/releases");
388    /// assert_eq!(entry.url(), "https://github.com/foo/bar/releases");
389    /// # }
390    /// ```
391    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    /// Set the matching pattern of the entry
401    ///
402    /// # Examples
403    ///
404    /// ```
405    /// # #[cfg(feature = "linebased")]
406    /// # {
407    /// use debian_watch::parse::ParsedWatchFile;
408    ///
409    /// let mut wf = ParsedWatchFile::new(4).unwrap();
410    /// let mut entry = wf.add_entry("https://github.com/foo/bar/tags", ".*/v?([\\d.]+)\\.tar\\.gz");
411    /// entry.set_matching_pattern(".*/release-([\\d.]+)\\.tar\\.gz");
412    /// assert_eq!(entry.matching_pattern(), Some(".*/release-([\\d.]+)\\.tar\\.gz".to_string()));
413    /// # }
414    /// ```
415    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    /// Get the line number (0-indexed) where this entry starts
425    ///
426    /// For line-based formats (v1-4), this returns the actual line number in the file.
427    /// For deb822 format (v5), this returns the line where the paragraph starts.
428    ///
429    /// # Examples
430    ///
431    /// ```
432    /// # #[cfg(feature = "linebased")]
433    /// # {
434    /// use debian_watch::parse::parse;
435    ///
436    /// let content = "version=4\nhttps://example.com/ .*.tar.gz\nhttps://example2.com/ .*.tar.gz";
437    /// let wf = parse(content).unwrap();
438    /// let entries: Vec<_> = wf.entries().collect();
439    /// assert_eq!(entries[0].line(), 1); // Second line (0-indexed)
440    /// assert_eq!(entries[1].line(), 2); // Third line (0-indexed)
441    /// # }
442    /// ```
443    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    /// Remove/delete an option from the entry
453    ///
454    /// For v5 (deb822) entries, this removes a field from the paragraph.
455    /// For v1-4 (line-based) entries, this removes an option from the opts= list.
456    /// If this is the last option in a line-based entry, the entire opts= declaration is removed.
457    ///
458    /// # Examples
459    ///
460    /// ```
461    /// # #[cfg(feature = "linebased")]
462    /// # {
463    /// use debian_watch::parse::ParsedWatchFile;
464    /// use debian_watch::WatchOption;
465    ///
466    /// let mut wf = ParsedWatchFile::new(4).unwrap();
467    /// let mut entry = wf.add_entry("https://github.com/foo/bar/tags", ".*/v?([\\d.]+)\\.tar\\.gz");
468    /// entry.set_option(WatchOption::Compression(debian_watch::Compression::Xz));
469    /// assert!(entry.has_option("compression"));
470    /// entry.remove_option(WatchOption::Compression(debian_watch::Compression::Xz));
471    /// assert!(!entry.has_option("compression"));
472    /// # }
473    /// ```
474    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
495/// Parse a watch file with automatic format detection
496///
497/// This function detects whether the input is line-based (v1-4) or
498/// deb822 format (v5) and parses it accordingly, returning a unified
499/// ParsedWatchFile enum.
500///
501/// # Examples
502///
503/// ```
504/// # #[cfg(feature = "linebased")]
505/// # {
506/// use debian_watch::parse::parse;
507///
508/// let content = "version=4\nhttps://example.com/ .*.tar.gz";
509/// let parsed = parse(content).unwrap();
510/// assert_eq!(parsed.version(), 4);
511/// # }
512/// ```
513pub 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        // Test v4
628        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        // Test v5
633        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        // Parse again
646        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        // Test setting options with enum
680        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        // Parse again
718        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); // Second line (0-indexed)
795        assert_eq!(entries[1].line(), 2); // Third line (0-indexed)
796    }
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); // Third line (0-indexed)
813        assert_eq!(entries[1].line(), 5); // Sixth line (0-indexed)
814    }
815}