summaryrefslogtreecommitdiff
path: root/src/traits.rs
blob: a24a689633a1c5f893f07cf8619a89c1b7a1f7ba (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
use chrono::{DateTime, Local, Utc};
use colored::Colorize;

pub trait IsStation {
  fn name(&self) -> &str;
  fn scheduled_arrival(&self) -> Option<&DateTime<Utc>>;
  fn real_arrival(&self) -> Option<&DateTime<Utc>>;
  fn ds100(&self) -> &str;

  fn to_fancy_string(&self) -> String {
    format!(
      "{} {} – {} ({})",
      self
        .real_arrival() // chrono's API for timezones is expressive, but reads like c++ …
        .map(|t| <DateTime<Local>>::from(*t).time().to_string())
        .unwrap_or("??:??:??".to_string())
        .blue(),
      {
        let delay = match (self.real_arrival(), self.scheduled_arrival()) {
          (Some(a), Some(s)) => (a.time() - s.time()).num_minutes(),
          _ => 0
        };
        let text = format!("({:+})", delay);
        if delay > 0 {
          text.red()
        } else {
          text.green()
        }
      },
      self.ds100().red(),
      self.name()
    )
  }
}