Files
hive-up/src/hive_config.rs
T

109 lines
3.2 KiB
Rust
Raw Normal View History

2026-08-21 20:58:06 +02:00
// <hive-up - update program fot Kira Linux.>
// Copyright (C) <2026> <Kira Foundation>
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/*
Functions fot system interaction
*/
use serde::{Serialize, Deserialize};
use std::{fs, io};
use toml;
#[derive(Debug, Serialize, Deserialize)]
pub struct HiveConfig {
// Matches [kernel] in TOML
pub kernel: KernelConfig,
// Updates process config
pub update: UpdateConfig,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct KernelConfig {
/// kernel cmd string that will be passed as kernel boot parameter.
cmd: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateConfig {
/// Interval between updates check in hours. 0 - no auto check for updates.
pub updates_check_interval: u64,
/// Tells hive-up to automatically download updates when awailable.
pub download_updates: bool,
/// Tells hive-up to automatically perform updates during system reboot.
pub auto_update: bool,
}
#[derive(Debug)]
pub enum ConfigError {
IoError(std::io::Error),
TomlParseError(toml::de::Error),
TomlSerError(toml::ser::Error),
}
// 2. Implement From for each wrapped error type
impl From<std::io::Error> for ConfigError {
fn from(err: std::io::Error) -> Self {
ConfigError::IoError(err)
}
}
impl From<toml::de::Error> for ConfigError {
fn from(err: toml::de::Error) -> Self {
ConfigError::TomlParseError(err)
}
}
impl From<toml::ser::Error> for ConfigError {
fn from(err: toml::ser::Error) -> Self {
ConfigError::TomlSerError(err)
}
}
impl std::fmt::Display for ConfigError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
ConfigError::IoError(e) => write!(f, "IO Error: {}", e),
ConfigError::TomlParseError(e) => {
write!(f, "TOML parse error: {}", e)
},
ConfigError::TomlSerError(e) => {
write!(f, "TOML serialize error: {}", e)
},
}
}
}
// Implement std::error::Error
impl std::error::Error for ConfigError {}
pub fn read_config(conf_file: &str) -> Result<HiveConfig, ConfigError> {
let file_content = fs::read_to_string(conf_file)?;
let config:HiveConfig = toml::from_str(&file_content)?;
Ok(config)
}
pub fn change_updates_check_interval(conf_file: &str, new_interval: u64) -> Result<HiveConfig, ConfigError> {
let mut config: HiveConfig = read_config(conf_file)?;
config.update.updates_check_interval = new_interval;
let conf_str = toml::to_string_pretty(&config)?;
fs::write(conf_file, conf_str)?;
Ok(config)
}