#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ColumnKind {
#[default]
Any,
Text,
Integer,
Number,
Decimal,
Bool,
Date,
DateTime,
Image,
}
impl ColumnKind {
pub fn is_image(self) -> bool {
matches!(self, ColumnKind::Image)
}
pub fn is_numeric(self) -> bool {
matches!(
self,
ColumnKind::Integer | ColumnKind::Number | ColumnKind::Decimal
)
}
pub fn is_temporal(self) -> bool {
matches!(self, ColumnKind::Date | ColumnKind::DateTime)
}
pub fn as_str(self) -> &'static str {
match self {
ColumnKind::Any => "any",
ColumnKind::Text => "text",
ColumnKind::Integer => "integer",
ColumnKind::Number => "number",
ColumnKind::Decimal => "decimal",
ColumnKind::Bool => "bool",
ColumnKind::Date => "date",
ColumnKind::DateTime => "datetime",
ColumnKind::Image => "image",
}
}
}
#[derive(Debug, Clone)]
pub struct ColumnDef {
pub header: String,
pub aliases: Vec<String>,
pub required: bool,
pub kind: ColumnKind,
pub width: Option<f64>,
pub format: Option<String>,
pub text_format: bool,
pub image: bool,
pub ignore: bool,
pub default: Option<String>,
pub note: Option<String>,
pub dropdown: Vec<String>,
}
impl ColumnDef {
pub fn new(header: impl Into<String>) -> Self {
Self {
header: header.into(),
aliases: Vec::new(),
required: false,
kind: ColumnKind::Any,
width: None,
format: None,
text_format: false,
image: false,
ignore: false,
default: None,
note: None,
dropdown: Vec::new(),
}
}
pub fn alias(mut self, alias: impl Into<String>) -> Self {
let a = alias.into();
if !a.is_empty() && a != self.header {
self.aliases.push(a);
}
self
}
pub fn aliases<I, S>(mut self, list: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
for a in list {
self = self.alias(a);
}
self
}
pub fn required(mut self) -> Self {
self.required = true;
self
}
pub fn optional(mut self) -> Self {
self.required = false;
self
}
pub fn kind(mut self, kind: ColumnKind) -> Self {
self.kind = kind;
if kind.is_image() {
self.image = true;
}
self
}
pub fn width(mut self, width: f64) -> Self {
self.width = Some(width);
self
}
pub fn format(mut self, format: impl Into<String>) -> Self {
self.format = Some(format.into());
self
}
pub fn as_text(mut self) -> Self {
self.text_format = true;
self
}
pub fn image(mut self) -> Self {
self.image = true;
self.kind = ColumnKind::Image;
self
}
pub fn ignore(mut self) -> Self {
self.ignore = true;
self
}
pub fn default_value(mut self, value: impl Into<String>) -> Self {
self.default = Some(value.into());
self
}
pub fn note(mut self, note: impl Into<String>) -> Self {
self.note = Some(note.into());
self
}
pub fn dropdown<I, S>(mut self, list: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.dropdown = list.into_iter().map(Into::into).collect();
self
}
pub fn names(&self) -> impl Iterator<Item = &str> {
std::iter::once(self.header.as_str()).chain(self.aliases.iter().map(|s| s.as_str()))
}
pub fn is_importable(&self) -> bool {
!self.ignore
}
pub fn mark_required(&self) -> bool {
self.required
}
}
impl From<&str> for ColumnDef {
fn from(header: &str) -> Self {
ColumnDef::new(header)
}
}
impl From<String> for ColumnDef {
fn from(header: String) -> Self {
ColumnDef::new(header)
}
}
impl From<(&str, ColumnKind)> for ColumnDef {
fn from((header, kind): (&str, ColumnKind)) -> Self {
ColumnDef::new(header).kind(kind)
}
}