use std::{fs, io, path::Path};
use serde::{Deserialize, Serialize};
use crate::AssetId;
pub const MANIFEST_NAME: &str = "manifest.toml";
pub const MANIFEST_VERSION: u32 = 1;
#[derive(Serialize, Deserialize)]
pub struct Manifest {
pub version: u32,
pub assets: Vec<ManifestEntry>,
}
impl Manifest {
pub fn load(path: impl AsRef<Path>) -> io::Result<Self> {
Self::parse(&fs::read_to_string(path)?)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
}
pub fn parse(toml_str: &str) -> Result<Self, ParseManifestError> {
let manifest: Manifest = toml::from_str(toml_str)?;
if manifest.version != MANIFEST_VERSION {
return Err(ParseManifestError::UnsupportedVersion {
found: manifest.version,
});
}
Ok(manifest)
}
pub fn save(&self, path: impl AsRef<Path>) -> io::Result<()> {
let toml_str = toml::to_string_pretty(self).map_err(io::Error::other)?;
fs::write(path, toml_str)
}
}
#[derive(Serialize, Deserialize)]
pub struct ManifestEntry {
pub id: AssetId,
pub file: String,
pub hash: String,
pub content_type: String,
}
#[derive(Debug, thiserror::Error)]
pub enum ParseManifestError {
#[error("invalid manifest TOML: {0}")]
Toml(#[from] toml::de::Error),
#[error("unsupported manifest version {found} (expected {})", MANIFEST_VERSION)]
UnsupportedVersion { found: u32 },
}