release_manager/config/
v2.rs

1// This file is part of Release Manager
2
3// Release Manager is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7
8// Release Manager is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU General Public License for more details.
12
13// You should have received a copy of the GNU General Public License
14// along with Release Manager  If not, see <http://www.gnu.org/licenses/>.
15
16use std::collections::HashMap;
17use std::convert::TryFrom;
18
19use super::{ConfigTrait, Target, Arch, OS};
20
21#[derive(Serialize, Deserialize)]
22pub struct Config {
23    pub release_path: String,
24    pub included_files: Vec<String>,
25    pub config: HashMap<String, HashMap<String, Vec<TargetConfig>>>,
26}
27
28impl Config {
29    pub fn included_files(&self) -> &[String] {
30        self.included_files.as_ref()
31    }
32}
33
34impl ConfigTrait for Config {
35    type Previous = super::v1::Config;
36
37    fn targets(&self) -> Vec<Target> {
38        let mut targets = Vec::new();
39
40        for (os_str, value) in self.config.iter() {
41            if let Ok(os) = OS::try_from(os_str.as_ref()) {
42                build_os(&mut targets, os, value);
43            } else {
44                debug!("{}, is not a valid Operating System!", os_str);
45            }
46        }
47
48        targets
49    }
50}
51
52impl From<super::v1::Config> for Config {
53    fn from(c: super::v1::Config) -> Self {
54        let mut config: HashMap<String, HashMap<String, Vec<TargetConfig>>> = HashMap::new();
55
56        for (os, arch_hash) in c.config {
57            for (arch, target) in arch_hash {
58                let os_entry = config.entry(os.clone().into()).or_insert(HashMap::new());
59                let arch_entry = os_entry.entry(arch.into()).or_insert(Vec::new());
60                arch_entry.push(target.into());
61            }
62        }
63
64        Config {
65            release_path: c.release_path,
66            included_files: c.included_files,
67            config: config,
68        }
69    }
70}
71
72#[derive(Serialize, Deserialize)]
73pub struct TargetConfig {
74    build_name: Option<String>,
75    libs: Vec<String>,
76    env: HashMap<String, String>,
77}
78
79impl From<super::TargetConfig> for TargetConfig {
80    fn from(tc: super::TargetConfig) -> Self {
81        TargetConfig {
82            build_name: None,
83            libs: tc.libs,
84            env: tc.env,
85        }
86    }
87}
88
89fn add_target(targets: &mut Vec<Target>, os: OS, arch: Arch, tc: &TargetConfig) {
90    if let Ok(mut target) = Target::new(os.clone(), arch, tc.build_name.clone()) {
91        target.add_libs(&tc.libs);
92        target.add_env(&tc.env);
93        targets.push(target);
94    }
95}
96
97fn build_os(targets: &mut Vec<Target>, os: OS, value: &HashMap<String, Vec<TargetConfig>>) {
98    for (arch, tcs) in value.iter() {
99        if let Ok(arc) = Arch::try_from(arch.as_ref()) {
100            for target in tcs {
101                add_target(targets, os.clone(), arc.clone(), target);
102            }
103        } else {
104            debug!("{} is not a valid architecture!", arch);
105        }
106    }
107}