summaryrefslogtreecommitdiffstats
path: root/src/package/dependency/condition.rs
blob: 81892c727064b776f301c75621b0acd6db7f0120 (plain)
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
//
// Copyright (c) 2020-2021 science+computing ag and other contributors
//
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//

use std::collections::HashMap;

use serde::Deserialize;
use serde::Serialize;
use getset::Getters;

use crate::util::EnvironmentVariableName;
use crate::util::docker::ImageName;

/// The Condition type
///
/// This type represents a condition whether a dependency should be included in the package tree or
/// not.
///
/// Right now, we are supporting condition by environment (set or equal) or whether a specific
/// build image is used.
/// All these settings are optional, of course.
///
#[derive(Serialize, Deserialize, Getters, Clone, Debug, Eq, PartialEq)]
pub struct Condition {
    #[serde(rename = "has_env", skip_serializing_if = "Option::is_none")]
    #[getset(get = "pub")]
    pub(super) has_env: Option<OneOrMore<EnvironmentVariableName>>,

    #[serde(rename = "env_eq", skip_serializing_if = "Option::is_none")]
    #[getset(get = "pub")]
    pub(super) env_eq: Option<HashMap<EnvironmentVariableName, String>>,

    #[serde(rename = "in_image", skip_serializing_if = "Option::is_none")]
    #[getset(get = "pub")]
    pub(super) in_image: Option<OneOrMore<String>>,
}

/// Manual implementation of PartialOrd for Condition
///
/// Because HashMap does not implement PartialOrd
impl PartialOrd for Condition {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        use std::cmp::Ordering as O;

        let cmp_has_env = match (self.has_env.as_ref(), other.has_env.as_ref()) {
            (Some(a), Some(b)) => a.partial_cmp(b),
            (Some(_), None)    => Some(O::Greater),
            (None, Some(_))    => Some(O::Less),
            (None, None)       => Some(O::Equal),
        };

        if cmp_has_env.as_ref().map(|o| *o != O::Equal).unwrap_or(false) {
            return cmp_has_env
        }

        let cmp_env_eq = match (self.env_eq.as_ref(), other.env_eq.as_ref()) {
            // TODO: Is this safe? We ignore the HashMaps here and just say they are equal. They are most certainly not.
            (Some(_), Some(_)) => Some(O::Equal),
            (Some(_), None)    => Some(O::Greater),
            (None, Some(_))    => Some(O::Less),
            (None, None)       => Some(O::Equal),
        };

        if cmp_env_eq.as_ref().map(|o| *o != O::Equal).unwrap_or(false) {
            return cmp_env_eq
        }

        match (self.in_image.as_ref(), other.in_image.as_ref()) {
            (Some(a), Some(b)) => a.partial_cmp(b),
            (Some(_), None)    => Some(O::Greater),
            (None, Some(_))    => Some(O::Less),
            (None, None)       => Some(O::Equal),
        }
    }
}

/// Manual implementation of Ord for Condition
///
/// Because HashMap does not implement Ord
impl Ord for Condition {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.partial_cmp(other).unwrap_or(std::cmp::Ordering::Equal)
    }
}

/// Manual implementation of Hash for Condition
///
/// Because HashMap does not implement Hash
impl std::hash::Hash for Condition {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.has_env.hash(state);
        if let Some(hm) = self.env_eq.as_ref() {
            hm.iter().for_each(|(k, v)| (k, v).hash(state));
        };
        self.in_image.hash(state);
    }
}


/// Helper type for supporting Vec<T> and T in value
/// position of Condition
#[derive(Serialize, Deserialize, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[serde(untagged)]
pub enum OneOrMore<T: Sized> {
    One(T),
    More(Vec<T>),
}

impl<T: Sized> Into<Vec<T>> for OneOrMore<T> {
    fn into(self) -> Vec<T> {
        match self {
            OneOrMore::One(o) => vec![o],
            OneOrMore::More(m) => m,
        }
    }
}

#[cfg(test)]
impl From<Vec<String>> for OneOrMore<String> {
    fn from(v: Vec<String>) -> Self {
        OneOrMore::More(v)
    }
}

#[cfg(test)]
impl From<String> for OneOrMore<String> {
    fn from(s: String) -> Self {
        OneOrMore::One(s)
    }
}


#[derive(Debug)]
pub struct ConditionData<'a> {
    pub(crate) image_name: Option<&'a ImageName>,
    pub(crate) env: &'a [(EnvironmentVariableName, String)],
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_has_env_deserialization() {
        let s = r#"has_env = "foo""#;
        let c: Condition = toml::from_str(s).expect("Deserializing has_env");

        assert_eq!(c.has_env.unwrap(), OneOrMore::<EnvironmentVariableName>::One(EnvironmentVariableName::from("foo")));
        assert!(c.env_eq.is_none());
        assert!(c.in_image.is_none());
    }

    #[test]
    fn test_has_env_list_deserialization() {
        let s = r#"has_env = ["foo", "bar"]"#;
        let c: Condition = toml::from_str(s).expect("Deserializing has_env");

        assert_eq!(c.has_env.unwrap(), {
            OneOrMore::<EnvironmentVariableName>::More({
                vec![EnvironmentVariableName::from("foo"), EnvironmentVariableName::from("bar")]
            })
        });
        assert!(c.env_eq.is_none());
        assert!(c.in_image.is_none());
    }

    #[test]
    fn test_env_eq_deserialization() {
        let s = r#"env_eq = { "foo" = "bar" }"#;
        let c: Condition = toml::from_str(s).expect("Deserializing has_env");

        assert!(c.has_env.is_none());
        assert_eq!(c.env_eq.unwrap(), {
            let mut hm = HashMap::new();
            hm.insert(EnvironmentVariableName::from("foo"), String::from("bar"));
            hm
        });
        assert!(c.in_image.is_none());
    }

    #[test]
    fn test_in_image_deserialization() {
        let s = r#"in_image = "foo""#;
        let c: Condition = toml::from_str(s).expect("Deserializing has_env");

        assert!(c.has_env.is_none());
        assert!(c.env_eq.is_none());
        assert_eq!(c.in_image.unwrap(), OneOrMore::<String>::One(String::from("foo")));
    }

    #[test]
    fn test_in_image_list_deserialization() {
        let s = r#"in_image = ["foo"]"#;
        let c: Condition = toml::from_str(s).expect("Deserializing has_env");

        assert!(c.has_env.is_none());
        assert!(c.env_eq.is_none());
        assert_eq!(c.in_image.unwrap(), OneOrMore::<String>::More(vec![String::from("foo")]));
    }

}