1use crate::{
2 defaults::window::*,
3 error::{ConfigError, Result},
4};
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct WindowConfig {
9 #[serde(default = "default_width")]
10 pub width: u32,
11 #[serde(default = "default_height")]
12 pub height: u32,
13 pub borderless: bool,
14}
15
16fn default_width() -> u32 {
17 DEFAULT_WIDTH
18}
19
20fn default_height() -> u32 {
21 DEFAULT_HEIGHT
22}
23
24impl Default for WindowConfig {
25 fn default() -> Self {
26 Self {
27 width: DEFAULT_WIDTH,
28 height: DEFAULT_HEIGHT,
29 borderless: BORDERLESS,
30 }
31 }
32}
33
34impl WindowConfig {
35 pub fn validate(&self) -> Result<()> {
36 if self.width < MIN_WIDTH {
37 return Err(ConfigError::ValidationError(format!(
38 "Window width must be at least {}",
39 MIN_WIDTH
40 )));
41 }
42 if self.height < MIN_HEIGHT {
43 return Err(ConfigError::ValidationError(format!(
44 "Window height must be at least {}",
45 MIN_HEIGHT
46 )));
47 }
48 Ok(())
49 }
50
51 pub fn with_dimensions(width: u32, height: u32) -> Result<Self> {
52 let config = Self { width, height, ..Self::default() };
53
54 config.validate()?;
55 Ok(config)
56 }
57}