alpm-types 0.11.2

Types for Arch Linux Package Management
Documentation
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! The [alpm-package-version] form _full_ and _full with epoch_.
//!
//! [alpm-package-version]: https://alpm.archlinux.page/specifications/alpm-package-version.7.html

use std::{
    cmp::Ordering,
    fmt::{Display, Formatter},
    str::FromStr,
};

use serde::{Deserialize, Serialize};
use winnow::{
    ModalResult,
    Parser,
    combinator::{cut_err, eof, opt, preceded, terminated},
    error::{StrContext, StrContextValue},
    token::{take_till, take_until},
};

use crate::{Epoch, Error, PackageRelease, PackageVersion, Version};

/// A package version with mandatory [`PackageRelease`].
///
/// Tracks an optional [`Epoch`], a [`PackageVersion`] and a [`PackageRelease`].
/// This reflects the _full_ and _full with epoch_ forms of [alpm-package-version].
///
/// # Note
///
/// If [`PackageRelease`] should be optional for your use-case, use [`Version`] instead.
///
/// # Examples
///
/// ```
/// use std::str::FromStr;
///
/// use alpm_types::FullVersion;
///
/// # fn main() -> testresult::TestResult {
/// // A full version.
/// let version = FullVersion::from_str("1.0.0-1")?;
///
/// // A full version with epoch.
/// let version = FullVersion::from_str("1:1.0.0-1")?;
/// # Ok(())
/// # }
/// ```
///
/// [alpm-package-version]: https://alpm.archlinux.page/specifications/alpm-package-version.7.html
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct FullVersion {
    /// The version of the package
    pub pkgver: PackageVersion,
    /// The release of the package
    pub pkgrel: PackageRelease,
    /// The epoch of the package
    pub epoch: Option<Epoch>,
}

impl FullVersion {
    /// Creates a new [`FullVersion`].
    ///
    /// # Examples
    ///
    /// ```
    /// use alpm_types::{Epoch, FullVersion, PackageRelease, PackageVersion};
    ///
    /// # fn main() -> testresult::TestResult {
    /// // A full version.
    /// let version = FullVersion::new(
    ///     PackageVersion::new("1.0.0".to_string())?,
    ///     PackageRelease::new(1, None),
    ///     None,
    /// );
    ///
    /// // A full version with epoch.
    /// let version = FullVersion::new(
    ///     PackageVersion::new("1.0.0".to_string())?,
    ///     PackageRelease::new(1, None),
    ///     Some(Epoch::new(1.try_into()?)),
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn new(pkgver: PackageVersion, pkgrel: PackageRelease, epoch: Option<Epoch>) -> Self {
        Self {
            pkgver,
            pkgrel,
            epoch,
        }
    }

    /// Compares `self` to another [`FullVersion`] and returns a number.
    ///
    /// - `1` if `self` is newer than `other`
    /// - `0` if `self` and `other` are equal
    /// - `-1` if `self` is older than `other`
    ///
    /// This output behavior is based on the behavior of the [vercmp] tool.
    ///
    /// Delegates to [`FullVersion::cmp`] for comparison.
    /// The rules and algorithms used for comparison are explained in more detail in
    /// [alpm-package-version] and [alpm-pkgver].
    ///
    /// # Examples
    ///
    /// ```
    /// use std::str::FromStr;
    ///
    /// use alpm_types::FullVersion;
    ///
    /// # fn main() -> Result<(), alpm_types::Error> {
    /// assert_eq!(
    ///     FullVersion::from_str("1.0.0-1")?.vercmp(&FullVersion::from_str("0.1.0-1")?),
    ///     1
    /// );
    /// assert_eq!(
    ///     FullVersion::from_str("1.0.0-1")?.vercmp(&FullVersion::from_str("1.0.0-1")?),
    ///     0
    /// );
    /// assert_eq!(
    ///     FullVersion::from_str("0.1.0-1")?.vercmp(&FullVersion::from_str("1.0.0-1")?),
    ///     -1
    /// );
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// [alpm-package-version]: https://alpm.archlinux.page/specifications/alpm-package-version.7.html
    /// [alpm-pkgver]: https://alpm.archlinux.page/specifications/alpm-pkgver.7.html
    /// [vercmp]: https://man.archlinux.org/man/vercmp.8
    pub fn vercmp(&self, other: &FullVersion) -> i8 {
        match self.cmp(other) {
            Ordering::Less => -1,
            Ordering::Equal => 0,
            Ordering::Greater => 1,
        }
    }

