mod deser;
pub mod error;
pub mod generation;
pub mod v1;
use std::collections::HashMap;
use std::fmt;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use crate::error::{BootspecError, SynthesizeError};
use crate::generation::Generation;
#[doc(hidden)]
pub(crate) type Result<T, E = BootspecError> = core::result::Result<T, E>;
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq, Hash)]
pub struct SpecialisationName(pub String);
impl fmt::Display for SpecialisationName {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct SystemConfigurationRoot(pub PathBuf);
pub const JSON_FILENAME: &str = "boot.json";
pub type Extensions = HashMap<String, serde_json::Value>;
pub type BootSpec = v1::GenerationV1;
pub type Specialisations = v1::SpecialisationsV1;
pub type Specialisation = v1::SpecialisationV1;
pub const SCHEMA_VERSION: u64 = v1::SCHEMA_VERSION;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct BootJson {
#[serde(flatten)]
pub generation: Generation,
#[serde(
default = "HashMap::new",
skip_serializing_if = "HashMap::is_empty",
deserialize_with = "deser::skip_generation_fields",
flatten
)]
pub extensions: Extensions,
}
impl BootJson {
pub fn synthesize_latest(generation_path: &Path) -> Result<BootJson> {
Self::synthesize_version(generation_path, SCHEMA_VERSION)
}
pub fn synthesize_version(generation_path: &Path, version: u64) -> Result<BootJson> {
let generation = match version {
v1::SCHEMA_VERSION => {
let generation = v1::GenerationV1::synthesize(generation_path)?;
Generation::V1(generation)
}
v => {
return Err(BootspecError::Synthesize(
SynthesizeError::UnsupportedVersion(v),
))
}
};
Ok(BootJson {
generation,
extensions: HashMap::new(),
})
}
}