summaryrefslogtreecommitdiffstats
path: root/src/package/dependency/runtime.rs
blob: 40ec93c0ba451d88b0710ca8633bad8abc1a1689 (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
//
// 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 anyhow::Result;
use serde::Deserialize;
use serde::Serialize;

use crate::package::PackageName;
use crate::package::PackageVersionConstraint;
use crate::package::dependency::ParseDependency;
use crate::package::dependency::StringEqual;
use crate::package::dependency::condition::Condition;

/// A dependency that is packaged and is required during runtime
#[derive(Serialize, Deserialize, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[serde(untagged)]
pub enum Dependency {
    Simple(String),
    Conditional {
        name: String,
        condition: Condition,
    },
}

impl AsRef<str> for Dependency {
    fn as_ref(&self) -> &str {
        match self {
            Dependency::Simple(name) => name,
            Dependency::Conditional { name, .. } => name,
        }
    }
}

impl StringEqual for Dependency {
    fn str_equal(&self, s: &str) -> bool {
        match self {
            Dependency::Simple(name) => name == s,
            Dependency::Conditional { name, .. } => name == s,
        }
    }
}

impl From<String> for Dependency {
    fn from(s: String) -> Dependency {
        Dependency::Simple(s)
    }
}

impl ParseDependency for Dependency {
    fn parse_as_name_and_version(&self) -> Result<(PackageName, PackageVersionConstraint)> {
        crate::package::dependency::parse_package_dependency_string_into_name_and_version(self.as_ref())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::package::dependency::condition::OneOrMore;

    #[derive(serde::Deserialize)]
    #[allow(unused)]
    pub struct TestSetting {
        setting: Dependency,
    }

    #[test]
    fn test_parse_dependency() {
        let s: TestSetting = toml::from_str(r#"setting = "foo""#).expect("Parsing TestSetting failed");

        match s.setting {
            Dependency::Simple(name) => assert_eq!(name, "foo", "Expected 'foo', got {}", name),
            other => panic!("Unexpected deserialization to other variant: {:?}", other),
        }
    }

    #[test]
    fn test_parse_conditional_dependency() {
        let s: TestSetting = toml::from_str(r#"setting = { name = "foo", condition = { in_image = "bar"} }"#).expect("Parsing TestSetting failed");
        match s.setting {
            Dependency::Conditional { name, condition } => {
                assert_eq!(name, "foo", "Expected 'foo', got {}", name);
                assert_eq!(*condition.has_env(), None);
                assert_eq!(*condition.env_eq(), None);
                assert_eq!(condition.in_image().as_ref(), Some(&OneOrMore::<String>::One(String::from("bar"))));
            },
            other => panic!("Unexpected deserialization to other variant: {:?}", other),
        }
    }

    #[test]
    fn test_parse_conditional_dependency_pretty() {
        let pretty = r#"
            [setting]
            name = "foo"
            [setting.condition]
            in_image = "bar"
        "#;

        let s: TestSetting = toml::from_str(pretty).expect("Parsing TestSetting failed");

        match s.setting {
            Dependency::Conditional { name, condition } => {
                assert_eq!(name, "foo", "Expected 'foo', got {}", name);
                assert_eq!(*condition.has_env(), None);
                assert_eq!(*condition.env_eq(), None);
                assert_eq!(condition.in_image().as_ref(), Some(&OneOrMore::<String>::One(String::from("bar"))));
            },
            other => panic!("Unexpected deserialization to other variant: {:?}", other),
        }
    }


    #[derive(serde::Serialize, serde::Deserialize)]
    #[allow(unused)]
    pub struct TestSettings {
        settings: Vec<Dependency>,
    }

    #[test]
    fn test_parse_conditional_dependencies() {
        let s: TestSettings = toml::from_str(r#"settings = [{ name = "foo", condition = { in_image = "bar"} }]"#).expect("Parsing TestSetting failed");
        match s.settings.get(0).expect("Has not one dependency") {
            Dependency::Conditional { name, condition } => {
                assert_eq!(name, "foo", "Expected 'foo', got {}", name);
                assert_eq!(*condition.has_env(), None);
                assert_eq!(*condition.env_eq(), None);
                assert_eq!(condition.in_image().as_ref(), Some(&OneOrMore::<String>::One(String::from("bar"))));
            },
            other => panic!("Unexpected deserialization to other variant: {:?}", other),
        }
    }

    #[test]
    fn test_parse_conditional_dependencies_pretty() {
        let pretty = r#"
            [[settings]]
            name = "foo"
            condition = { in_image = "bar" }
        "#;

        let s: TestSettings = toml::from_str(pretty).expect("Parsing TestSetting failed");

        match s.settings.get(0).expect("Has not one dependency") {
            Dependency::Conditional { name, condition } => {
                assert_eq!(name, "foo", "Expected 'foo', got {}", name);
                assert_eq!(*condition.has_env(), None);
                assert_eq!(*condition.env_eq(), None);
                assert_eq!(condition.in_image().as_ref(), Some(&OneOrMore::<String>::One(String::from("bar"))));
            },
            other => panic!("Unexpected deserialization to other variant: {:?}", other),
        }
    }

    #[test]
    fn test_parse_conditional_dependencies_pretty_2() {
        let pretty = r#"
            [[settings]]
            name = "foo"
            condition.in_image = "bar"
        "#;

        let s: TestSettings = toml::from_str(pretty).expect("Parsing TestSetting failed");

        match s.settings.get(0).expect("Has not one dependency") {
            Dependency::Conditional { name, condition } => {
                assert_eq!(name, "foo", "Expected 'foo', got {}", name);
                assert_eq!(*condition.has_env(), None);
                assert_eq!(*condition.env_eq(), None);
                assert_eq!(condition.in_image().as_ref(), Some(&OneOrMore::<String>::One(String::from("bar"))));
            },
            other => panic!("Unexpected deserialization to other variant: {:?}", other),
        }
    }
}