    /// Recognizes a [`FullVersion`] in a string slice.
    ///
    /// Consumes all of its input.
    ///
    /// # Errors
    ///
    /// Returns an error if `input` is not a valid [alpm-package-version] (_full_ or _full with
    /// epoch_).
    ///
    /// [alpm-package-version]: https://alpm.archlinux.page/specifications/alpm-package-version.7.html
    pub fn parser(input: &mut &str) -> ModalResult<Self> {
        // Advance the parser until after a ':' if there is one, e.g.:
        // "1:1.0.0-1" -> "1.0.0-1"
        let epoch = opt(terminated(take_till(1.., ':'), ':').and_then(
            // cut_err now that we've found a pattern with ':'
            cut_err(Epoch::parser),
        ))
        .context(StrContext::Expected(StrContextValue::Description(
            "followed by a ':'",
        )))
        .parse_next(input)?;

        // Advance the parser until the next '-', e.g.:
        // "1.0.0-1" -> "-1"
        let pkgver: PackageVersion = cut_err(take_until(0.., "-"))
            .context(StrContext::Expected(StrContextValue::Description(
                "alpm-pkgver string, followed by a '-' and an alpm-pkgrel string",
            )))
            .take()
            .and_then(cut_err(PackageVersion::parser))
            .parse_next(input)?;

        // Consume the delimiter '-'
        // "-1" -> "1"
        // and parse everything until eof as a PackageRelease, e.g.:
        // "1" -> ""
        let pkgrel: PackageRelease = preceded("-", cut_err(PackageRelease::parser))
            .context(StrContext::Expected(StrContextValue::Description(
                "alpm-pkgrel string",
            )))
            .parse_next(input)?;

        // Ensure that there are no trailing chars left.
        eof.context(StrContext::Expected(StrContextValue::Description(
            "end of full alpm-package-version string",
        )))
        .parse_next(input)?;

        Ok(Self {
            epoch,
            pkgver,
            pkgrel,
        })
    }
}

impl Display for FullVersion {
    fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
        if let Some(epoch) = self.epoch {
            write!(fmt, "{epoch}:")?;
        }
        write!(fmt, "{}-{}", self.pkgver, self.pkgrel)?;

        Ok(())
    }
}

impl FromStr for FullVersion {
    type Err = Error;
    /// Creates a new [`FullVersion`] from a string slice.
    ///
    /// Delegates to [`FullVersion::parser`].
    ///
    /// # Errors
    ///
    /// Returns an error if [`Version::parser`] fails.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self::parser.parse(s)?)
    }
}

