use super::Spec;
use crate::error::Error;
use crate::platform;
use crate::platform::ffi;
use std::path::Path;
use std::path::PathBuf;
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Bundle {
path: PathBuf,
}
impl Bundle {
pub fn create(path: &Path, spec: &Spec) -> Result<Self, Error> {
Self::at(
ffi::cz_bundle_create(platform::path(path)?, spec.clone()),
format!("create a bundle at {}", path.display()),
)
}
pub fn create_from_data(path: &Path, spec: &[u8]) -> Result<Self, Error> {
Self::at(
ffi::cz_bundle_create_from_data(platform::path(path)?, spec.to_vec()),
format!("create a bundle at {}", path.display()),
)
}
pub fn load(path: &Path) -> Result<Self, Error> {
Self::at(
ffi::cz_bundle_load(platform::path(path)?),
format!("load the bundle at {}", path.display()),
)
}
fn at(outcome: ffi::CzOutcome, action: String) -> Result<Self, Error> {
platform::outcome(outcome, action).map(|outcome| Self {
path: PathBuf::from(outcome.text()),
})
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn config_path(&self) -> Result<PathBuf, Error> {
platform::outcome(
ffi::cz_bundle_config_path(platform::path(&self.path)?),
"find a bundle's config",
)
.map(|outcome| PathBuf::from(outcome.text()))
}
pub fn rootfs_path(&self) -> Result<PathBuf, Error> {
platform::outcome(
ffi::cz_bundle_rootfs_path(platform::path(&self.path)?),
"find a bundle's rootfs",
)
.map(|outcome| PathBuf::from(outcome.text()))
}
pub fn delete(&self) -> Result<(), Error> {
platform::outcome(
ffi::cz_bundle_delete(platform::path(&self.path)?),
format!("delete the bundle at {}", self.path.display()),
)
.map(drop)
}
pub fn load_config(&self) -> Result<Spec, Error> {
platform::outcome(
ffi::cz_bundle_load_config(platform::path(&self.path)?),
format!("load the config of the bundle at {}", self.path.display()),
)
.map(|outcome| outcome.spec())
}
}