#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/doc_assets/tvf.svg"))]
use bytes::Bytes;
use chrono::{NaiveDate, NaiveDateTime};
use std::borrow::Cow;
use std::fmt::Debug;
use thiserror::Error;
#[derive(Debug, Eq, Error, PartialOrd, PartialEq)]
pub enum TvfError {
#[error("The key `{0}` is not present in the Tvf")]
FieldNotFound(usize),
#[error("The type can't be retrieve from the Tvf")]
TypeMismatch,
#[error("The field can't be Converted. {0}")]
ConvertionError(String),
#[error("Serialization error: {0}")]
SerializationError(String),
}
pub trait Tvf {
fn is_empty(&self) -> bool;
fn len(&self) -> usize;
fn contains(&self, id: usize) -> bool;
fn remove(&mut self, id: usize);
fn into_keys(self) -> Vec<usize>;
fn keys(&self) -> Vec<usize>;
fn get_buffer(&self, id: usize) -> Result<Cow<'_, Self>, TvfError>
where
Self: Tvf + Clone;
fn get_unsigned(&self, id: usize) -> Result<u64, TvfError>;
fn get_signed(&self, id: usize) -> Result<i64, TvfError>;
fn get_byte(&self, id: usize) -> Result<u8, TvfError>;
fn get_float(&self, id: usize) -> Result<f64, TvfError>;
fn get_string(&self, id: usize) -> Result<Cow<'_, String>, TvfError>;
fn get_bytes(&self, id: usize) -> Result<Cow<'_, Bytes>, TvfError>;
fn get_date(&self, id: usize) -> Result<NaiveDate, TvfError>;
fn get_datetime(&self, id: usize) -> Result<NaiveDateTime, TvfError>;
fn put_buffer(&mut self, id: usize, buffer: Self)
where
Self: Tvf;
fn put_unsigned(&mut self, id: usize, unsigned: u64);
fn put_signed(&mut self, id: usize, signed: i64);
fn put_byte(&mut self, id: usize, byte: u8);
fn put_float(&mut self, id: usize, float: f64);
fn put_string<T: Into<String>>(&mut self, id: usize, string: T);
fn put_bytes(&mut self, id: usize, buffer: Bytes);
fn put_date(&mut self, id: usize, date: NaiveDate);
fn put_datetime(&mut self, id: usize, datetime: NaiveDateTime);
}
pub trait TvfFilter {
fn filter<T: Tvf>(tvf: T) -> T;
fn mask_tvf_str_field<T: Tvf>(mut tvf: T, id: usize, fill_char: &str) -> T {
if tvf.contains(id) {
if let Ok(str) = tvf.get_string(id) {
tvf.put_string(id, fill_char.repeat(str.len()));
} else {
tvf.remove(id);
}
}
tvf
}
}