#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use super::{ArrowDataType, Metadata};
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Field {
pub name: String,
pub data_type: ArrowDataType,
pub is_nullable: bool,
pub metadata: Metadata,
}
impl Field {
pub fn new<T: Into<String>>(name: T, data_type: ArrowDataType, is_nullable: bool) -> Self {
Field {
name: name.into(),
data_type,
is_nullable,
metadata: Default::default(),
}
}
#[inline]
pub fn with_metadata(self, metadata: Metadata) -> Self {
Self {
name: self.name,
data_type: self.data_type,
is_nullable: self.is_nullable,
metadata,
}
}
#[inline]
pub fn data_type(&self) -> &ArrowDataType {
&self.data_type
}
}
#[cfg(feature = "arrow_rs")]
impl From<Field> for arrow_schema::Field {
fn from(value: Field) -> Self {
Self::new(value.name, value.data_type.into(), value.is_nullable)
.with_metadata(value.metadata.into_iter().collect())
}
}
#[cfg(feature = "arrow_rs")]
impl From<arrow_schema::Field> for Field {
fn from(value: arrow_schema::Field) -> Self {
(&value).into()
}
}
#[cfg(feature = "arrow_rs")]
impl From<&arrow_schema::Field> for Field {
fn from(value: &arrow_schema::Field) -> Self {
let data_type = value.data_type().clone().into();
let metadata = value
.metadata()
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
Self::new(value.name(), data_type, value.is_nullable()).with_metadata(metadata)
}
}
#[cfg(feature = "arrow_rs")]
impl From<arrow_schema::FieldRef> for Field {
fn from(value: arrow_schema::FieldRef) -> Self {
value.as_ref().into()
}
}
#[cfg(feature = "arrow_rs")]
impl From<&arrow_schema::FieldRef> for Field {
fn from(value: &arrow_schema::FieldRef) -> Self {
value.as_ref().into()
}
}