Skip to main content

supreme_boards/
board_config.rs

1use std::path::Path;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct BoardConfig {
7    #[serde(default)]
8    pub name: String,
9    #[serde(default)]
10    pub tri_file: String,
11    #[serde(default)]
12    pub mat_file: String,
13    #[serde(default)]
14    pub logo_texture: String,
15    #[serde(default = "default_prop")]
16    pub sidecut_radius: i32,
17    #[serde(default = "default_prop")]
18    pub friction: i32,
19    #[serde(default = "default_prop")]
20    pub flexibility: i32,
21}
22
23fn default_prop() -> i32 {
24    3
25}
26
27impl BoardConfig {
28    #[must_use]
29    pub fn new() -> Self {
30        Self {
31            name: String::new(),
32            tri_file: String::new(),
33            mat_file: String::new(),
34            logo_texture: String::new(),
35            sidecut_radius: 3,
36            friction: 3,
37            flexibility: 3,
38        }
39    }
40}
41
42impl Default for BoardConfig {
43    fn default() -> Self {
44        Self::new()
45    }
46}
47
48/// Parse a `menu_board_cnf.txt` file
49///
50/// # Errors
51///
52/// Returns an error if the file cannot be parsed or if the configuration is invalid.
53pub fn parse_board_config(path: &Path) -> Result<BoardConfig, Box<dyn std::error::Error>> {
54    let mut config: BoardConfig = supreme_parsing::from_path(path)?;
55    config.sidecut_radius = config.sidecut_radius.clamp(1, 5);
56    config.friction = config.friction.clamp(1, 5);
57    config.flexibility = config.flexibility.clamp(1, 5);
58    Ok(config)
59}
60
61/// Write a `BoardConfig` to a `menu_board_cnf.txt` file and an `Info.txt` file in the same directory.
62///
63/// # Errors
64///
65/// Returns an error if either file cannot be written.
66pub fn write_board_config(
67    path: &Path,
68    config: &BoardConfig,
69) -> Result<(), Box<dyn std::error::Error>> {
70    supreme_parsing::to_path(path, config)?;
71
72    if let Some(parent) = path.parent() {
73        let info_path = parent.join("Info.txt");
74        let info_content = format!("Charasteristics {{{}}};\r\n", config.name);
75        let bytes = supreme_parsing::encode_string(&info_content);
76        std::fs::write(&info_path, bytes)?;
77    }
78
79    Ok(())
80}