Skip to main content

frm/
config.rs

1// Copyright (c) 2025-2026 Michael S. Klishin and Contributors
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use std::fs;
10
11use serde::{Deserialize, Serialize};
12
13use crate::Result;
14use crate::paths::Paths;
15use crate::version::Version;
16
17#[derive(Debug, Clone, Serialize, Deserialize, Default)]
18pub struct Config {
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub default_version: Option<Version>,
21}
22
23impl Config {
24    pub fn load(paths: &Paths) -> Result<Self> {
25        let config_file = paths.config_file();
26        if !config_file.exists() {
27            return Ok(Self::default());
28        }
29
30        let content = fs::read_to_string(config_file)?;
31        let config: Config = toml::from_str(&content)?;
32        Ok(config)
33    }
34
35    pub fn save(&self, paths: &Paths) -> Result<()> {
36        let config_file = paths.config_file();
37        let content = toml::to_string_pretty(self)?;
38        fs::write(config_file, content)?;
39        Ok(())
40    }
41
42    pub fn set_default(&mut self, version: Version) {
43        self.default_version = Some(version);
44    }
45
46    pub fn clear_default(&mut self) {
47        self.default_version = None;
48    }
49}