mod bool8;
pub use bool8::Bool8;
mod fixed_shape_tensor;
pub use fixed_shape_tensor::{FixedShapeTensor, FixedShapeTensorMetadata};
mod json;
pub use json::{Json, JsonMetadata};
mod opaque;
pub use opaque::{Opaque, OpaqueMetadata};
mod timestamp_with_offset;
pub use timestamp_with_offset::TimestampWithOffset;
mod uuid;
pub use uuid::Uuid;
mod variable_shape_tensor;
pub use variable_shape_tensor::{VariableShapeTensor, VariableShapeTensorMetadata};
use crate::{ArrowError, Field};
use super::ExtensionType;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum CanonicalExtensionType {
FixedShapeTensor(FixedShapeTensor),
VariableShapeTensor(VariableShapeTensor),
Json(Json),
Uuid(Uuid),
Opaque(Opaque),
Bool8(Bool8),
TimestampWithOffset(TimestampWithOffset),
}
impl TryFrom<&Field> for CanonicalExtensionType {
type Error = ArrowError;
fn try_from(value: &Field) -> Result<Self, Self::Error> {
match value.extension_type_name() {
Some(name) if name.starts_with("arrow.") => match name {
FixedShapeTensor::NAME => value
.try_extension_type::<FixedShapeTensor>()
.map(Into::into),
VariableShapeTensor::NAME => value
.try_extension_type::<VariableShapeTensor>()
.map(Into::into),
Json::NAME => value.try_extension_type::<Json>().map(Into::into),
Uuid::NAME => value.try_extension_type::<Uuid>().map(Into::into),
Opaque::NAME => value.try_extension_type::<Opaque>().map(Into::into),
Bool8::NAME => value.try_extension_type::<Bool8>().map(Into::into),
TimestampWithOffset::NAME => value
.try_extension_type::<TimestampWithOffset>()
.map(Into::into),
_ => Err(ArrowError::InvalidArgumentError(format!(
"Unsupported canonical extension type: {name}"
))),
},
Some(name) => Err(ArrowError::InvalidArgumentError(format!(
"Field extension type name mismatch, expected a name with an `arrow.` prefix, found {name}"
))),
None => Err(ArrowError::InvalidArgumentError(
"Field extension type name missing".to_owned(),
)),
}
}
}
impl From<FixedShapeTensor> for CanonicalExtensionType {
fn from(value: FixedShapeTensor) -> Self {
CanonicalExtensionType::FixedShapeTensor(value)
}
}
impl From<VariableShapeTensor> for CanonicalExtensionType {
fn from(value: VariableShapeTensor) -> Self {
CanonicalExtensionType::VariableShapeTensor(value)
}
}
impl From<Json> for CanonicalExtensionType {
fn from(value: Json) -> Self {
CanonicalExtensionType::Json(value)
}
}
impl From<Uuid> for CanonicalExtensionType {
fn from(value: Uuid) -> Self {
CanonicalExtensionType::Uuid(value)
}
}
impl From<Opaque> for CanonicalExtensionType {
fn from(value: Opaque) -> Self {
CanonicalExtensionType::Opaque(value)
}
}
impl From<Bool8> for CanonicalExtensionType {
fn from(value: Bool8) -> Self {
CanonicalExtensionType::Bool8(value)
}
}
impl From<TimestampWithOffset> for CanonicalExtensionType {
fn from(value: TimestampWithOffset) -> Self {
CanonicalExtensionType::TimestampWithOffset(value)
}
}