use crate::types::concept::{DataType, Type};
use crate::value::concept::ValueCell;
use crate::value::error::TypeResult;
use crate::value::sequence::Sequence;
pub type Tuple = Vec<Type>;
pub fn sum_size(record: &Tuple) -> usize {
fn_size(record, |a, b| a + b)
}
pub fn fn_size(record: &Tuple, func: fn(acc: usize, item: usize) -> usize) -> usize {
record.iter()
.map(|t| t.size())
.reduce(func).unwrap_or_default()
}
pub fn join(record: &Tuple, with: &str) -> String {
record.iter()
.map(|t| t.typename()).collect::<Vec<_>>()
.join(with).to_string()
}
impl DataType for Tuple {
fn size(&self) -> usize {
sum_size(self)
}
fn typename(&self) -> String {
format!("({})", join(self, ","))
}
fn construct_from_raw(&self, raw: &[u8]) -> TypeResult<ValueCell> {
Ok(Sequence::from(self.clone(), raw)?.to_cell())
}
}