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, Clone)]
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, Clone)]
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    /// Capture an independent snapshot of this watch file.
136    ///
137    /// The returned value shares the underlying immutable green-node data
138    /// with `self` at the time of the call, but lives in its own mutable
139    /// tree: subsequent mutations to `self` do not propagate to the snapshot.
140    /// Pair with [`Self::tree_eq`] to detect later mutations.
141    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    /// Returns true iff the syntax trees of `self` and `other` are
151    /// value-equal. An O(1) pointer-identity fast path makes this free for
152    /// trees that still share state with a recent [`Self::snapshot`].
153    /// Mismatched variants are never considered equal.
154    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    /// Create a new empty watch file with the specified version.
166    ///
167    /// - For version 5, creates a deb822-format watch file (requires `deb822` feature)
168    /// - For versions 1-4, creates a line-based watch file (requires `linebased` feature)
169    ///
170    /// # Examples
171    ///
172    /// ```
173    /// # #[cfg(feature = "deb822")]
174    /// # {
175    /// use debian_watch::parse::ParsedWatchFile;
176    ///
177    /// let wf = ParsedWatchFile::new(5).unwrap();
178    /// assert_eq!(wf.version(), 5);
179    /// # }
180    /// ```
181    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    /// Get the version of the watch file
206    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    /// Get an iterator over entries as ParsedEntry enum
216    pub fn entries(&self) -> impl Iterator<Item = ParsedEntry> + '_ {
217        // We need to collect because we can't return different iterator types from match arms
218        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    /// Add a new entry to the watch file and return it.
228    ///
229    /// For v5 (deb822) watch files, this adds a new paragraph with Source and Matching-Pattern fields.
230    /// For v1-4 (line-based) watch files, this adds a new entry line.
231    ///
232    /// Returns a `ParsedEntry` that can be used to query or modify the entry.
233    ///
234    /// # Examples
235    ///
236    /// ```
237    /// # #[cfg(feature = "deb822")]
238    /// # {
239    /// use debian_watch::parse::ParsedWatchFile;
240    /// use debian_watch::WatchOption;
241    ///
242    /// let mut wf = ParsedWatchFile::new(5).unwrap();
243    /// let mut entry = wf.add_entry("https://github.com/foo/bar/tags", ".*/v?([\\d.]+)\\.tar\\.gz");
244    /// entry.set_option(WatchOption::Component("upstream".to_string()));
245    /// # }
246    /// ```
247    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    /// Byte range of the version declaration.
266    ///
267    /// In line-based files this is the `version=N` directive on the
268    /// first line; in deb822 files it's the `Version:` entry on the
269    /// header paragraph. Returns `None` when the file has no version
270    /// declaration (legal for v1 line-based files; unusual for v5).
271    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                // The header paragraph in v5 carries `Version:`; it's
278                // the first paragraph in the deb822 document.
279                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    /// Get the URL/Source of the entry
288    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    /// Get the matching pattern
298    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    /// Get a generic option/field value by key (case-insensitive)
308    ///
309    /// This handles the difference between line-based format (lowercase keys)
310    /// and deb822 format (capitalized keys). It tries the key as-is first,
311    /// then tries with the first letter capitalized.
312    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                // Try exact match first, then try capitalized
319                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    /// Check if an option/field is set (case-insensitive)
333    pub fn has_option(&self, key: &str) -> bool {
334        self.get_option(key).is_some()
335    }
336
337    /// Byte range of the source URL within the buffer.
338    ///
339    /// In line-based format this covers the URL token; in deb822 format
340    /// it covers the `Source:` (or `URL:`) entry as a whole — key,
341    /// separator, and value. Returns `None` when the entry has no
342    /// recognisable source.
343    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    /// Byte range of the matching-pattern within the buffer.
353    ///
354    /// Returns `None` when the entry has no matching pattern (either
355    /// not yet set, or the entry is a template).
356    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    /// Byte range of the named option's `key=value` pair (line-based)
366    /// or `Key: value` entry (deb822).
367    ///
368    /// `key` is matched case-insensitively, mirroring `get_option`.
369    /// Returns `None` if the option is unset.
370    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                // Try the key as-is, then capitalised — same shape as
381                // `get_option`, since deb822 uses `Component` /
382                // `Mode` / `Pgpsigurlmangle` while line-based uses
383                // lowercase.
384                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    /// Byte range of the version-policy / `version=...` part of the
399    /// entry, in line-based files. Returns `None` when not set, or when
400    /// this is a deb822 entry (per-file `Version:` lives on the header
401    /// paragraph, not on individual entries — use
402    /// [`ParsedWatchFile::version_range`] for that).
403    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    /// Byte range of the `Template:` field in this entry, when the
413    /// entry uses one. Templates are a v5 (deb822) feature only;
414    /// line-based entries always return `None`.
415    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    /// Template kind for this entry (e.g. `"GitHub"`, `"PyPI"`,
425    /// `"CRAN"`), if the entry uses one. Line-based entries always
426    /// return `None`.
427    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    /// Get the script
437    pub fn script(&self) -> Option<String> {
438        self.get_option("script")
439    }
440
441    /// Get the component name (empty for main paragraph)
442    pub fn component(&self) -> Option<String> {
443        self.get_option("component")
444    }
445
446    /// Format the URL with package and component substitution
447    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    /// Get the user agent
456    pub fn user_agent(&self) -> Option<String> {
457        self.get_option("user-agent")
458    }
459
460    /// Get the pagemangle option
461    pub fn pagemangle(&self) -> Option<String> {
462        self.get_option("pagemangle")
463    }
464
465    /// Get the uversionmangle option
466    pub fn uversionmangle(&self) -> Option<String> {
467        self.get_option("uversionmangle")
468    }
469
470    /// Get the downloadurlmangle option
471    pub fn downloadurlmangle(&self) -> Option<String> {
472        self.get_option("downloadurlmangle")
473    }
474
475    /// Get the pgpsigurlmangle option
476    pub fn pgpsigurlmangle(&self) -> Option<String> {
477        self.get_option("pgpsigurlmangle")
478    }
479
480    /// Get the filenamemangle option
481    pub fn filenamemangle(&self) -> Option<String> {
482        self.get_option("filenamemangle")
483    }
484
485    /// Get the oversionmangle option
486    pub fn oversionmangle(&self) -> Option<String> {
487        self.get_option("oversionmangle")
488    }
489
490    /// Get the searchmode, with default fallback
491    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    /// Set an option/field value using a WatchOption enum.
498    ///
499    /// For v5 (deb822) entries, this sets a field in the paragraph.
500    /// For v1-4 (line-based) entries, this sets an option in the opts= list.
501    ///
502    /// # Examples
503    ///
504    /// ```
505    /// # #[cfg(feature = "linebased")]
506    /// # {
507    /// use debian_watch::parse::ParsedWatchFile;
508    /// use debian_watch::{WatchOption, Compression};
509    ///
510    /// let mut wf = ParsedWatchFile::new(4).unwrap();
511    /// let mut entry = wf.add_entry("https://github.com/foo/bar/tags", ".*/v?([\\d.]+)\\.tar\\.gz");
512    /// entry.set_option(WatchOption::Component("upstream".to_string()));
513    /// entry.set_option(WatchOption::Compression(Compression::Xz));
514    /// assert_eq!(entry.get_option("component"), Some("upstream".to_string()));
515    /// assert_eq!(entry.get_option("compression"), Some("xz".to_string()));
516    /// # }
517    /// ```
518    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    /// Set the URL/Source of the entry
532    ///
533    /// # Examples
534    ///
535    /// ```
536    /// # #[cfg(feature = "linebased")]
537    /// # {
538    /// use debian_watch::parse::ParsedWatchFile;
539    ///
540    /// let mut wf = ParsedWatchFile::new(4).unwrap();
541    /// let mut entry = wf.add_entry("https://github.com/foo/bar/tags", ".*/v?([\\d.]+)\\.tar\\.gz");
542    /// entry.set_url("https://github.com/foo/bar/releases");
543    /// assert_eq!(entry.url(), "https://github.com/foo/bar/releases");
544    /// # }
545    /// ```
546    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    /// Set the matching pattern of the entry
556    ///
557    /// # Examples
558    ///
559    /// ```
560    /// # #[cfg(feature = "linebased")]
561    /// # {
562    /// use debian_watch::parse::ParsedWatchFile;
563    ///
564    /// let mut wf = ParsedWatchFile::new(4).unwrap();
565    /// let mut entry = wf.add_entry("https://github.com/foo/bar/tags", ".*/v?([\\d.]+)\\.tar\\.gz");
566    /// entry.set_matching_pattern(".*/release-([\\d.]+)\\.tar\\.gz");
567    /// assert_eq!(entry.matching_pattern(), Some(".*/release-([\\d.]+)\\.tar\\.gz".to_string()));
568    /// # }
569    /// ```
570    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    /// Get the line number (0-indexed) where this entry starts
580    ///
581    /// For line-based formats (v1-4), this returns the actual line number in the file.
582    /// For deb822 format (v5), this returns the line where the paragraph starts.
583    ///
584    /// # Examples
585    ///
586    /// ```
587    /// # #[cfg(feature = "linebased")]
588    /// # {
589    /// use debian_watch::parse::parse;
590    ///
591    /// let content = "version=4\nhttps://example.com/ .*.tar.gz\nhttps://example2.com/ .*.tar.gz";
592    /// let wf = parse(content).unwrap();
593    /// let entries: Vec<_> = wf.entries().collect();
594    /// assert_eq!(entries[0].line(), 1); // Second line (0-indexed)
595    /// assert_eq!(entries[1].line(), 2); // Third line (0-indexed)
596    /// # }
597    /// ```
598    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    /// Remove/delete an option from the entry
608    ///
609    /// For v5 (deb822) entries, this removes a field from the paragraph.
610    /// For v1-4 (line-based) entries, this removes an option from the opts= list.
611    /// If this is the last option in a line-based entry, the entire opts= declaration is removed.
612    ///
613    /// # Examples
614    ///
615    /// ```
616    /// # #[cfg(feature = "linebased")]
617    /// # {
618    /// use debian_watch::parse::ParsedWatchFile;
619    /// use debian_watch::WatchOption;
620    ///
621    /// let mut wf = ParsedWatchFile::new(4).unwrap();
622    /// let mut entry = wf.add_entry("https://github.com/foo/bar/tags", ".*/v?([\\d.]+)\\.tar\\.gz");
623    /// entry.set_option(WatchOption::Compression(debian_watch::Compression::Xz));
624    /// assert!(entry.has_option("compression"));
625    /// entry.remove_option(WatchOption::Compression(debian_watch::Compression::Xz));
626    /// assert!(!entry.has_option("compression"));
627    /// # }
628    /// ```
629    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    /// Retrieve the mode of the watch file entry.
639    ///
640    /// Returns the mode with default fallback to `Mode::LWP` if not specified.
641    /// Returns an error if the mode value is invalid.
642    ///
643    /// # Examples
644    ///
645    /// ```
646    /// # #[cfg(feature = "linebased")]
647    /// # {
648    /// use debian_watch::parse::ParsedWatchFile;
649    /// use debian_watch::{WatchOption, Mode};
650    ///
651    /// let mut wf = ParsedWatchFile::new(4).unwrap();
652    /// let mut entry = wf.add_entry("https://github.com/foo/bar/tags", ".*/v?([\\d.]+)\\.tar\\.gz");
653    ///
654    /// // Default mode is LWP
655    /// assert_eq!(entry.mode().unwrap(), Mode::LWP);
656    ///
657    /// // Set git mode
658    /// entry.set_option(WatchOption::Mode(Mode::Git));
659    /// assert_eq!(entry.mode().unwrap(), Mode::Git);
660    /// # }
661    /// ```
662    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/// Look up the byte range of a deb822 entry by trying each name in
673/// `names` in order. Returns the first match's range. Used by the
674/// watch-file range helpers to handle aliased fields (`Source` vs
675/// `URL`) without spelling out two lookups at every call site.
676#[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
700/// Parse a watch file with automatic format detection
701///
702/// This function detects whether the input is line-based (v1-4) or
703/// deb822 format (v5) and parses it accordingly, returning a unified
704/// ParsedWatchFile enum.
705///
706/// # Examples
707///
708/// ```
709/// # #[cfg(feature = "linebased")]
710/// # {
711/// use debian_watch::parse::parse;
712///
713/// let content = "version=4\nhttps://example.com/ .*.tar.gz";
714/// let parsed = parse(content).unwrap();
715/// assert_eq!(parsed.version(), 4);
716/// # }
717/// ```
718pub 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        // Test v4
833        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        // Test v5
838        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        // Parse again
851        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        // Test setting options with enum
885        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        // Parse again
923        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); // Second line (0-indexed)
1000        assert_eq!(entries[1].line(), 2); // Third line (0-indexed)
1001    }
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); // Third line (0-indexed)
1018        assert_eq!(entries[1].line(), 5); // Sixth line (0-indexed)
1019    }
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        // The range covers the whole `Source: ...` entry, ending after
1086        // the trailing newline.
1087        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        // The line-based format uses `mode=git`; deb822 v5 spells the
1107        // same option as `Mode: git`. option_range looks up either
1108        // case, so callers using the line-based naming convention
1109        // still work against v5 files.
1110        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/// Thread-safe parse result for watch files, suitable for use in Salsa databases.
1146///
1147/// This wrapper provides a thread-safe interface around the parsed watch file,
1148/// storing either a line-based parse tree or the raw text for deb822 format.
1149/// The underlying lossless parse trees (based on rowan's GreenNode) are thread-safe.
1150#[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    /// Parse a watch file with automatic format detection
1165    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                // Fallback to storing text if linebased feature is not enabled
1180                #[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                // Fallback to linebased if deb822 feature is not enabled
1192                #[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                // Default to linebased v1 if we can't detect
1203                #[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    /// Get the parsed watch file
1223    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    /// Get the version of the watch file
1236    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
1246// Implement Send + Sync since the underlying types are thread-safe
1247// Both variants store GreenNode (thread-safe) via their Parse types
1248unsafe impl Send for Parse {}
1249unsafe impl Sync for Parse {}