summaryrefslogtreecommitdiff
path: root/src/traits.rs
diff options
context:
space:
mode:
authorstuebinm2022-02-09 02:51:59 +0100
committerstuebinm2022-02-09 02:51:59 +0100
commitcd13c85c69cf761b2da84ad91af64d23a3568aa5 (patch)
tree4cbdbf6f348508ad7f70aa11450dccc8148ae0cb /src/traits.rs
parentcf88935b5245daea51d2b513709b61a0e43483d6 (diff)
existential types in rust are weird
… lots and lots of traits …
Diffstat (limited to 'src/traits.rs')
-rw-r--r--src/traits.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/traits.rs b/src/traits.rs
new file mode 100644
index 0000000..a24a689
--- /dev/null
+++ b/src/traits.rs
@@ -0,0 +1,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()
+ )
+ }
+}