ips/libips/src/image/mod.rs

68 lines
1.7 KiB
Rust
Raw Normal View History

mod properties;
2025-07-26 12:54:01 +02:00
use properties::*;
use miette::Diagnostic;
2025-07-26 12:54:01 +02:00
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<T> = std::result::Result<T, ImageError>;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Image {
path: PathBuf,
props: Vec<ImageProperty>,
version: i32,
variants: HashMap<String, String>,
mediators: HashMap<String, String>,
}
impl Image {
pub fn new<P: Into<PathBuf>>(path: P) -> Image {
2025-07-26 12:54:01 +02:00
Image {
path: path.into(),
version: 5,
variants: HashMap::new(),
mediators: HashMap::new(),
props: vec![],
}
}
pub fn open<P: AsRef<Path>>(path: P) -> Result<Image> {
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<P: AsRef<Path>>(path: P) -> Image {
if let Ok(img) = Image::open(path.as_ref()) {
img
} else {
Image::new(path.as_ref())
}
}
2025-07-26 12:54:01 +02:00
}