use std::collections::BTreeMap;
use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::error::{CoreError, ErrorCode};
use crate::fs::Fs;
pub const MANIFEST_FILE: &str = "machine.json";
pub const EXPORT_FILE: &str = "EXPORT.json";
pub const MANIFEST_SCHEMA: u32 = 1;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MachineManifest {
pub schema: u32,
pub store_id: String,
pub machine_id: String,
pub label: String,
pub os: String,
pub app_version: String,
pub pushed_at: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub head: Option<String>,
#[serde(default)]
pub tags: BTreeMap<String, String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mirror: Option<String>,
#[serde(default)]
pub archives: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ExportInfo {
pub schema: u32,
pub commit: String,
pub time: String,
pub machine: String,
}
impl MachineManifest {
pub fn load(fs: &dyn Fs, dir: &Path) -> Option<Self> {
let text = fs.read_to_string(&dir.join(MANIFEST_FILE)).ok()?;
serde_json::from_str(&text).ok()
}
pub fn write(&self, fs: &dyn Fs, dir: &Path) -> Result<(), CoreError> {
let text = serde_json::to_string_pretty(self).map_err(|err| {
CoreError::new(
ErrorCode::InvalidValue,
format!("cannot write manifest: {err}"),
)
})?;
crate::apply::write_atomic(fs, &dir.join(MANIFEST_FILE), text.as_bytes())
}
}