use std::path::Path;
use rspack_cacheable::{
__private::rkyv::{Archive, Deserialize, Serialize, bytecheck::CheckBytes},
Deserializer, Serializer, Validator, from_bytes, to_bytes,
};
use rspack_error::Result;
use rspack_paths::Utf8PathBuf;
#[derive(Debug, Clone)]
struct Context {
project_path: Option<Utf8PathBuf>,
}
impl rspack_cacheable::CacheableContext for Context {
fn project_root(&self) -> Option<&Path> {
self.project_path.as_ref().map(|p| p.as_std_path())
}
}
#[derive(Debug, Clone)]
pub struct CacheCodec {
context: Context,
}
impl CacheCodec {
pub fn new(project_path: Option<Utf8PathBuf>) -> Self {
Self {
context: Context { project_path },
}
}
pub fn encode<T>(&self, data: &T) -> Result<Vec<u8>>
where
T: for<'a> Serialize<Serializer<'a>>,
{
to_bytes(data, &self.context).map_err(|e| rspack_error::error!(e.to_string()))
}
pub fn decode<T>(&self, bytes: &[u8]) -> Result<T>
where
T: Archive,
T::Archived: for<'a> CheckBytes<Validator<'a>> + Deserialize<T, Deserializer>,
{
from_bytes(bytes, &self.context).map_err(|e| rspack_error::error!(e.to_string()))
}
}