Refactoring mensa
This commit is contained in:
19
src/main.rs
19
src/main.rs
@@ -1,34 +1,33 @@
|
||||
use chrono::Local;
|
||||
use mensa::{Mealplan, Mensa, MensaName};
|
||||
use prettytable::{row, Cell, Row, Table};
|
||||
|
||||
mod cli;
|
||||
mod mensa;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
fn main() {
|
||||
let args = cli::get_args();
|
||||
exec_arguments(&args)?;
|
||||
Ok(())
|
||||
exec_arguments(&args);
|
||||
}
|
||||
|
||||
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);
|
||||
fn exec_arguments(args: &cli::Args) {
|
||||
let shedhalle = Mensa::from(MensaName::Shedhalle);
|
||||
let morgenstelle = Mensa::from(MensaName::Morgenstelle);
|
||||
|
||||
if args.morgenstelle {
|
||||
if let mensa::Mensa::Morgenstelle(resp) = morgenstelle? {
|
||||
if let Ok(resp) = morgenstelle {
|
||||
exec_arg_helper(args, &resp);
|
||||
}
|
||||
}
|
||||
|
||||
if args.shedhalle {
|
||||
if let mensa::Mensa::Shedhalle(resp) = shedhalle? {
|
||||
if let Ok(resp) = shedhalle {
|
||||
exec_arg_helper(args, &resp);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn exec_arg_helper(args: &cli::Args, m: &dyn mensa::Mealplan) {
|
||||
fn exec_arg_helper(args: &cli::Args, m: &impl Mealplan) {
|
||||
if let Some(menus) = m.nth(args.days, args.vegetarian) {
|
||||
if args.plaintext {
|
||||
for i in menus.iter() {
|
||||
|
124
src/mensa.rs
124
src/mensa.rs
@@ -1,9 +1,7 @@
|
||||
extern crate serde_derive;
|
||||
use std::time::Duration;
|
||||
|
||||
use chrono::{Datelike, Local};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::time::Duration;
|
||||
use ureq::{Agent, Error};
|
||||
|
||||
pub enum MensaName {
|
||||
@@ -18,47 +16,6 @@ pub trait Mealplan {
|
||||
fn nth(&self, days: u8, vegetarian: bool) -> Option<Vec<&Menu>>;
|
||||
}
|
||||
|
||||
pub enum Mensa {
|
||||
Shedhalle(MensaShedhalle),
|
||||
Morgenstelle(MensaMorgenstelle),
|
||||
}
|
||||
|
||||
impl Mensa {
|
||||
pub fn from(name: MensaName) -> Result<Mensa, Error> {
|
||||
let agent: Agent = ureq::AgentBuilder::new()
|
||||
.timeout_read(Duration::from_secs(5))
|
||||
.timeout_write(Duration::from_secs(5))
|
||||
.build();
|
||||
|
||||
match name {
|
||||
MensaName::Shedhalle => {
|
||||
let resp = agent
|
||||
.get("https://www.my-stuwe.de//wp-json/mealplans/v1/canteens/611?lang=de")
|
||||
.call()?
|
||||
.into_json::<MensaShedhalle>()?;
|
||||
//let resp = reqwest::blocking::get(
|
||||
// "https://www.my-stuwe.de//wp-json/mealplans/v1/canteens/611?lang=de",
|
||||
//)?
|
||||
//.json::<MensaShedhalle>()?;
|
||||
|
||||
Ok(Mensa::Shedhalle(resp))
|
||||
}
|
||||
MensaName::Morgenstelle => {
|
||||
//let resp = reqwest::blocking::get(
|
||||
// "https://www.my-stuwe.de//wp-json/mealplans/v1/canteens/621?lang=de",
|
||||
//)?
|
||||
//.json::<MensaMorgenstelle>()?;
|
||||
let resp = agent
|
||||
.get("https://www.my-stuwe.de//wp-json/mealplans/v1/canteens/621?lang=de")
|
||||
.call()?
|
||||
.into_json::<MensaMorgenstelle>()?;
|
||||
|
||||
Ok(Mensa::Morgenstelle(resp))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_nth_date(days: u8) -> Option<chrono::DateTime<Local>> {
|
||||
if days > 7 {
|
||||
return None;
|
||||
@@ -75,69 +32,36 @@ fn get_nth_date(days: u8) -> Option<chrono::DateTime<Local>> {
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct MensaShedhalle {
|
||||
pub struct Mensa {
|
||||
#[serde(alias = "621")]
|
||||
#[serde(rename = "611")]
|
||||
canteen: Canteen,
|
||||
}
|
||||
|
||||
impl MensaShedhalle {
|
||||
fn print(&self) {
|
||||
impl Mensa {
|
||||
pub fn from(name: MensaName) -> Result<Mensa, Error> {
|
||||
let agent: Agent = ureq::AgentBuilder::new()
|
||||
.timeout_read(Duration::from_secs(5))
|
||||
.timeout_write(Duration::from_secs(5))
|
||||
.build();
|
||||
|
||||
let canteen_id = match name {
|
||||
MensaName::Shedhalle => 611,
|
||||
MensaName::Morgenstelle => 621,
|
||||
};
|
||||
|
||||
let url =
|
||||
format!("https://www.my-stuwe.de//wp-json/mealplans/v1/canteens/{canteen_id}?lang=de");
|
||||
|
||||
Ok(agent.get(&url).call()?.into_json::<Mensa>()?)
|
||||
}
|
||||
|
||||
fn _print(&self) {
|
||||
println!("{:#?}", self);
|
||||
}
|
||||
}
|
||||
|
||||
impl Mealplan for MensaShedhalle {
|
||||
fn id(&self) -> &str {
|
||||
&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, vegetarian: bool) -> Option<Vec<&Menu>> {
|
||||
match get_nth_date(days) {
|
||||
Some(dt) => {
|
||||
let local = format!("{}", dt.format("%Y-%m-%d"));
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct MensaMorgenstelle {
|
||||
#[serde(rename = "621")]
|
||||
canteen: Canteen,
|
||||
}
|
||||
|
||||
impl Mealplan for MensaMorgenstelle {
|
||||
impl Mealplan for Mensa {
|
||||
fn id(&self) -> &str {
|
||||
&self.canteen.canteen_id
|
||||
}
|
||||
@@ -214,7 +138,7 @@ pub struct Menu {
|
||||
}
|
||||
|
||||
impl Menu {
|
||||
pub fn print(&self) {
|
||||
pub fn _print(&self) {
|
||||
println!("{:#?}", self);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user