diff options
author | stuebinm | 2022-01-25 23:07:23 +0100 |
---|---|---|
committer | stuebinm | 2022-01-25 23:07:23 +0100 |
commit | 130d8907335c93e48017c13e3202816d552683c9 (patch) | |
tree | d94809523a3e47d3eadf94d19cd26b22cdb3a0ca /src/travelynx.rs |
hacky proof of concept
Diffstat (limited to '')
-rw-r--r-- | src/travelynx.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/travelynx.rs b/src/travelynx.rs new file mode 100644 index 0000000..0188f32 --- /dev/null +++ b/src/travelynx.rs @@ -0,0 +1,64 @@ +use clap::Args; +use serde::{Serialize, Deserialize}; +use colored::*; + +use crate::types::Status; + +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Travel { + token: String, + #[serde(flatten)] + action: Action, +} + +#[derive(Serialize, Debug)] +#[serde(rename_all = "camelCase")] +#[serde(tag = "action")] +pub enum Action { + #[serde(rename = "checkin")] + #[serde(rename_all = "camelCase")] + CheckIn { + token: String, + train: TrainRef, + from_station: String, + #[serde(skip_serializing_if = "Option::is_none")] + to_station: Option<String>, + #[serde(skip_serializing_if = "Option::is_none")] + comment: Option<String>, + }, + #[serde(rename = "checkout")] + CheckOut { + to_station: String, + force: bool, + #[serde(skip_serializing_if = "Option::is_none")] + comment: Option<String>, + }, + Undo {token: String}, +} + +#[derive(Args, Serialize, Debug)] +pub struct TrainRef { + #[clap(name = "TRAIN TYPE")] + #[serde(rename = "type")] + pub _type: String, + #[clap(name = "NUMBER")] + pub no: String, +} + +#[derive(Deserialize, Debug)] +pub struct Response { + success: Option<bool>, + deprecated: bool, + status: Status, + error: Option<String> +} + +impl std::fmt::Display for Response { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match &self.error { + Some(msg) => write!(f, "{}", msg.red()), + None => write!(f, "{}\n\n{}", "Success!".green(), self.status) + } + } +} |