use std::sync::Arc;
use polars_utils::pl_str::PlSmallStr;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use super::{ArrowDataType, Metadata};
pub static DTYPE_ENUM_VALUES: &str = "_PL_ENUM_VALUES";
pub static DTYPE_CATEGORICAL: &str = "_PL_CATEGORICAL";
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Field {
pub name: PlSmallStr,
pub dtype: ArrowDataType,
pub is_nullable: bool,
pub metadata: Option<Arc<Metadata>>,
}
impl From<Field> for (PlSmallStr, Field) {
fn from(value: Field) -> Self {
(value.name.clone(), value)
}
}
impl Field {
pub fn new(name: PlSmallStr, dtype: ArrowDataType, is_nullable: bool) -> Self {
Field {
name,
dtype,
is_nullable,
metadata: Default::default(),
}
}
#[inline]
pub fn with_metadata(self, metadata: Metadata) -> Self {
if metadata.is_empty() {
return self;
}
Self {
name: self.name,
dtype: self.dtype,
is_nullable: self.is_nullable,
metadata: Some(Arc::new(metadata)),
}
}
#[inline]
pub fn dtype(&self) -> &ArrowDataType {
&self.dtype
}
pub fn is_enum(&self) -> bool {
if let Some(md) = &self.metadata {
md.get(DTYPE_ENUM_VALUES).is_some()
} else {
false
}
}
pub fn is_categorical(&self) -> bool {
if let Some(md) = &self.metadata {
md.get(DTYPE_CATEGORICAL).is_some()
} else {
false
}
}
}