jobber 1.1.7-alpha

Minimalistic console work time tracker
//! Entity console views

pub(crate) use bill_view::*;
pub(crate) use calendar_view::*;
pub(crate) use client_view::*;
pub(crate) use company_view::*;
pub(crate) use info_view::*;
pub(crate) use invoice_view::*;
pub(crate) use job_view::*;
pub(crate) use tag_view::*;
pub(crate) use work_view::*;
pub(crate) use worker_view::*;

use serde::{Deserialize, Serialize};
use std::str::FromStr;
use tabled::{
    Table,
    settings::{Style, Theme},
};

mod bill_view;
mod calendar_view;
mod client_view;
mod company_view;
mod info_view;
mod invoice_view;
mod job_view;
mod tag_view;
mod work_view;
mod worker_view;

#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
pub(crate) enum TableStyle {
    #[serde(rename = "empty")]
    Empty,
    #[serde(rename = "markdown")]
    Markdown,
    #[default]
    #[serde(rename = "rounded")]
    Rounded,
}

impl TableStyle {
    fn table_theme(&self) -> Theme {
        match self {
            TableStyle::Empty => Theme::from_style(Style::empty()),
            TableStyle::Markdown => Theme::from_style(Style::markdown()),
            TableStyle::Rounded => Theme::from_style(Style::rounded()),
        }
    }
}

impl std::fmt::Display for TableStyle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TableStyle::Empty => write!(f, "empty"),
            TableStyle::Markdown => write!(f, "markdown"),
            TableStyle::Rounded => write!(f, "rounded"),
        }
    }
}

pub(crate) trait ApplyTableStyle<'a> {
    fn style(&'a mut self, style: &TableStyle) -> &'a mut Self;
}

impl<'a> ApplyTableStyle<'a> for Table {
    fn style(&'a mut self, style: &TableStyle) -> &'a mut Self {
        self.with(style.table_theme())
    }
}

impl FromStr for TableStyle {
    type Err = crate::commands::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s.to_lowercase().as_str() {
            "rounded" => Self::Rounded,
            "markdown" => Self::Markdown,
            _ => Self::Empty,
        })
    }
}