impl Ord for FullVersion {
    /// Compares `self` to another [`FullVersion`].
    ///
    /// The comparison rules and algorithms are explained in more detail in [alpm-package-version]
    /// and [alpm-pkgver].
    ///
    /// # Examples
    ///
    /// ```
    /// use std::{cmp::Ordering, str::FromStr};
    ///
    /// use alpm_types::FullVersion;
    ///
    /// # fn main() -> testresult::TestResult {
    /// // Examples for "full"
    /// let version_a = FullVersion::from_str("1.0.0-1")?;
    /// let version_b = FullVersion::from_str("1.0.0-2")?;
    /// assert_eq!(version_a.cmp(&version_b), Ordering::Less);
    /// assert_eq!(version_b.cmp(&version_a), Ordering::Greater);
    ///
    /// let version_a = FullVersion::from_str("1.0.0-1")?;
    /// let version_b = FullVersion::from_str("1.0.0-1")?;
    /// assert_eq!(version_a.cmp(&version_b), Ordering::Equal);
    ///
    /// // Examples for "full with epoch"
    /// let version_a = FullVersion::from_str("1:1.0.0-1")?;
    /// let version_b = FullVersion::from_str("1.0.0-2")?;
    /// assert_eq!(version_a.cmp(&version_b), Ordering::Greater);
    /// assert_eq!(version_b.cmp(&version_a), Ordering::Less);
    ///
    /// let version_a = FullVersion::from_str("1:1.0.0-1")?;
    /// let version_b = FullVersion::from_str("1:1.0.0-1")?;
    /// assert_eq!(version_a.cmp(&version_b), Ordering::Equal);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// [alpm-package-version]: https://alpm.archlinux.page/specifications/alpm-package-version.7.html
    /// [alpm-pkgver]: https://alpm.archlinux.page/specifications/alpm-pkgver.7.html
    fn cmp(&self, other: &Self) -> Ordering {
        match (self.epoch, other.epoch) {
            (Some(self_epoch), Some(other_epoch)) if self_epoch.cmp(&other_epoch).is_ne() => {
                return self_epoch.cmp(&other_epoch);
            }
            (Some(_), None) => return Ordering::Greater,
            (None, Some(_)) => return Ordering::Less,
            (_, _) => {}
        }

        let pkgver_cmp = self.pkgver.cmp(&other.pkgver);
        if pkgver_cmp.is_ne() {
            return pkgver_cmp;
        }

        self.pkgrel.cmp(&other.pkgrel)
    }
}

impl PartialOrd for FullVersion {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl TryFrom<Version> for FullVersion {
    type Error = crate::Error;

    /// Creates a [`FullVersion`] from a [`Version`].
    ///
    /// # Errors
    ///
    /// Returns an error if `value.pkgrel` is [`None`].
    fn try_from(value: Version) -> Result<Self, Self::Error> {
        Ok(Self {
            pkgver: value.pkgver,
            pkgrel: value.pkgrel.ok_or(Error::MissingComponent {
                component: "pkgrel",
            })?,
            epoch: value.epoch,
        })
    }
}

impl TryFrom<&Version> for FullVersion {
    type Error = crate::Error;

    /// Creates a [`FullVersion`] from a [`Version`] reference.
    ///
    /// # Errors
    ///
    /// Returns an error if `value.pkgrel` is [`None`].
    fn try_from(value: &Version) -> Result<Self, Self::Error> {
        Self::try_from(value.clone())
    }
}

impl From<FullVersion> for Version {
    /// Creates a [`Version`] from a [`FullVersion`].
    fn from(value: FullVersion) -> Self {
        Self {
            pkgver: value.pkgver,
            pkgrel: Some(value.pkgrel),
            epoch: value.epoch,
        }
    }
}

impl From<&FullVersion> for Version {
    /// Creates a [`Version`] from a [`FullVersion`] reference.
    fn from(value: &FullVersion) -> Self {
        Self::from(value.clone())
    }
}

#[cfg(test)]
mod tests {
    use log::{LevelFilter, debug};
    use rstest::rstest;
    use simplelog::{ColorChoice, Config, TermLogger, TerminalMode};
    use testresult::TestResult;

    use super::*;

    /// Initialize a logger that shows trace messages on stderr.
    fn init_logger() {
        if TermLogger::init(
            LevelFilter::Trace,
            Config::default(),
            TerminalMode::Stderr,
            ColorChoice::Auto,
        )
        .is_err()
        {
            debug!("Not initializing another logger, as one is initialized already.");
        }
    }

