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
use crate::{extra, recipe::Derivation, store::object::Blob};
use console::style;
use semver::VersionReq;
use serde::{Deserialize, Serialize};
use std::{collections::BTreeSet, fmt, fmt::Debug, hash::Hash};
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Hash)]
pub struct Requirement {
    name: String,
    version_req: VersionReq,
    blobs: BTreeSet<Blob>,
}
impl Requirement {
    
    pub fn new(name: String, version_req: VersionReq, objects: BTreeSet<Blob>) -> Self {
        Self {
            name,
            version_req,
            blobs: objects,
        }
    }
    
    pub fn name(&self) -> &String {
        &self.name
    }
    
    pub fn version_req(&self) -> &VersionReq {
        &self.version_req
    }
    
    pub fn blobs(&self) -> &BTreeSet<Blob> {
        &self.blobs
    }
}
impl Ord for Requirement {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.name
            .cmp(other.name())
            .then(self.blobs.cmp(other.blobs()))
    }
}
impl PartialOrd for Requirement {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl fmt::Display for Requirement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} {}\ncomponents: {:#?}\n",
            style(&self.name).green(),
            self.version_req,
            self.blobs
        )
    }
}
impl From<(Derivation, BTreeSet<Blob>)> for Requirement {
    fn from(data: (Derivation, BTreeSet<Blob>)) -> Self {
        Self::new(
            data.0.name,
            extra::exact_version_req(data.0.version),
            data.1,
        )
    }
}