From 4d40ae3977f94dae82392aaf838c472569c101a8 Mon Sep 17 00:00:00 2001 From: structix Date: Thu, 17 Nov 2022 22:44:47 +0100 Subject: [PATCH] Add vegetarian switch --- src/cli.rs | 4 ++++ src/main.rs | 14 +------------- src/mensa.rs | 18 +++++++++++++----- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 8baf49b..0b2f1b9 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -23,6 +23,10 @@ pub struct Args { /// Offset of days in the future (valid inputs 0-7) #[arg(short, long, default_value_t = 0)] pub days: u8, + + /// Show the vegetarian menu + #[arg(short, long, default_value_t = false)] + pub vegetarian: bool, } pub fn get_args() -> Args { diff --git a/src/main.rs b/src/main.rs index a9de113..493d2d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,18 +12,6 @@ mod cli; async fn main() -> Result<(), Box> { let args = cli::get_args(); exec_arguments(&args).await?; - //let shedhalle = mensa::Mensa::from(mensa::MensaName::Shedhalle); - //let morgenstelle = mensa::Mensa::from(mensa::MensaName::Morgenstelle); - //if let mensa::Mensa::Shedhalle(resp) = shedhalle.await? { - // let data = resp.today().iter().map(|&x| x.get_short_info()).collect::>(); - // table_short(data); - //} - - //if let mensa::Mensa::Morgenstelle(resp) = morgenstelle.await? { - // let data = resp.today().iter().map(|&x| x.get_short_info()).collect::>(); - // table_short(data); - //} - Ok(()) } @@ -46,7 +34,7 @@ async fn exec_arguments(args: &cli::Args) -> Result<(), Box &str; fn name(&self) -> &str; fn today(&self) -> Vec<&Menu>; - fn nth(&self, days: u8) -> Option>; + fn nth(&self, days: u8, vegetarian: bool) -> Option>; } pub enum Mensa { @@ -88,11 +88,15 @@ impl Mealplan for MensaShedhalle { self.canteen.menus.iter().filter(|&x| x.menu_date == local).collect() } - fn nth(&self, days: u8) -> Option> { + fn nth(&self, days: u8, vegetarian: bool) -> Option> { match get_nth_date(days) { Some(dt) => { let local = format!("{}", dt.format("%Y-%m-%d")); - Some(self.canteen.menus.iter().filter(|&x| x.menu_date == local).collect()) + if vegetarian { + Some(self.canteen.menus.iter().filter(|&x| x.menu_date == local && x.menu_line.contains("veg")).collect()) + } else { + Some(self.canteen.menus.iter().filter(|&x| x.menu_date == local).collect()) + } }, _ => None } @@ -122,11 +126,15 @@ impl Mealplan for MensaMorgenstelle { self.canteen.menus.iter().filter(|&x| x.menu_date == local).collect() } - fn nth(&self, days: u8) -> Option> { + fn nth(&self, days: u8, vegetarian: bool) -> Option> { match get_nth_date(days) { Some(dt) => { let local = format!("{}", dt.format("%Y-%m-%d")); - Some(self.canteen.menus.iter().filter(|&x| x.menu_date == local).collect()) + if vegetarian { + Some(self.canteen.menus.iter().filter(|&x| x.menu_date == local && x.menu_line.contains("veg")).collect()) + } else { + Some(self.canteen.menus.iter().filter(|&x| x.menu_date == local).collect()) + } }, _ => None }