Skip to content

Commit

Permalink
Update CLI Tabled Usage
Browse files Browse the repository at this point in the history
There where a few cases where the 'Tabled' Trait was implemented by hand where it could be derived
  • Loading branch information
isiko committed Jul 3, 2024
1 parent 2dabb57 commit 8659a7a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 54 deletions.
8 changes: 6 additions & 2 deletions cli-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,12 @@ async fn main() {
}
let shopping_tours = shopping_tours.unwrap();

let table = Table::new(shopping_tours).with(table_config).to_string();
println!("{}", table);
if shopping_tours.len() == 0 {
println!("No Shopping Tours planned");
} else {
let table = (Table::new(shopping_tours).with(table_config)).to_string();
println!("{}", table);
}
}
ListType::SourceOverrides(override_data) => {
let event = food_base
Expand Down
38 changes: 8 additions & 30 deletions foodlib/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,24 @@ use tabled::Tabled;

use crate::{recipes::EventRecipeIngredient, FoodBase, ShoppingListItem};

#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Tabled)]
pub struct Event {
#[tabled(rename = "ID")]
pub event_id: i32,
#[tabled(rename = "Name")]
pub event_name: String,
#[tabled(rename = "Comment", display_with = "crate::util::display_optional")]
pub comment: Option<String>,
#[serde(
serialize_with = "crate::util::serialize_optional_money",
deserialize_with = "crate::util::deserialize_optional_money"
)]
#[tabled(
rename = "Budget",
display_with = "crate::util::display_optional_money"
)]
pub budget: Option<PgMoney>,
}
impl Display for Event {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.event_name.as_str())
}
}

impl Tabled for Event {
const LENGTH: usize = 4;
fn headers() -> Vec<Cow<'static, str>> {
vec![
"ID".into(),
"Name".into(),
"Comment".into(),
"Budget".into(),
]
}

fn fields(&self) -> Vec<Cow<'_, str>> {
vec![
self.event_id.to_string().into(),
self.event_name.clone().into(),
self.comment.clone().unwrap_or_default().into(),
self.budget
.map(crate::util::format_pg_money)
.unwrap_or_default()
.into(),
]
}
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Tabled)]
pub struct Place {
Expand Down
27 changes: 5 additions & 22 deletions foodlib/src/ingredients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,35 +125,18 @@ impl std::string::ToString for Unit {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Tabled)]
pub struct ShoppingListItem {
#[tabled(rename = "ID")]
pub ingredient_id: i32,
#[tabled(rename = "Name")]
pub ingredient_name: String,
#[tabled(rename = "Price", display_with = "crate::util::format_pg_money")]
pub price: PgMoney,
#[tabled(rename = "Weight")]
pub weight: BigDecimal,
}

impl Tabled for ShoppingListItem {
const LENGTH: usize = 4;
fn headers() -> Vec<Cow<'static, str>> {
vec![
"ID".into(),
"Ingredient".into(),
"Price".into(),
"Weight".into(),
]
}

fn fields(&self) -> Vec<Cow<'_, str>> {
vec![
self.ingredient_id.to_string().into(),
self.ingredient_name.clone().into(),
crate::util::format_pg_money(self.price).into(),
self.weight.to_string().into(),
]
}
}

#[derive(Clone)]
pub struct IngredientSource {
pub ingredient_source_id: i32,
Expand Down
7 changes: 7 additions & 0 deletions foodlib/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ pub(crate) fn display_optional<T: Display>(value: &Option<T>) -> String {
}
}

pub(crate) fn display_optional_money(value: &Option<PgMoney>) -> String {
match value {
Some(value) => crate::util::format_pg_money(value),
None => "".to_string(),
}
}

pub(crate) fn format_pg_money(money: impl Borrow<PgMoney>) -> String {
format!("{} €", money.borrow().to_bigdecimal(2))
}

0 comments on commit 8659a7a

Please sign in to comment.