From 0de9008a3df42d171f01f0d431861c9d765b5234 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Sat, 26 Feb 2022 13:28:15 +0100 Subject: from / to commands for travelynx --- src/main.rs | 35 +++++++++++++++++++++++++++++++++-- src/travelynx.rs | 11 ++++++++++- src/types.rs | 14 +++++++------- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9bb2f9e..f41d871 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,9 +32,14 @@ enum Command { train: TrainRef }, /// (If already checked in) change the trip's destination - Destination { + To { to: String }, + /// (If already checked in) change the trip's starting point, + /// Warning: may lead to doubled entries in your trip history! + From { + from: String + }, Station { station: String }, @@ -146,7 +151,33 @@ fn main() -> Result<(), ureq::Error> { println!("{}: {}", traveltext, resp); } - Command::Destination { to } => { + Command::From { from } => { + println!("{}: {}", traveltext, "warning: this command may pollute your trip history".red()); + let status: Status = exiting_get_request( + &format!("{}/api/v1/status/{}", cli.baseurl, config.token_status), + cli.debug + ); + match status.train { + Some(train) => { + let resp: Response = exiting_post_request( + &format!("{}/api/v1/travel", cli.baseurl), + Action::CheckIn { + train: train.into(), + to_station: status.to_station.map(|s| s.ds100), + token: config.token_travel, + comment: None, + from_station: from + }, + cli.debug + ); + println!("{}: {}", traveltext, resp); + } + None => { + println!("{}: {}", traveltext, "not checked in?".red()) + } + } + } + Command::To { to } => { let resp: Response = exiting_post_request( &format!("{}/api/v1/travel", cli.baseurl), Action::CheckOut { diff --git a/src/travelynx.rs b/src/travelynx.rs index c5351eb..c3318ab 100644 --- a/src/travelynx.rs +++ b/src/travelynx.rs @@ -2,7 +2,7 @@ use clap::Args; use colored::*; use serde::{Deserialize, Serialize}; -use crate::types::Status; +use crate::types::{Status, Train}; #[derive(Serialize)] #[serde(rename_all = "camelCase")] @@ -56,6 +56,15 @@ impl std::fmt::Display for TrainRef { } } +impl std::convert::From for TrainRef { + fn from(from: Train) -> Self { + TrainRef { + _type: from._type, + no: from.no + } + } +} + #[derive(Deserialize, Debug)] pub struct Response { success: Option, diff --git a/src/types.rs b/src/types.rs index 5afbad2..257f857 100644 --- a/src/types.rs +++ b/src/types.rs @@ -10,7 +10,7 @@ use crate::traits::IsStation; #[serde(rename_all = "camelCase")] pub struct Station { name: String, - ds100: String, + pub ds100: String, uic: u64, latitude: f64, longitude: f64, @@ -81,10 +81,10 @@ impl IsStation for Stop { #[serde(rename_all = "camelCase")] pub struct Train { #[serde(rename = "type")] - _type: String, - line: Option, - no: String, - id: String + pub _type: String, + pub line: Option, + pub no: String, + pub id: String } impl std::fmt::Display for Train { @@ -98,11 +98,11 @@ impl std::fmt::Display for Train { pub struct Status { deprecated: bool, pub checked_in: bool, - from_station: Station, + pub from_station: Station, #[serde(deserialize_with = "parse_optional_station")] pub to_station: Option, intermediate_stops: Vec, - train: Option, + pub train: Option, action_time: u64 } -- cgit v1.2.3