arch_pkg_text/value/
dependency_specification_operator.rs

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
use strum::{AsRefStr, Display, EnumString, IntoStaticStr};

/// Operator at the start of a [`DependencySpecification`](super::DependencySpecification).
#[derive(Debug, Clone, Copy, PartialEq, Eq)] // core traits
#[derive(AsRefStr, Display, EnumString, IntoStaticStr)] // strum traits
pub enum DependencySpecificationOperator {
    #[strum(serialize = "<")]
    Less = -2,
    #[strum(serialize = "<=")]
    LessOrEqual = -1,
    #[strum(serialize = "=")]
    Equal = 0,
    #[strum(serialize = ">=")]
    GreaterOrEqual = 1,
    #[strum(serialize = ">")]
    Greater = 2,
}

impl DependencySpecificationOperator {
    /// Parse a dependency spec operator from an input string.
    ///
    /// ```
    /// # use arch_pkg_text::value::DependencySpecificationOperator;
    /// # use pretty_assertions::assert_eq;
    /// assert_eq!(
    ///     DependencySpecificationOperator::parse("<1.27.0-1"),
    ///     Some((DependencySpecificationOperator::Less, "1.27.0-1")),
    /// );
    /// assert_eq!(
    ///     DependencySpecificationOperator::parse("<=1.27.0-1"),
    ///     Some((DependencySpecificationOperator::LessOrEqual, "1.27.0-1")),
    /// );
    /// assert_eq!(
    ///     DependencySpecificationOperator::parse("=1.27.0-1"),
    ///     Some((DependencySpecificationOperator::Equal, "1.27.0-1")),
    /// );
    /// assert_eq!(
    ///     DependencySpecificationOperator::parse(">=1.27.0-1"),
    ///     Some((DependencySpecificationOperator::GreaterOrEqual, "1.27.0-1")),
    /// );
    /// assert_eq!(
    ///     DependencySpecificationOperator::parse(">1.27.0-1"),
    ///     Some((DependencySpecificationOperator::Greater, "1.27.0-1")),
    /// );
    /// assert_eq!(DependencySpecificationOperator::parse("1.27.0-1"), None);
    /// ```
    pub fn parse(input: &str) -> Option<(Self, &'_ str)> {
        use DependencySpecificationOperator::*;
        [LessOrEqual, GreaterOrEqual, Less, Equal, Greater] // XOrEqual must place before X
            .into_iter()
            .find_map(|candidate| {
                input
                    .strip_prefix(candidate.as_ref())
                    .map(|rest| (candidate, rest))
            })
    }
}