use clap::Args; use colored::*; use serde::{Deserialize, Serialize}; use crate::types::{Status, Train}; #[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, #[serde(skip_serializing_if = "Option::is_none")] comment: Option }, #[serde(rename = "checkout")] #[serde(rename_all = "camelCase")] CheckOut { to_station: String, force: bool, #[serde(skip_serializing_if = "Option::is_none")] comment: Option, token: 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 } impl std::fmt::Display for TrainRef { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{} {}", self._type, self.no) } } 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, deprecated: bool, status: Status, error: Option } 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) } } }