use yew::Html;
use serde::{Serialize};
use serde_value::Value;
use std::fmt;
use crate::error::*;
pub trait TableData: 'static + Default + Clone + Ord + Serialize {
fn get_field_as_html(&self, field_name: &str) -> Result<Html<Table<Self>>>;
fn get_field_as_value(&self, field_name: &str) -> Result<Value>;
}
#[derive(Clone, PartialEq, Default, Debug)]
pub struct Column {
pub name: String,
pub short_name: Option<String>,
pub data_property: Option<String>,
}
impl fmt::Display for Column {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.short_name.as_ref().unwrap_or(&self.name))
}
}
#[derive(Clone, PartialEq)]
pub struct TableOptions {
pub orderable: bool,
}
#[derive(Copy, Clone, PartialEq)]
pub enum TableOrder {
Unordered = 0,
Ascending,
Descending,
}
impl Default for TableOrder {
fn default() -> Self { TableOrder::Unordered }
}
impl TableOrder {
pub fn rotate(&self) -> Self {
use TableOrder::*;
match *self {
Unordered => Ascending,
Ascending => Descending,
Descending => Unordered,
}
}
}
#[derive(Clone, PartialEq, Default)]
pub struct TableState {
pub order: Vec<TableOrder>,
}
#[derive(Clone, PartialEq, Default)]
pub struct Table<T> where T: TableData {
pub (crate) columns: Vec<Column>,
pub (crate) data: Vec<T>,
pub (crate) options: Option<TableOptions>,
pub (crate) state: TableState,
}
impl<T> Table<T> where T: TableData {
pub fn is_orderable(&self) -> bool {
if let Some(options) = &self.options {
options.orderable
} else {
false
}
}
}