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
// This file 'config_models.rs' is part of the 'hisho' project.
//
// Copyright 2023 Thomas Obernosterer (https://atjon.tv).
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Debug, Deserialize, Serialize)]
pub struct Project {
    pub name: String,
    #[serde(default)]
    pub environments: Environments,
    #[serde(default)]
    pub containers: Containers,
    #[serde(default)]
    pub build: BuildSteps,
    #[serde(default)]
    pub services: Services,
    #[serde(default)]
    pub commands: Commands,

    // this is a runtime variable
    #[serde(skip)]
    pub workdir: String,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Environment {
    pub name: String,
    #[serde(default)]
    pub system: Vec<String>,
    #[serde(default)]
    pub inherits: Vec<String>,
    #[serde(default)]
    pub values: HashMap<String, String>,
    #[serde(default)]
    pub sources: Vec<String>,
}
pub type Environments = Vec<Environment>;

impl Environment {
    pub fn new_empty() -> Environment {
        Environment {
            name: "empty".to_string(),
            system: Vec::new(),
            inherits: Vec::new(),
            values: HashMap::new(),
            sources: Vec::new(),
        }
    }
    pub fn new(name: &str, inherits: Vec<String>, values: HashMap<String, String>) -> Environment {
        Environment {
            name: name.to_string(),
            system: Vec::new(),
            inherits,
            values,
            sources: Vec::new(),
        }
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Container {
    pub name: String,
}
pub type Containers = Vec<Container>;

#[derive(Debug, Deserialize, Serialize)]
pub struct Command {
    pub name: String,
    #[serde(default)]
    pub environment: String,
    #[serde(default)]
    pub shell: Vec<Process>,
    #[serde(default)]
    #[deprecated(note = "this field is no longer unused")]
    pub args: HashMap<String, String>,
    #[serde(default)]
    pub depends_on_build: Vec<String>,
}
pub type Commands = Vec<Command>;

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Process {
    pub command: String,
    #[serde(default)]
    pub args: Vec<String>,
    #[serde(default)]
    pub cwd: String,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct BuildStep {
    pub name: String,
    pub shell: Vec<Process>,
    #[serde(default)]
    pub depends_on: Vec<String>,
    #[serde(default)]
    pub input_files: Vec<String>,
}
pub type BuildSteps = Vec<BuildStep>;

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Service {
    pub name: String,
    pub protocol: ServiceProtocol,
    pub uri: String,
}
pub type Services = Vec<Service>;

#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
pub enum ServiceProtocol {
    HTTP,
    TCP,
}

impl PartialEq for BuildStep {
    fn eq(&self, other: &Self) -> bool {
        self.name.eq(&other.name)
    }
}