use std::io::Read;
use crate::model::AssetId;
use crate::bundle::{ASSETS_DIR, BundleError, BundlePath, BundleRoot, open_bundle_file};
#[derive(Clone, Debug)]
pub struct ParticipantAssets {
root: BundleRoot,
}
impl ParticipantAssets {
pub(crate) const fn new(root: BundleRoot) -> Self {
Self { root }
}
pub(crate) fn relocate(&mut self, path: std::path::PathBuf) {
self.root.relocate(path);
}
pub fn read(&self, id: &AssetId) -> Result<Vec<u8>, BundleError> {
let path = Self::path(id)?;
let mut file = open_bundle_file(&self.root, &path)?;
let mut bytes = Vec::new();
file.read_to_end(&mut bytes)
.map_err(|source| BundleError::ReadFile {
path: path.filesystem_path(self.root.path()),
source,
})?;
Ok(bytes)
}
pub(crate) fn path(id: &AssetId) -> Result<BundlePath, BundleError> {
Ok(BundlePath::new(format!("{ASSETS_DIR}/{}", id.as_str()))?)
}
}