Echo Writes Code

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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
use crate::errors::{ Error, Result };

use kdl::{ KdlNode };

use std::ffi::{ OsStr };
use std::fmt::{ self };
use std::path::{ Path };

pub trait KdlConfigurable {
  fn from_kdl_node(node: &KdlNode) -> Result<Self>
    where Self: Sized;
}

#[derive(Debug, Default)]
pub struct ApplicationConfiguration {
  name: String,
  desktop: DesktopConfiguration,
  mobile: MobileConfiguration,
  browser: BrowserConfiguration,
}

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 name(&self) -> &str {
    &self.name
  }

  pub fn desktop(&self) -> &DesktopConfiguration {
    &self.desktop
  }

  pub fn mobile(&self) -> &MobileConfiguration {
    &self.mobile
  }

  pub fn browser(&self) -> &BrowserConfiguration {
    &self.browser
  }
}

impl KdlConfigurable for ApplicationConfiguration {
  fn from_kdl_node(node: &KdlNode) -> Result<ApplicationConfiguration> {
    let Some(children) = node.children() else {
      return Ok(ApplicationConfiguration::default());
    };

    let name = children
      .get("name")
      .and_then(|node| node.get(0))
      .and_then(|value| value.as_string())
      .unwrap_or("Crucible Application")
      .to_string();

    let desktop = children
      .get("desktop")
      .map(|node| DesktopConfiguration::from_kdl_node(node))
      .unwrap_or(Ok(DesktopConfiguration::default()))?;

    let mobile = children
      .get("mobile")
      .map(|node| MobileConfiguration::from_kdl_node(node))
      .unwrap_or(Ok(MobileConfiguration::default()))?;

    let browser = children
      .get("browser")
      .map(|node| BrowserConfiguration::from_kdl_node(node))
      .unwrap_or(Ok(BrowserConfiguration::default()))?;

    Ok(ApplicationConfiguration {
      name,
      desktop,
      mobile,
      browser,
    })
  }
}

#[derive(Debug)]
pub struct DesktopConfiguration {
  main_window: WindowConfiguration,
}

impl DesktopConfiguration {
  pub fn main_window(&self) -> &WindowConfiguration {
    &self.main_window
  }
}

impl Default for DesktopConfiguration {
  fn default() -> DesktopConfiguration {
    DesktopConfiguration {
      main_window: WindowConfiguration::default(),
    }
  }
}

impl KdlConfigurable for DesktopConfiguration {
  fn from_kdl_node(node: &KdlNode) -> Result<DesktopConfiguration> {
    let Some(children) = node.children() else {
      return Ok(DesktopConfiguration::default());
    };

    let main_window = children
      .get("main-window")
      .map(|node| WindowConfiguration::from_kdl_node(node))
      .unwrap_or(Ok(WindowConfiguration::default()))?;

    Ok(DesktopConfiguration {
      main_window,
    })
  }
}

#[derive(Debug)]
pub struct MobileConfiguration {
  is_decorated: bool,
  is_reorientable: bool,
  orientation: Orientation,
}

impl MobileConfiguration {
  pub fn is_decorated(&self) -> bool {
    self.is_decorated
  }

  pub fn is_reorientable(&self) -> bool {
    self.is_reorientable
  }

  pub fn orientation(&self) -> Orientation {
    self.orientation
  }
}

impl KdlConfigurable for MobileConfiguration {
  fn from_kdl_node(node: &KdlNode) -> Result<MobileConfiguration> {
    let Some(children) = node.children() else {
      return Ok(MobileConfiguration::default());
    };

    let is_decorated = children
      .get("is-decorated")
      .and_then(|node| node.get(0))
      .and_then(|value| value.as_bool())
      .unwrap_or(true);

    let is_reorientable = children
      .get("is-reorientable")
      .and_then(|node| node.get(0))
      .and_then(|value| value.as_bool())
      .unwrap_or(true);

    let orientation = children
      .get("orientation")
      .and_then(|node| node.get(0))
      .and_then(|value| value.as_string())
      .map(|keyword| {
        match keyword {
          "portrait" => Ok(Orientation::Portrait),
          "landscape" => Ok(Orientation::Landscape),
          _ => Err(Error::ConfigurationError(format!("Unrecognized orientation: `{}`", keyword))),
        }
      })
      .unwrap_or(Ok(Orientation::Portrait))?;

    Ok(MobileConfiguration {
      is_decorated,
      is_reorientable,
      orientation,
    })
  }
}

impl Default for MobileConfiguration {
  fn default() -> MobileConfiguration {
    MobileConfiguration {
      is_decorated: true,
      is_reorientable: true,
      orientation: Orientation::Portrait,
    }
  }
}

