1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
use std::str::FromStr;

pub enum ComponentType {
    Perl,
    NodeJS,
}

impl ToString for ComponentType {
    fn to_string(&self) -> String {
        match self {
            ComponentType::Perl => "perl".to_string(),
            ComponentType::NodeJS => "nodejs".to_string(),
        }
    }
}

impl FromStr for ComponentType {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "perl" => Ok(ComponentType::Perl),
            "nodejs" => Ok(ComponentType::NodeJS),
            _ => Err(()),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Compression {
    Gzip,
    Xz,
    Bzip2,
    Lzma,
    #[default]
    Default,
}

impl ToString for Compression {
    fn to_string(&self) -> String {
        match self {
            Compression::Gzip => "gzip".to_string(),
            Compression::Xz => "xz".to_string(),
            Compression::Bzip2 => "bzip2".to_string(),
            Compression::Lzma => "lzma".to_string(),
            Compression::Default => "default".to_string(),
        }
    }
}

impl FromStr for Compression {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "gz" | "gzip" => Ok(Compression::Gzip),
            "xz" => Ok(Compression::Xz),
            "bz2" | "bzip2" => Ok(Compression::Bzip2),
            "lzma" => Ok(Compression::Lzma),
            "default" => Ok(Compression::Default),
            _ => Err(()),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Pretty {
    Describe,
    Pattern(String),
}

impl Default for Pretty {
    fn default() -> Self {
        Pretty::Pattern("0.0~git%cd.h%".to_string())
    }
}

impl ToString for Pretty {
    fn to_string(&self) -> String {
        match self {
            Pretty::Describe => "describe".to_string(),
            Pretty::Pattern(pattern) => pattern.clone(),
        }
    }
}

impl FromStr for Pretty {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s == "describe" {
            Ok(Pretty::Describe)
        } else {
            Ok(Pretty::Pattern(s.to_string()))
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum GitExport {
    #[default]
    Default,
    All,
}

impl ToString for GitExport {
    fn to_string(&self) -> String {
        match self {
            GitExport::Default => "default".to_string(),
            GitExport::All => "all".to_string(),
        }
    }
}

impl FromStr for GitExport {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "default" => Ok(GitExport::Default),
            "all" => Ok(GitExport::All),
            _ => Err(()),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum GitMode {
    #[default]
    Shallow,
    Full,
}

impl ToString for GitMode {
    fn to_string(&self) -> String {
        match self {
            GitMode::Shallow => "shallow".to_string(),
            GitMode::Full => "full".to_string(),
        }
    }
}

impl FromStr for GitMode {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "shallow" => Ok(GitMode::Shallow),
            "full" => Ok(GitMode::Full),
            _ => Err(()),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum PgpMode {
    Auto,
    #[default]
    Default,
    Mangle,
    Next,
    Previous,
    SelfSignature,
    GitTag,
}

impl ToString for PgpMode {
    fn to_string(&self) -> String {
        match self {
            PgpMode::Auto => "auto".to_string(),
            PgpMode::Default => "default".to_string(),
            PgpMode::Mangle => "mangle".to_string(),
            PgpMode::Next => "next".to_string(),
            PgpMode::Previous => "previous".to_string(),
            PgpMode::SelfSignature => "self".to_string(),
            PgpMode::GitTag => "gittag".to_string(),
        }
    }
}

impl FromStr for PgpMode {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "auto" => Ok(PgpMode::Auto),
            "default" => Ok(PgpMode::Default),
            "mangle" => Ok(PgpMode::Mangle),
            "next" => Ok(PgpMode::Next),
            "previous" => Ok(PgpMode::Previous),
            "self" => Ok(PgpMode::SelfSignature),
            "gittag" => Ok(PgpMode::GitTag),
            _ => Err(()),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum SearchMode {
    #[default]
    Html,
    Plain,
}

impl ToString for SearchMode {
    fn to_string(&self) -> String {
        match self {
            SearchMode::Html => "html".to_string(),
            SearchMode::Plain => "plain".to_string(),
        }
    }
}

impl FromStr for SearchMode {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "html" => Ok(SearchMode::Html),
            "plain" => Ok(SearchMode::Plain),
            _ => Err(()),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Mode {
    #[default]
    LWP,
    Git,
    Svn,
}

impl ToString for Mode {
    fn to_string(&self) -> String {
        match self {
            Mode::LWP => "lwp".to_string(),
            Mode::Git => "git".to_string(),
            Mode::Svn => "svn".to_string(),
        }
    }
}

impl FromStr for Mode {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "lwp" => Ok(Mode::LWP),
            "git" => Ok(Mode::Git),
            "svn" => Ok(Mode::Svn),
            _ => Err(()),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum VersionPolicy {
    #[default]
    // Requires the downloading upstream tarball to be newer than the version obtained from debian/changelog
    Debian,

    /// Requires the upstream tarball to be newer than specified version
    Version(debversion::Version),

    /// Requires the downloaded version of the secondary tarballs to be exactly the same as the one for the first upstream tarball downloaded
    Same,

    /// Restricts the version of the seignature file (used with pgpmode=previous)
    Previous,

    /// Does not restrict the version of the secondary tarballs
    Ignore,

    /// Requires the downloading upstream tarball to be newer than the version obtained from
    /// debian/changelog. Package version is the concatenation of all "group" upstream version.
    Group,

    /// Requires the downloading upstream tarball to be newer than the version obtained from
    /// debian/changelog. Package version is the concatenation of the version of the main tarball,
    /// followed by a checksum of all the tarballs using the checksum version system. At least the
    /// main upstream source has to be declared as group.
    Checksum,
}

impl ToString for VersionPolicy {
    fn to_string(&self) -> String {
        match self {
            VersionPolicy::Debian => "debian".to_string(),
            VersionPolicy::Version(v) => format!("version-{}", v.to_string()),
            VersionPolicy::Same => "same".to_string(),
            VersionPolicy::Previous => "previous".to_string(),
            VersionPolicy::Ignore => "ignore".to_string(),
            VersionPolicy::Group => "group".to_string(),
            VersionPolicy::Checksum => "checksum".to_string(),
        }
    }
}

impl std::str::FromStr for VersionPolicy {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "debian" => Ok(VersionPolicy::Debian),
            "same" => Ok(VersionPolicy::Same),
            "previous" => Ok(VersionPolicy::Previous),
            "ignore" => Ok(VersionPolicy::Ignore),
            "group" => Ok(VersionPolicy::Group),
            "checksum" => Ok(VersionPolicy::Checksum),
            s if s.starts_with("version-") => {
                let v = s.trim_start_matches("version-");
                Ok(VersionPolicy::Version(
                    v.parse::<debversion::Version>()
                        .map_err(|e| e.to_string())?,
                ))
            }
            _ => Err(format!("Unknown version policy: {}", s)),
        }
    }
}

#[cfg(test)]
mod version_policy_tests {
    use super::VersionPolicy;
    use std::str::FromStr;

    #[test]
    fn test_version_policy_to_string() {
        assert_eq!("debian", VersionPolicy::Debian.to_string());
        assert_eq!("same", VersionPolicy::Same.to_string());
        assert_eq!("previous", VersionPolicy::Previous.to_string());
        assert_eq!("ignore", VersionPolicy::Ignore.to_string());
        assert_eq!("group", VersionPolicy::Group.to_string());
        assert_eq!("checksum", VersionPolicy::Checksum.to_string());
        assert_eq!(
            "version-1.2.3",
            VersionPolicy::Version("1.2.3".parse().unwrap()).to_string()
        );
    }

    #[test]
    fn test_version_policy_from_str() {
        assert_eq!(
            VersionPolicy::Debian,
            VersionPolicy::from_str("debian").unwrap()
        );
        assert_eq!(
            VersionPolicy::Same,
            VersionPolicy::from_str("same").unwrap()
        );
        assert_eq!(
            VersionPolicy::Previous,
            VersionPolicy::from_str("previous").unwrap()
        );
        assert_eq!(
            VersionPolicy::Ignore,
            VersionPolicy::from_str("ignore").unwrap()
        );
        assert_eq!(
            VersionPolicy::Group,
            VersionPolicy::from_str("group").unwrap()
        );
        assert_eq!(
            VersionPolicy::Checksum,
            VersionPolicy::from_str("checksum").unwrap()
        );
        assert_eq!(
            VersionPolicy::Version("1.2.3".parse().unwrap()),
            VersionPolicy::from_str("version-1.2.3").unwrap()
        );
    }
}