    /// Ensures that valid [`FullVersion`] strings are parsed successfully as expected.
    #[rstest]
    #[case::full_with_epoch(
        "1:foo-1",
        FullVersion {
            pkgver: PackageVersion::from_str("foo")?,
            epoch: Some(Epoch::from_str("1")?),
            pkgrel: PackageRelease::from_str("1")?,
        },
    )]
    #[case::full(
        "foo-1",
        FullVersion {
            pkgver: PackageVersion::from_str("foo")?,
            epoch: None,
            pkgrel: PackageRelease::from_str("1")?
        }
    )]
    fn valid_full_version_from_string(
        #[case] version: &str,
        #[case] expected: FullVersion,
    ) -> TestResult {
        init_logger();

        assert_eq!(
            FullVersion::from_str(version),
            Ok(expected),
            "Expected valid parsing for FullVersion {version}"
        );

        Ok(())
    }

    /// Ensures that invalid [`FullVersion`] strings lead to parse errors.
    #[rstest]
    #[case::two_pkgrel("1:foo-1-1", "expected end of package release value")]
    #[case::two_epoch("1:1:foo-1", "invalid pkgver character")]
    #[case::empty_string(
        "",
        "expected alpm-pkgver string, followed by a '-' and an alpm-pkgrel string"
    )]
    #[case::colon(
        ":",
        "expected alpm-pkgver string, followed by a '-' and an alpm-pkgrel string"
    )]
    #[case::dot(
        ".",
        "expected alpm-pkgver string, followed by a '-' and an alpm-pkgrel string"
    )]
    #[case::no_pkgrel_with_epoch(
        "1:1.0.0",
        "expected alpm-pkgver string, followed by a '-' and an alpm-pkgrel string"
    )]
    #[case::no_pkgrel(
        "1.0.0",
        "expected alpm-pkgver string, followed by a '-' and an alpm-pkgrel string"
    )]
    #[case::no_pkgrel_dash_end(
        "1.0.0-",
        "invalid package release\nexpected positive decimal integer, alpm-pkgrel string"
    )]
    #[case::starts_with_dash(
        "-1foo:1",
        "invalid package epoch\nexpected positive non-zero decimal integer, followed by a ':'"
    )]
    #[case::ends_with_colon(
        "1-foo:",
        "invalid package epoch\nexpected positive non-zero decimal integer, followed by a ':'"
    )]
    #[case::ends_with_colon_number(
        "1-foo:1",
        "invalid package epoch\nexpected positive non-zero decimal integer, followed by a ':'"
    )]
    fn parse_error_in_full_version_from_string(#[case] version: &str, #[case] err_snippet: &str) {
        init_logger();

        let Err(Error::ParseError(err_msg)) = FullVersion::from_str(version) else {
            panic!("parsing '{version}' as FullVersion did not fail as expected")
        };
        assert!(
            err_msg.contains(err_snippet),
            "Error:\n=====\n{err_msg}\n=====\nshould contain snippet:\n\n{err_snippet}"
        );
    }

    /// Ensures that [`FullVersion`] can be created from valid/compatible [`Version`] (and
    /// [`Version`] reference) and fails otherwise.
    #[rstest]
    #[case::full_with_epoch(Version::from_str("1:1.0.0-1")?, Ok(FullVersion::from_str("1:1.0.0-1")?))]
    #[case::full(Version::from_str("1.0.0-1")?, Ok(FullVersion::from_str("1.0.0-1")?))]
    #[case::minimal_with_epoch(Version::from_str("1:1.0.0")?, Err(Error::MissingComponent{component: "pkgrel"}))]
    #[case::minimal(Version::from_str("1.0.0")?, Err(Error::MissingComponent{component: "pkgrel"}))]
    fn full_version_try_from_version(
        #[case] version: Version,
        #[case] expected: Result<FullVersion, Error>,
    ) -> TestResult {
        assert_eq!(FullVersion::try_from(&version), expected);
        assert_eq!(FullVersion::try_from(version), expected);
        Ok(())
    }

    /// Ensures that [`Version`] can be created from [`FullVersion`] (and [`FullVersion`]
    /// reference).
    #[rstest]
    #[case::full_with_epoch(Version::from_str("1:1.0.0-1")?, FullVersion::from_str("1:1.0.0-1")?)]
    #[case::full(Version::from_str("1.0.0-1")?, FullVersion::from_str("1.0.0-1")?)]
    fn version_from_full_version(
        #[case] version: Version,
        #[case] full_version: FullVersion,
    ) -> TestResult {
        assert_eq!(Version::from(&full_version), version);
        Ok(())
    }

    /// Ensures that [`FullVersion`] is properly serialized back to its string representation.
    #[rstest]
    #[case::with_epoch("1:1-1")]
    #[case::plain("1-1")]
    fn full_version_to_string(#[case] input: &str) -> TestResult {
        assert_eq!(format!("{}", FullVersion::from_str(input)?), input);
        Ok(())
    }

    /// Ensures that [`FullVersion`]s can be compared.
    ///
    /// For more detailed version comparison tests refer to the unit tests for [`Version`] and
    /// [`PackageRelease`].
    #[rstest]
    #[case::full_equal("1.0.0-1", "1.0.0-1", Ordering::Equal)]
    #[case::full_less("1.0.0-1", "1.0.0-2", Ordering::Less)]
    #[case::full_greater("1.0.0-2", "1.0.0-1", Ordering::Greater)]
    #[case::full_with_epoch_equal("1:1.0.0-1", "1:1.0.0-1", Ordering::Equal)]
    #[case::full_with_epoch_less("1.0.0-1", "1:1.0.0-1", Ordering::Less)]
    #[case::full_with_epoch_less("1:1.0.0-1", "2:1.0.0-1", Ordering::Less)]
    #[case::full_with_epoch_greater("1:1.0.0-1", "1.0.0-1", Ordering::Greater)]
    #[case::full_with_epoch_greater("2:1.0.0-1", "1:1.0.0-1", Ordering::Greater)]
    fn full_version_comparison(
        #[case] version_a: &str,
        #[case] version_b: &str,
        #[case] expected: Ordering,
    ) -> TestResult {
        let version_a = FullVersion::from_str(version_a)?;
        let version_b = FullVersion::from_str(version_b)?;

        // Derive the expected vercmp binary exitcode from the expected Ordering.
        let vercmp_result = match &expected {
            Ordering::Equal => 0,
            Ordering::Greater => 1,
            Ordering::Less => -1,
        };

        let ordering = version_a.cmp(&version_b);
        assert_eq!(
            ordering, expected,
            "Failed to compare '{version_a}' and '{version_b}'. Expected {expected:?} got {ordering:?}"
        );

        assert_eq!(version_a.vercmp(&version_b), vercmp_result);

        // If we find the `vercmp` binary, also run the test against the actual binary.
        #[cfg(feature = "compatibility_tests")]
        {
            let output = std::process::Command::new("vercmp")
                .arg(version_a.to_string())
                .arg(version_b.to_string())
                .output()?;
            let result = String::from_utf8_lossy(&output.stdout);
            assert_eq!(result.trim(), vercmp_result.to_string());
        }

        // Now check that the opposite holds true as well.
        let reverse_vercmp_result = match &expected {
            Ordering::Equal => 0,
            Ordering::Greater => -1,
            Ordering::Less => 1,
        };
        let reverse_expected = match &expected {
            Ordering::Equal => Ordering::Equal,
            Ordering::Greater => Ordering::Less,
            Ordering::Less => Ordering::Greater,
        };

        let reverse_ordering = version_b.cmp(&version_a);
        assert_eq!(
            reverse_ordering, reverse_expected,
            "Failed to compare '{version_a}' and '{version_b}'. Expected {expected:?} got {ordering:?}"
        );

        assert_eq!(version_b.vercmp(&version_a), reverse_vercmp_result);

        Ok(())
    }
}