#[derive(Debug)]
pub struct BrowserConfiguration {
  main_canvas: String,
}

impl BrowserConfiguration {
  pub fn main_canvas(&self) -> &str {
    &self.main_canvas
  }
}

impl KdlConfigurable for BrowserConfiguration {
  fn from_kdl_node(node: &KdlNode) -> Result<BrowserConfiguration> {
    let Some(children) = node.children() else {
      return Ok(BrowserConfiguration::default());
    };

    let main_canvas = children
      .get("main-canvas")
      .and_then(|node| node.get(0))
      .and_then(|value| value.as_string())
      .unwrap_or("")
      .to_string();

    Ok(BrowserConfiguration {
      main_canvas
    })
  }
}

impl Default for BrowserConfiguration {
  fn default() -> BrowserConfiguration {
    BrowserConfiguration {
      main_canvas: String::new(),
    }
  }
}

#[derive(Debug)]
pub struct WindowConfiguration {
  is_decorated: bool,
  is_fullscreen: bool,
  is_resizable: bool,
  resolution: ResolutionConfiguration,
  title: String,
}

impl WindowConfiguration {
  pub fn is_decorated(&self) -> bool {
    self.is_decorated
  }

  pub fn is_fullscreen(&self) -> bool {
    self.is_fullscreen
  }

  pub fn is_resizable(&self) -> bool {
    self.is_resizable
  }

  pub fn title(&self) -> &str {
    &self.title
  }

  pub fn resolution(&self) -> &ResolutionConfiguration {
    &self.resolution
  }
}

impl KdlConfigurable for WindowConfiguration {
  fn from_kdl_node(node: &KdlNode) -> Result<WindowConfiguration> {
    let Some(children) = node.children() else {
      return Ok(WindowConfiguration::default());
    };

    let is_decorated = children
      .get("is-decorated")
      .and_then(|node| node.get(0))
      .and_then(|value| value.as_bool())
      .unwrap_or(true);

    let is_fullscreen = children
      .get("is-fullscreen")
      .and_then(|node| node.get(0))
      .and_then(|value| value.as_bool())
      .unwrap_or(false);

    let is_resizable = children
      .get("is-resizable")
      .and_then(|node| node.get(0))
      .and_then(|value| value.as_bool())
      .unwrap_or(true);

    let title = children
      .get("title")
      .and_then(|node| node.get(0))
      .and_then(|value| value.as_string())
      .unwrap_or("Crucible Application")
      .to_string();

    let resolution = children
      .get("resolution")
      .map(|node| ResolutionConfiguration::from_kdl_node(node))
      .unwrap_or(Ok(ResolutionConfiguration::default()))?;

    Ok(WindowConfiguration {
      is_decorated,
      is_fullscreen,
      is_resizable,
      title,
      resolution,
    })
  }
}

impl Default for WindowConfiguration {
  fn default() -> WindowConfiguration {
    WindowConfiguration {
      is_decorated: true,
      is_fullscreen: false,
      is_resizable: true,
      title: "Crucible Application".to_string(),
      resolution: ResolutionConfiguration::default(),
    }
  }
}

#[derive(Debug)]
pub struct ResolutionConfiguration {
  width: u32,
  height: u32,
}

impl ResolutionConfiguration {
  pub fn width(&self) -> u32 {
    self.width
  }

  pub fn height(&self) -> u32 {
    self.height
  }
}

impl KdlConfigurable for ResolutionConfiguration {
  fn from_kdl_node(node: &KdlNode) -> Result<ResolutionConfiguration> {
    let width: u32 = node
      .get("width")
      .and_then(|value| value.as_integer())
      .unwrap_or(1280)
      .try_into()
      .map_err(|_| Error::ConfigurationError("Resolution width out of range".to_string()))?;

    let height: u32 = node
      .get("height")
      .and_then(|value| value.as_integer())
      .unwrap_or(720)
      .try_into()
      .map_err(|_| Error::ConfigurationError("Resolution height out of range".to_string()))?;

    Ok(ResolutionConfiguration {
      width,
      height,
    })
  }
}

impl Default for ResolutionConfiguration {
  fn default() -> ResolutionConfiguration {
    ResolutionConfiguration {
      width: 1280,
      height: 720,
    }
  }
}

#[derive(Clone, Copy, Debug)]
pub enum Orientation {
  Portrait,
  Landscape,
}

impl fmt::Display for Orientation {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match *self {
      Orientation::Portrait => write!(f, "portrait"),
      Orientation::Landscape => write!(f, "landscape"),
    }
  }
}