hyprshell-config-lib 4.10.2

A library for reading, writing and migrating configuration files for hyprshell
Documentation
use crate::CURRENT_CONFIG_VERSION;
use crate::io::load_config_file;
use anyhow::{Context, bail};
use serde::Deserialize;
use std::path::Path;
use tracing::debug_span;

#[derive(Debug, Clone, Deserialize)]
pub(super) struct EmptyConfig {
    pub(super) version: Option<u16>,
}

pub fn check_migration_needed(config_file: &Path) -> anyhow::Result<bool> {
    let _span = debug_span!("check_migration_needed").entered();
    let version = get_config_version(config_file).context("Failed to get config version")?;
    if version > CURRENT_CONFIG_VERSION {
        bail!("Config version is newer than currently supported version");
    }
    Ok(version != CURRENT_CONFIG_VERSION)
}

pub fn get_config_version(config_file: &Path) -> anyhow::Result<u16> {
    let _span = debug_span!("get_config_version").entered();
    if !config_file.exists() {
        bail!("Config file does not exist no need to migrate");
    }

    let config: EmptyConfig = load_config_file(config_file).with_context(|| {
        format!(
            "Failed to load config from file ({})",
            config_file.display()
        )
    })?;
    if let Some(version) = config.version {
        Ok(version)
    } else {
        bail!("Config file does not have a version specified! please generate a new one");
    }
}