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
use super::{error::DependencyParseError, version::VersionReq};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum DependencyMode {
    Required,
    Optional { hidden: bool },
    Independent,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Compatibility {
    Compatible(DependencyMode, VersionReq),
    Incompatible,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Dependency {
    pub name: String,
    pub compatibility: Compatibility,
}

impl Dependency {
    pub fn new<T: Into<String>>(name: T, compatibility: Compatibility) -> Self {
        Self {
            name: name.into(),
            compatibility,
        }
    }

    pub fn required<T: Into<String>>(name: T, version_req: VersionReq) -> Self {
        Self::new(
            name,
            Compatibility::Compatible(DependencyMode::Required, version_req),
        )
    }

    pub fn optional<T: Into<String>>(name: T, version_req: VersionReq, hidden: bool) -> Self {
        Self::new(
            name,
            Compatibility::Compatible(DependencyMode::Optional { hidden }, version_req),
        )
    }

    pub fn independent<T: Into<String>>(name: T, version_req: VersionReq) -> Self {
        Self::new(
            name,
            Compatibility::Compatible(DependencyMode::Independent, version_req),
        )
    }

    pub fn incompatible<T: Into<String>>(name: T) -> Self {
        Self::new(name, Compatibility::Incompatible)
    }

    pub fn parse(s: &str) -> Result<Self, DependencyParseError> {
        s.parse()
    }
}