mod properties; use properties::*; use miette::Diagnostic; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::fs::File; use std::path::{Path, PathBuf}; use thiserror::Error; #[derive(Debug, Error, Diagnostic)] pub enum ImageError { #[error("I/O error: {0}")] #[diagnostic( code(ips::image_error::io), help("Check system resources and permissions") )] IO(#[from] std::io::Error), #[error("JSON error: {0}")] #[diagnostic( code(ips::image_error::json), help("Check the JSON format and try again") )] Json(#[from] serde_json::Error), } pub type Result = std::result::Result; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Image { path: PathBuf, props: Vec, version: i32, variants: HashMap, mediators: HashMap, } impl Image { pub fn new>(path: P) -> Image { Image { path: path.into(), version: 5, variants: HashMap::new(), mediators: HashMap::new(), props: vec![], } } pub fn open>(path: P) -> Result { let path = path.as_ref(); //TODO: Parse the old INI format of pkg5 //TODO once root images are implemented, look for metadata under sub directory var/pkg let props_path = path.join("pkg6.image.json"); let mut f = File::open(props_path)?; Ok(serde_json::from_reader(&mut f)?) } pub fn open_default>(path: P) -> Image { if let Ok(img) = Image::open(path.as_ref()) { img } else { Image::new(path.as_ref()) } } }