mirror of
https://github.com/edera-dev/sprout.git
synced 2025-12-19 15:40:16 +00:00
improve configuration loading mechanism
This commit is contained in:
@@ -5,11 +5,12 @@ use crate::extractors::ExtractorDeclaration;
|
||||
use crate::generators::GeneratorDeclaration;
|
||||
use crate::phases::PhasesConfiguration;
|
||||
use crate::utils;
|
||||
use anyhow::Context;
|
||||
use anyhow::Result;
|
||||
use anyhow::{Context, bail};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeMap;
|
||||
use std::ops::Deref;
|
||||
use toml::Value;
|
||||
use uefi::proto::device_path::LoadedImageDevicePath;
|
||||
|
||||
#[derive(Serialize, Deserialize, Default, Clone)]
|
||||
@@ -36,7 +37,7 @@ pub fn latest_version() -> u32 {
|
||||
1
|
||||
}
|
||||
|
||||
pub fn load() -> Result<RootConfiguration> {
|
||||
fn load_raw_config() -> Result<Vec<u8>> {
|
||||
let current_image_device_path_protocol =
|
||||
uefi::boot::open_protocol_exclusive::<LoadedImageDevicePath>(uefi::boot::image_handle())
|
||||
.context("unable to get loaded image device path")?;
|
||||
@@ -44,7 +45,28 @@ pub fn load() -> Result<RootConfiguration> {
|
||||
|
||||
let content = utils::read_file_contents(&path, "sprout.toml")
|
||||
.context("unable to read sprout.toml file")?;
|
||||
let config: RootConfiguration =
|
||||
toml::from_slice(&content).context("unable to parse sprout.toml file")?;
|
||||
Ok(content)
|
||||
}
|
||||
|
||||
pub fn load() -> Result<RootConfiguration> {
|
||||
let content = load_raw_config()?;
|
||||
let value: Value = toml::from_slice(&content).context("unable to parse sprout.toml file")?;
|
||||
|
||||
let version = value
|
||||
.get("version")
|
||||
.cloned()
|
||||
.unwrap_or_else(|| Value::Integer(latest_version() as i64));
|
||||
|
||||
let version: u32 = version
|
||||
.try_into()
|
||||
.context("unable to get configuration version")?;
|
||||
|
||||
if version != latest_version() {
|
||||
bail!("unsupported configuration version: {}", version);
|
||||
}
|
||||
|
||||
let config: RootConfiguration = value
|
||||
.try_into()
|
||||
.context("unable to parse sprout.toml file")?;
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user