configuration.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use crate::errors::{ Error, Result }; use crate::math::{ Vector2 }; use kdl::{ KdlDocument, KdlValue }; use std::ffi::{ OsStr }; use std::path::{ Path }; pub trait KdlConfigurable { fn from_kdl_document(kdl: &KdlDocument) -> Result<Self> where Self: Sized; } #[derive(Debug, Default)] pub struct ApplicationConfiguration { window: WindowConfiguration, } impl ApplicationConfiguration { pub fn is_configuration_file<P: AsRef<Path>>(path: P) -> bool { path .as_ref() .file_name() .and_then(OsStr::to_str) .map(|filename| filename == "crucible.kdl") .unwrap_or(false) } pub fn window(&self) -> &WindowConfiguration { &self.window } } impl KdlConfigurable for ApplicationConfiguration { fn from_kdl_document(kdl: &KdlDocument) -> Result<ApplicationConfiguration> { let window = kdl .get("window") .and_then(|node| node.children()) .map(|children| WindowConfiguration::from_kdl_document(children)) .unwrap_or(Ok(WindowConfiguration::default()))?; Ok(ApplicationConfiguration { window, }) } } #[derive(Debug)] pub struct WindowConfiguration { initial_title: String, initial_size: Vector2<u32>, } impl WindowConfiguration { pub fn initial_title(&self) -> &str { &self.initial_title } pub fn initial_size(&self) -> &Vector2<u32> { &self.initial_size } } impl KdlConfigurable for WindowConfiguration { fn from_kdl_document(kdl: &KdlDocument) -> Result<WindowConfiguration> { let initial_title = kdl .get("initial-title") .and_then(|node| node.get(0)) .and_then(|value| value.as_string()) .unwrap_or("Crucible Application") .to_string(); let initial_size = kdl .get("initial-size") .and_then(|node| node.children()) .map(|children| Vector2::from_kdl_document(children)) .unwrap_or(Ok(Vector2::new(1280, 720)))?; Ok(WindowConfiguration { initial_title, initial_size, }) } } impl Default for WindowConfiguration { fn default() -> WindowConfiguration { WindowConfiguration { initial_title: "Crucible Application".to_string(), initial_size: Vector2::new(1280, 720), } } } impl<T: TryFromKdlValue> KdlConfigurable for Vector2<T> { fn from_kdl_document(kdl: &KdlDocument) -> Result<Vector2<T>> { let x = kdl .get("x") .and_then(|node| node.get(0)) .map(|value| TryFromKdlValue::try_from_kdl_value(value)) .ok_or(Error::ConfigurationError("Expected a number".to_string())) .flatten()?; let y = kdl .get("y") .and_then(|node| node.get(0)) .map(|value| TryFromKdlValue::try_from_kdl_value(value)) .ok_or(Error::ConfigurationError("Expected a number".to_string())) .flatten()?; Ok(Vector2::new(x, y)) } } pub trait TryFromKdlValue { fn try_from_kdl_value(kdl: &KdlValue) -> Result<Self> where Self: Sized; } impl TryFromKdlValue for u8 { fn try_from_kdl_value(kdl: &KdlValue) -> Result<u8> { let value = kdl .as_integer() .map(|value| TryInto::<u8>::try_into(value)) .ok_or(Error::ConfigurationError("Expected a u8 value".to_string())) .map(|inner| inner.map_err(|_| Error::ConfigurationError("u8 value out of range".to_string()))) .flatten()?; Ok(value) } } impl TryFromKdlValue for u16 { fn try_from_kdl_value(kdl: &KdlValue) -> Result<u16> { let value = kdl .as_integer() .map(|value| TryInto::<u16>::try_into(value)) .ok_or(Error::ConfigurationError("Expected a u16 value".to_string())) .map(|inner| inner.map_err(|_| Error::ConfigurationError("u16 value out of range".to_string()))) .flatten()?; Ok(value) } } impl TryFromKdlValue for u32 { fn try_from_kdl_value(kdl: &KdlValue) -> Result<u32> { let value = kdl .as_integer() .map(|value| TryInto::<u32>::try_into(value)) .ok_or(Error::ConfigurationError("Expected a u32 value".to_string())) .map(|inner| inner.map_err(|_| Error::ConfigurationError("u32 value out of range".to_string()))) .flatten()?; Ok(value) } } impl TryFromKdlValue for u64 { fn try_from_kdl_value(kdl: &KdlValue) -> Result<u64> { let value = kdl .as_integer() .map(|value| TryInto::<u64>::try_into(value)) .ok_or(Error::ConfigurationError("Expected a u64 value".to_string())) .map(|inner| inner.map_err(|_| Error::ConfigurationError("u64 value out of range".to_string()))) .flatten()?; Ok(value) } } impl TryFromKdlValue for u128 { fn try_from_kdl_value(kdl: &KdlValue) -> Result<u128> { let value = kdl .as_integer() .map(|value| TryInto::<u128>::try_into(value)) .ok_or(Error::ConfigurationError("Expected a u128 value".to_string())) .map(|inner| inner.map_err(|_| Error::ConfigurationError("u128 value out of range".to_string()))) .flatten()?; Ok(value) } } impl TryFromKdlValue for i8 { fn try_from_kdl_value(kdl: &KdlValue) -> Result<i8> { let value = kdl .as_integer() .map(|value| TryInto::<i8>::try_into(value)) .ok_or(Error::ConfigurationError("Expected a i8 value".to_string())) .map(|inner| inner.map_err(|_| Error::ConfigurationError("i8 value out of range".to_string()))) .flatten()?; Ok(value) } } impl TryFromKdlValue for i16 { fn try_from_kdl_value(kdl: &KdlValue) -> Result<i16> { let value = kdl .as_integer() .map(|value| TryInto::<i16>::try_into(value)) .ok_or(Error::ConfigurationError("Expected a i16 value".to_string())) .map(|inner| inner.map_err(|_| Error::ConfigurationError("i16 value out of range".to_string()))) .flatten()?; Ok(value) } } impl TryFromKdlValue for i32 { fn try_from_kdl_value(kdl: &KdlValue) -> Result<i32> { let value = kdl .as_integer() .map(|value| TryInto::<i32>::try_into(value)) .ok_or(Error::ConfigurationError("Expected a i32 value".to_string())) .map(|inner| inner.map_err(|_| Error::ConfigurationError("i32 value out of range".to_string()))) .flatten()?; Ok(value) } } impl TryFromKdlValue for i64 { fn try_from_kdl_value(kdl: &KdlValue) -> Result<i64> { let value = kdl .as_integer() .map(|value| TryInto::<i64>::try_into(value)) .ok_or(Error::ConfigurationError("Expected a i64 value".to_string())) .map(|inner| inner.map_err(|_| Error::ConfigurationError("i64 value out of range".to_string()))) .flatten()?; Ok(value) } } impl TryFromKdlValue for i128 { fn try_from_kdl_value(kdl: &KdlValue) -> Result<i128> { let value = kdl .as_integer() .map(|value| TryInto::<i128>::try_into(value)) .ok_or(Error::ConfigurationError("Expected a i128 value".to_string())) .map(|inner| inner.map_err(|_| Error::ConfigurationError("i128 value out of range".to_string()))) .flatten()?; Ok(value) } } impl TryFromKdlValue for f32 { fn try_from_kdl_value(kdl: &KdlValue) -> Result<f32> { let value = kdl .as_float() .map(|value| value as f32) .ok_or(Error::ConfigurationError("Expected a f32 value".to_string()))?; Ok(value) } } impl TryFromKdlValue for f64 { fn try_from_kdl_value(kdl: &KdlValue) -> Result<f64> { let value = kdl .as_float() .map(|value| value as f64) .ok_or(Error::ConfigurationError("Expected a f64 value".to_string()))?; Ok(value) } }