SHA256
108 lines
3.2 KiB
Rust
108 lines
3.2 KiB
Rust
// <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::{Deserialize, Serialize};
|
|
use std::fs;
|
|
use toml;
|
|
|
|
const CONFIG_PATH: &str = "/etc/hive-up.toml";
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct HiveConfig {
|
|
// Matches [kernel] in TOML
|
|
pub kernel: Option<KernelConfig>,
|
|
// Updates [update] in TOML
|
|
pub update: UpdateConfig,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct KernelConfig {
|
|
/// kernel cmd string that will be passed as kernel boot parameter.
|
|
pub cmd: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct UpdateConfig {
|
|
/// Interval between updates check in hours. 0 - no auto check for updates.
|
|
pub 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() -> Result<HiveConfig, ConfigError> {
|
|
let file_content = fs::read_to_string(CONFIG_PATH)?;
|
|
let config: HiveConfig = toml::from_str(&file_content)?;
|
|
Ok(config)
|
|
}
|
|
|
|
// pub fn change_updates_check_interval(new_interval: u64) -> Result<HiveConfig, ConfigError> {
|
|
// let mut config: HiveConfig = read_config()?;
|
|
// config.update.updates_check_interval = new_interval;
|
|
// let conf_str = toml::to_string_pretty(&config)?;
|
|
// fs::write(CONFIG_PATH, conf_str)?;
|
|
|
|
// Ok(config)
|
|
// }
|