summaryrefslogtreecommitdiffstats
path: root/src/package/dependency/build.rs
blob: 42c0763e7afcfd0cb40d4714897b1be81247cd08 (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
//
// 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 only required during build time
#[derive(Serialize, Deserialize, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[serde(untagged)]
pub enum BuildDependency {
    Simple(String),
    Conditional(String, Condition),
}

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

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

impl ParseDependency for BuildDependency {
    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::*;

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

    #[test]
    fn test_parse_dependency() {
        let s: TestSetting = toml::from_str(r#"setting = "foo""#).expect("Parsing TestSetting failed");
        match s.setting {
            BuildDependency::Simple(name) => assert_eq!(name, "foo", "Expected 'foo', got {}", name),
            other => panic!("Unexpected deserialization to other variant: {:?}", other),
        }
    }
}