use serde::{Deserialize, Serialize};
use crate::arrays::datatype::DataType;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Field {
pub name: String,
pub datatype: DataType,
pub nullable: bool,
}
impl Field {
pub fn new(name: impl Into<String>, datatype: DataType, nullable: bool) -> Self {
Field {
name: name.into(),
datatype,
nullable,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ColumnSchema {
pub fields: Vec<Field>,
}
impl ColumnSchema {
pub const fn empty() -> Self {
ColumnSchema { fields: Vec::new() }
}
pub fn new(fields: impl IntoIterator<Item = Field>) -> Self {
ColumnSchema {
fields: fields.into_iter().collect(),
}
}
pub fn merge(self, other: ColumnSchema) -> Self {
ColumnSchema {
fields: self.fields.into_iter().chain(other.fields).collect(),
}
}
pub fn iter(&self) -> impl Iterator<Item = &Field> {
self.fields.iter()
}
pub fn type_schema(&self) -> TypeSchema {
TypeSchema {
types: self
.fields
.iter()
.map(|field| field.datatype.clone())
.collect(),
}
}
pub fn into_type_schema(self) -> TypeSchema {
TypeSchema {
types: self
.fields
.into_iter()
.map(|field| field.datatype)
.collect(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TypeSchema {
pub types: Vec<DataType>,
}
impl TypeSchema {
pub const fn empty() -> Self {
TypeSchema { types: Vec::new() }
}
pub fn new(types: impl IntoIterator<Item = DataType>) -> Self {
TypeSchema {
types: types.into_iter().collect(),
}
}
pub fn merge(self, other: TypeSchema) -> Self {
TypeSchema {
types: self.types.into_iter().chain(other.types).collect(),
}
}
}