Add cli arguments
This commit is contained in:
30
src/cli.rs
Normal file
30
src/cli.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use clap::Parser;
|
||||
|
||||
/// tuemensa is a simple cli tool to retrieve the current meal plan.
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
pub struct Args {
|
||||
/// Show Mensa Morgenstelle
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
pub morgenstelle: bool,
|
||||
|
||||
/// Show Mensa Shedhalle
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
pub shedhalle: bool,
|
||||
|
||||
/// Format as plain text
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
pub plaintext: bool,
|
||||
|
||||
/// Use very short format (oneline)
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
pub oneline: bool,
|
||||
|
||||
/// Offset of days in the future (valid inputs 0-7)
|
||||
#[arg(short, long, default_value_t = 0)]
|
||||
pub days: u8,
|
||||
}
|
||||
|
||||
pub fn get_args() -> Args {
|
||||
Args::parse()
|
||||
}
|
64
src/main.rs
64
src/main.rs
@@ -4,27 +4,68 @@ use comfy_table::presets::UTF8_FULL;
|
||||
use comfy_table::modifiers::UTF8_ROUND_CORNERS;
|
||||
|
||||
mod mensa;
|
||||
mod cli;
|
||||
|
||||
//use crate::mensa::*;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
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::<Vec<(&str, String, &str)>>();
|
||||
table_short(data).await;
|
||||
}
|
||||
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::<Vec<(&str, String, &str)>>();
|
||||
// table_short(data);
|
||||
//}
|
||||
|
||||
if let mensa::Mensa::Morgenstelle(resp) = morgenstelle.await? {
|
||||
let data = resp.today().iter().map(|&x| x.get_short_info()).collect::<Vec<(&str, String, &str)>>();
|
||||
table_short(data).await;
|
||||
}
|
||||
//if let mensa::Mensa::Morgenstelle(resp) = morgenstelle.await? {
|
||||
// let data = resp.today().iter().map(|&x| x.get_short_info()).collect::<Vec<(&str, String, &str)>>();
|
||||
// table_short(data);
|
||||
//}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn table_short(data: Vec<(&str, String, &str)>) {
|
||||
async fn exec_arguments(args: &cli::Args) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let shedhalle = mensa::Mensa::from(mensa::MensaName::Shedhalle);
|
||||
let morgenstelle = mensa::Mensa::from(mensa::MensaName::Morgenstelle);
|
||||
|
||||
if args.morgenstelle {
|
||||
if let mensa::Mensa::Morgenstelle(resp) = morgenstelle.await? {
|
||||
exec_arg_helper(args, &resp);
|
||||
}
|
||||
}
|
||||
|
||||
if args.shedhalle {
|
||||
if let mensa::Mensa::Shedhalle(resp) = shedhalle.await? {
|
||||
exec_arg_helper(args, &resp);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn exec_arg_helper(args: &cli::Args, m: &dyn mensa::Mealplan) {
|
||||
if let Some(menus) = m.nth(args.days) {
|
||||
if args.plaintext {
|
||||
for i in menus.iter() {
|
||||
i.print_short_info();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if args.oneline {
|
||||
menus.first().unwrap().print_very_short_info();
|
||||
return;
|
||||
}
|
||||
|
||||
// Default case --> print fancy
|
||||
println!("{}", m.name());
|
||||
table_short(menus.iter().map(|&x| x.get_short_info()).collect::<Vec<(&str, String, &str)>>());
|
||||
}
|
||||
}
|
||||
|
||||
fn table_short(data: Vec<(&str, String, &str)>) {
|
||||
let mut table = Table::new();
|
||||
|
||||
//let mut data_cells = data.iter().map(|x| vec![Cell::new(x.0), Cell::new(&x.1), Cell::new(x.2)]).collect::<Vec<Vec<Cell>>>();
|
||||
@@ -46,4 +87,3 @@ async fn table_short(data: Vec<(&str, String, &str)>) {
|
||||
println!("{table}");
|
||||
}
|
||||
|
||||
|
||||
|
49
src/mensa.rs
49
src/mensa.rs
@@ -1,5 +1,5 @@
|
||||
extern crate serde_derive;
|
||||
use chrono::{DateTime, Local};
|
||||
use chrono::{Local, Datelike};
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
@@ -11,7 +11,9 @@ pub enum MensaName {
|
||||
|
||||
pub trait Mealplan {
|
||||
fn id(&self) -> &str;
|
||||
fn name(&self) -> &str;
|
||||
fn today(&self) -> Vec<&Menu>;
|
||||
fn nth(&self, days: u8) -> Option<Vec<&Menu>>;
|
||||
}
|
||||
|
||||
pub enum Mensa {
|
||||
@@ -43,6 +45,23 @@ impl Mensa {
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn get_nth_date(days: u8) -> Option<chrono::DateTime<Local>> {
|
||||
if days > 7 {
|
||||
return None;
|
||||
}
|
||||
|
||||
if let Some(dt) = Local::now().checked_add_days(chrono::Days::new(days as u64)) {
|
||||
return match dt.weekday() {
|
||||
chrono::Weekday::Sat => dt.checked_add_days(chrono::Days::new(2)),
|
||||
chrono::Weekday::Sun => dt.checked_add_days(chrono::Days::new(1)),
|
||||
_ => Some(dt)
|
||||
};
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct MensaShedhalle {
|
||||
#[serde(rename = "611")]
|
||||
@@ -60,10 +79,24 @@ impl Mealplan for MensaShedhalle {
|
||||
&self.canteen.canteen_id
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
&&self.canteen.canteen
|
||||
}
|
||||
|
||||
fn today(&self) -> Vec<&Menu> {
|
||||
let local = format!("{}", Local::now().format("%Y-%m-%d"));
|
||||
self.canteen.menus.iter().filter(|&x| x.menu_date == local).collect()
|
||||
}
|
||||
|
||||
fn nth(&self, days: u8) -> Option<Vec<&Menu>> {
|
||||
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())
|
||||
},
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -80,10 +113,24 @@ impl Mealplan for MensaMorgenstelle {
|
||||
&self.canteen.canteen_id
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
&&self.canteen.canteen
|
||||
}
|
||||
|
||||
fn today(&self) -> Vec<&Menu> {
|
||||
let local = format!("{}", Local::now().format("%Y-%m-%d"));
|
||||
self.canteen.menus.iter().filter(|&x| x.menu_date == local).collect()
|
||||
}
|
||||
|
||||
fn nth(&self, days: u8) -> Option<Vec<&Menu>> {
|
||||
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())
|
||||
},
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user