debian_watch/
types.rs

1use std::str::FromStr;
2
3/// The type of the component
4pub enum ComponentType {
5    /// Perl component
6    Perl,
7
8    /// NodeJS component
9    NodeJS,
10}
11
12impl std::fmt::Display for ComponentType {
13    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
14        write!(
15            f,
16            "{}",
17            match self {
18                ComponentType::Perl => "perl",
19                ComponentType::NodeJS => "nodejs",
20            }
21        )
22    }
23}
24
25impl FromStr for ComponentType {
26    type Err = ();
27
28    fn from_str(s: &str) -> Result<Self, Self::Err> {
29        match s {
30            "perl" => Ok(ComponentType::Perl),
31            "nodejs" => Ok(ComponentType::NodeJS),
32            _ => Err(()),
33        }
34    }
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
38/// Compression type
39pub enum Compression {
40    /// Gzip compression
41    Gzip,
42
43    /// Xz compression
44    Xz,
45
46    /// Bzip2 compression
47    Bzip2,
48
49    /// Lzma compression
50    Lzma,
51
52    #[default]
53    /// Default compression
54    Default,
55}
56
57impl std::fmt::Display for Compression {
58    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
59        write!(
60            f,
61            "{}",
62            match self {
63                Compression::Gzip => "gzip",
64                Compression::Xz => "xz",
65                Compression::Bzip2 => "bzip2",
66                Compression::Lzma => "lzma",
67                Compression::Default => "default",
68            }
69        )
70    }
71}
72
73impl FromStr for Compression {
74    type Err = ();
75
76    fn from_str(s: &str) -> Result<Self, Self::Err> {
77        match s {
78            "gz" | "gzip" => Ok(Compression::Gzip),
79            "xz" => Ok(Compression::Xz),
80            "bz2" | "bzip2" => Ok(Compression::Bzip2),
81            "lzma" => Ok(Compression::Lzma),
82            "default" => Ok(Compression::Default),
83            _ => Err(()),
84        }
85    }
86}
87
88#[derive(Debug, Clone, PartialEq, Eq, Hash)]
89/// How to generate upstream version string from git tags
90pub enum Pretty {
91    /// Use git describe to generate the version string
92    Describe,
93
94    /// Use a custom pattern to generate the version string
95    Pattern(String),
96}
97
98impl Default for Pretty {
99    fn default() -> Self {
100        Pretty::Pattern("0.0~git%cd.h%".to_string())
101    }
102}
103
104impl std::fmt::Display for Pretty {
105    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
106        write!(
107            f,
108            "{}",
109            match self {
110                Pretty::Describe => "describe",
111                Pretty::Pattern(pattern) => pattern,
112            }
113        )
114    }
115}
116
117impl FromStr for Pretty {
118    type Err = ();
119
120    fn from_str(s: &str) -> Result<Self, Self::Err> {
121        if s == "describe" {
122            Ok(Pretty::Describe)
123        } else {
124            Ok(Pretty::Pattern(s.to_string()))
125        }
126    }
127}
128
129#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
130/// Git export mode
131pub enum GitExport {
132    #[default]
133    /// Export only files in the .orig.tar archive that are not ignored by the upstream.
134    Default,
135
136    /// Export all files in the .orig.tar archive, ignoring any export-ignore git attributes
137    /// defined by the upstream.
138    All,
139}
140
141impl std::fmt::Display for GitExport {
142    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
143        write!(
144            f,
145            "{}",
146            match self {
147                GitExport::Default => "default".to_string(),
148                GitExport::All => "all".to_string(),
149            }
150        )
151    }
152}
153
154impl FromStr for GitExport {
155    type Err = ();
156
157    fn from_str(s: &str) -> Result<Self, Self::Err> {
158        match s {
159            "default" => Ok(GitExport::Default),
160            "all" => Ok(GitExport::All),
161            _ => Err(()),
162        }
163    }
164}
165
166#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
167/// Git clone operation mode
168pub enum GitMode {
169    #[default]
170    /// Clone the git repository in shallow mode
171    Shallow,
172
173    /// Clone the git repository in full mode
174    Full,
175}
176
177impl std::fmt::Display for GitMode {
178    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
179        write!(
180            f,
181            "{}",
182            match self {
183                GitMode::Shallow => "shallow".to_string(),
184                GitMode::Full => "full".to_string(),
185            }
186        )
187    }
188}
189
190impl FromStr for GitMode {
191    type Err = ();
192
193    fn from_str(s: &str) -> Result<Self, Self::Err> {
194        match s {
195            "shallow" => Ok(GitMode::Shallow),
196            "full" => Ok(GitMode::Full),
197            _ => Err(()),
198        }
199    }
200}
201
202#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
203/// PGP verification mode
204pub enum PgpMode {
205    /// check possible URLs for the signature file and autogenerate a ``pgpsigurlmangle`` rule to
206    /// use it
207    Auto,
208
209    #[default]
210    /// Use pgpsigurlmangle=rules to generate the candidate upstream signature file URL string from
211    /// the upstream tarball URL.
212    ///
213    /// If the specified pgpsigurlmangle is missing, uscan checks possible URLs for the signature
214    /// file and suggests adding a pgpsigurlmangle rule.
215    ///
216    Default,
217
218    /// Use pgpsigurlmangle=rules to generate the candidate upstream signature file URL string from the upstream tarball URL.
219    Mangle,
220
221    /// Verify this downloaded tarball file with the signature file specified in the next watch
222    /// line. The next watch line must be pgpmode=previous. Otherwise, no verification occurs.
223    Next,
224
225    /// Verify the downloaded tarball file specified in the previous watch line with this signature
226    /// file.  The previous watch line must be pgpmode=next.
227    Previous,
228
229    /// Verify the downloaded file foo.ext with its self signature and extract its content tarball
230    /// file as foo.
231    SelfSignature,
232
233    /// Verify tag signature if mode=git.
234    GitTag,
235}
236
237impl std::fmt::Display for PgpMode {
238    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
239        write!(
240            f,
241            "{}",
242            match self {
243                PgpMode::Auto => "auto",
244                PgpMode::Default => "default",
245                PgpMode::Mangle => "mangle",
246                PgpMode::Next => "next",
247                PgpMode::Previous => "previous",
248                PgpMode::SelfSignature => "self",
249                PgpMode::GitTag => "gittag",
250            }
251        )
252    }
253}
254impl FromStr for PgpMode {
255    type Err = ();
256
257    fn from_str(s: &str) -> Result<Self, Self::Err> {
258        match s {
259            "auto" => Ok(PgpMode::Auto),
260            "default" => Ok(PgpMode::Default),
261            "mangle" => Ok(PgpMode::Mangle),
262            "next" => Ok(PgpMode::Next),
263            "previous" => Ok(PgpMode::Previous),
264            "self" => Ok(PgpMode::SelfSignature),
265            "gittag" => Ok(PgpMode::GitTag),
266            _ => Err(()),
267        }
268    }
269}
270
271#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
272/// How to search for the upstream tarball
273pub enum SearchMode {
274    #[default]
275    /// Search for the upstream tarball in the HTML page
276    Html,
277
278    /// Search for the upstream tarball in the plain text page
279    Plain,
280}
281
282impl std::fmt::Display for SearchMode {
283    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
284        write!(
285            f,
286            "{}",
287            match self {
288                SearchMode::Html => "html",
289                SearchMode::Plain => "plain",
290            }
291        )
292    }
293}
294
295impl FromStr for SearchMode {
296    type Err = ();
297
298    fn from_str(s: &str) -> Result<Self, Self::Err> {
299        match s {
300            "html" => Ok(SearchMode::Html),
301            "plain" => Ok(SearchMode::Plain),
302            _ => Err(()),
303        }
304    }
305}
306
307#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
308/// Archive download mode
309pub enum Mode {
310    #[default]
311    /// downloads the specified tarball from the archive URL on the web. Automatically internal
312    /// mode value is updated to either http or ftp by URL.
313    LWP,
314
315    /// Access  the  upstream git archive directly with the git command and packs the source tree
316    /// with the specified tag via matching-pattern into spkg-version.tar.xz.
317    Git,
318
319    /// Access the upstream Subversion archive directly with the svn command and packs the source
320    /// tree.
321    Svn,
322}
323
324impl std::fmt::Display for Mode {
325    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
326        write!(
327            f,
328            "{}",
329            match self {
330                Mode::LWP => "lwp",
331                Mode::Git => "git",
332                Mode::Svn => "svn",
333            }
334        )
335    }
336}
337
338impl FromStr for Mode {
339    type Err = ();
340
341    fn from_str(s: &str) -> Result<Self, Self::Err> {
342        match s {
343            "lwp" => Ok(Mode::LWP),
344            "git" => Ok(Mode::Git),
345            "svn" => Ok(Mode::Svn),
346            _ => Err(()),
347        }
348    }
349}
350
351#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
352/// The version policy to use when downloading upstream tarballs
353pub enum VersionPolicy {
354    #[default]
355    /// Requires the downloading upstream tarball to be newer than the version obtained from debian/changelog
356    Debian,
357
358    /// Requires the upstream tarball to be newer than specified version
359    Version(debversion::Version),
360
361    /// Requires the downloaded version of the secondary tarballs to be exactly the same as the one for the first upstream tarball downloaded
362    Same,
363
364    /// Restricts the version of the seignature file (used with pgpmode=previous)
365    Previous,
366
367    /// Does not restrict the version of the secondary tarballs
368    Ignore,
369
370    /// Requires the downloading upstream tarball to be newer than the version obtained from
371    /// debian/changelog. Package version is the concatenation of all "group" upstream version.
372    Group,
373
374    /// Requires the downloading upstream tarball to be newer than the version obtained from
375    /// debian/changelog. Package version is the concatenation of the version of the main tarball,
376    /// followed by a checksum of all the tarballs using the checksum version system. At least the
377    /// main upstream source has to be declared as group.
378    Checksum,
379}
380
381impl std::fmt::Display for VersionPolicy {
382    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
383        match self {
384            VersionPolicy::Debian => write!(f, "debian"),
385            VersionPolicy::Version(v) => write!(f, "version-{}", v),
386            VersionPolicy::Same => write!(f, "same"),
387            VersionPolicy::Previous => write!(f, "previous"),
388            VersionPolicy::Ignore => write!(f, "ignore"),
389            VersionPolicy::Group => write!(f, "group"),
390            VersionPolicy::Checksum => write!(f, "checksum"),
391        }
392    }
393}
394
395impl std::str::FromStr for VersionPolicy {
396    type Err = String;
397
398    fn from_str(s: &str) -> Result<Self, Self::Err> {
399        match s {
400            "debian" => Ok(VersionPolicy::Debian),
401            "same" => Ok(VersionPolicy::Same),
402            "previous" => Ok(VersionPolicy::Previous),
403            "ignore" => Ok(VersionPolicy::Ignore),
404            "group" => Ok(VersionPolicy::Group),
405            "checksum" => Ok(VersionPolicy::Checksum),
406            s if s.starts_with("version-") => {
407                let v = s.trim_start_matches("version-");
408                Ok(VersionPolicy::Version(
409                    v.parse::<debversion::Version>()
410                        .map_err(|e| e.to_string())?,
411                ))
412            }
413            _ => Err(format!("Unknown version policy: {}", s)),
414        }
415    }
416}
417
418#[cfg(test)]
419mod version_policy_tests {
420    use super::VersionPolicy;
421    use std::str::FromStr;
422
423    #[test]
424    fn test_version_policy_to_string() {
425        assert_eq!("debian", VersionPolicy::Debian.to_string());
426        assert_eq!("same", VersionPolicy::Same.to_string());
427        assert_eq!("previous", VersionPolicy::Previous.to_string());
428        assert_eq!("ignore", VersionPolicy::Ignore.to_string());
429        assert_eq!("group", VersionPolicy::Group.to_string());
430        assert_eq!("checksum", VersionPolicy::Checksum.to_string());
431        assert_eq!(
432            "version-1.2.3",
433            VersionPolicy::Version("1.2.3".parse().unwrap()).to_string()
434        );
435    }
436
437    #[test]
438    fn test_version_policy_from_str() {
439        assert_eq!(
440            VersionPolicy::Debian,
441            VersionPolicy::from_str("debian").unwrap()
442        );
443        assert_eq!(
444            VersionPolicy::Same,
445            VersionPolicy::from_str("same").unwrap()
446        );
447        assert_eq!(
448            VersionPolicy::Previous,
449            VersionPolicy::from_str("previous").unwrap()
450        );
451        assert_eq!(
452            VersionPolicy::Ignore,
453            VersionPolicy::from_str("ignore").unwrap()
454        );
455        assert_eq!(
456            VersionPolicy::Group,
457            VersionPolicy::from_str("group").unwrap()
458        );
459        assert_eq!(
460            VersionPolicy::Checksum,
461            VersionPolicy::from_str("checksum").unwrap()
462        );
463        assert_eq!(
464            VersionPolicy::Version("1.2.3".parse().unwrap()),
465            VersionPolicy::from_str("version-1.2.3").unwrap()
466        );
467    }
468}