summaryrefslogtreecommitdiff
path: root/src/travelynx.rs
blob: c5351ebd44ae8d741c3848b74a33325de0cac592 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use clap::Args;
use colored::*;
use serde::{Deserialize, Serialize};

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")]
  #[serde(rename_all = "camelCase")]
  CheckOut {
    to_station: String,
    force: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    comment: Option<String>,
    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)
  }
}

#[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)
    }
  }
}