use std::fmt::Debug;
use std::fmt::Display;
use std::hash::Hash;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use crate::DType;
use crate::ExtDType;
use crate::ExtID;
use crate::extension::ExtDTypeRef;
pub trait ExtDTypeVTable: 'static + Sized + Send + Sync + Clone + Debug + Eq + Hash {
type Metadata: 'static + Send + Sync + Clone + Debug + Display + Eq + Hash;
fn id(&self) -> ExtID;
fn serialize(&self, metadata: &Self::Metadata) -> VortexResult<Vec<u8>> {
_ = metadata;
vortex_bail!(
"Serialization not implemented for extension type {}",
self.id()
);
}
fn deserialize(&self, metadata: &[u8]) -> VortexResult<Self::Metadata> {
_ = metadata;
vortex_bail!(
"Deserialization not implemented for extension type {}",
self.id()
);
}
fn validate_dtype(&self, metadata: &Self::Metadata, storage_dtype: &DType) -> VortexResult<()>;
}
pub trait DynExtDTypeVTable: 'static + Send + Sync + Debug {
fn id(&self) -> ExtID;
fn deserialize(&self, data: &[u8], storage_dtype: DType) -> VortexResult<ExtDTypeRef>;
}
impl<V: ExtDTypeVTable> DynExtDTypeVTable for V {
fn id(&self) -> ExtID {
ExtDTypeVTable::id(self)
}
fn deserialize(&self, data: &[u8], storage_dtype: DType) -> VortexResult<ExtDTypeRef> {
let metadata = ExtDTypeVTable::deserialize(self, data)?;
Ok(ExtDType::try_with_vtable(self.clone(), metadata, storage_dtype)?.erased())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EmptyMetadata;
impl Display for EmptyMetadata {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "")
}
}