mod content_writer;
mod local_content_store;
mod parsed_digest;
pub use self::content_writer::ContentWriter;
pub use self::local_content_store::LocalContentStore;
pub use self::parsed_digest::ParsedDigest;
use crate::error::Error;
use crate::platform;
use crate::platform::ffi;
use std::path::Path;
use std::path::PathBuf;
pub struct Content {
pub(crate) handle: ffi::CzContent,
}
unsafe impl Send for Content {}
unsafe impl Sync for Content {}
impl Content {
pub const MAX_DECODED_SIZE: isize = 4 * 1024 * 1024;
pub fn open(path: &Path) -> Result<Self, Error> {
platform::outcome(
ffi::cz_local_content_open(&path.display().to_string()),
format!("open {} as content", path.display()),
)
.map(|outcome| Self {
handle: outcome.content(),
})
}
pub fn path(&self) -> PathBuf {
PathBuf::from(self.handle.path())
}
pub fn digest(&self) -> Result<String, Error> {
platform::outcome(self.handle.digest(), format!("digest {}", self.path().display())).map(|outcome| outcome.text())
}
pub fn size(&self) -> Result<u64, Error> {
platform::outcome(self.handle.size(), format!("size {}", self.path().display())).map(|outcome| outcome.number())
}
pub fn data(&self) -> Result<Vec<u8>, Error> {
platform::outcome(self.handle.data(), format!("read {}", self.path().display())).map(|outcome| outcome.bytes())
}
pub fn data_range(&self, offset: u64, length: usize) -> Result<Option<Vec<u8>>, Error> {
let outcome = platform::outcome(
self.handle.data_range(offset, length),
format!("read {}", self.path().display()),
)?;
Ok(outcome.has_bytes().then(|| outcome.bytes()))
}
}