-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.rs
154 lines (127 loc) · 3.51 KB
/
config.rs
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
use std::{
fs::{read_to_string, File},
ops::Deref,
path::PathBuf,
};
use cached::proc_macro::once;
use serde::{Deserialize, Serialize};
use crate::{root, Res};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Configuration {
pub languages: Vec<Element>,
pub platforms: Vec<Element>,
pub tools: Vec<Tool>,
}
impl Configuration {
pub fn path() -> PathBuf {
let mut config = root();
config.push("config.yml");
config
}
pub fn exists() -> bool {
Self::path().exists()
}
pub fn find_tool(&self, name: &str) -> Option<Tool> {
self.tools
.iter()
.find(|tool| {
tool.element.name == name || tool.element.aliases.iter().any(|alias| alias == name)
})
.cloned()
}
pub fn find_language(&self, name: &str) -> Option<Element> {
if name == "any" {
return Some(Element::default());
}
self.languages
.iter()
.find(|language| {
language.name == name || language.aliases.iter().any(|alias| alias == name)
})
.cloned()
}
pub fn find_platform(&self, name: &str) -> Option<Element> {
if name == "any" {
return Some(Element::default());
}
self.platforms
.iter()
.find(|platform| {
platform.name == name || platform.aliases.iter().any(|alias| alias == name)
})
.cloned()
}
pub fn save(&self) -> Res<()> {
let path = Self::path();
let mut file = File::create(path)?;
serde_yaml::to_writer(&mut file, &self)?;
Ok(())
}
}
impl Default for Configuration {
fn default() -> Self {
Self {
languages: vec![Element {
name: "rust".to_string(),
aliases: vec!["rs".to_string()],
}],
platforms: vec![Element {
name: "x86".to_string(),
aliases: vec!["x86_64".to_string(), "x64".to_string()],
}],
tools: vec![
Tool {
element: Element {
name: "docker".to_string(),
aliases: vec!["dockerfile".to_string()],
},
filename: "Dockerfile".to_string(),
},
Tool {
element: Element {
name: "drone".to_string(),
aliases: vec!["drone.yml".to_string(), ".drone.yml".to_string()],
},
filename: ".drone.yml".to_string(),
},
],
}
}
}
#[once(result = true)]
pub fn load() -> Res<Configuration> {
let path = Configuration::path();
let content = read_to_string(path)?;
Ok(serde_yaml::from_str(&content)?)
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Element {
pub name: String,
pub aliases: Vec<String>,
}
impl Default for Element {
fn default() -> Self {
Self {
name: "any".to_string(),
aliases: vec![],
}
}
}
impl Deref for Element {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.name
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Tool {
#[serde(flatten)]
pub element: Element,
pub filename: String,
}
impl Deref for Tool {
type Target = Element;
fn deref(&self) -> &Self::Target {
&self.element
}
}