livesplit_core/timing/formatter/
none_wrapper.rs1use super::{TimeFormatter, DASH};
6use crate::TimeSpan;
7use core::fmt::{Display, Formatter, Result};
8
9pub struct Inner<'a, F, S> {
11 time: Option<TimeSpan>,
12 wrapper: &'a NoneWrapper<F, S>,
13}
14
15pub struct NoneWrapper<F, S>(F, S);
19
20pub struct DashWrapper;
23
24pub struct EmptyWrapper;
27
28impl<'a, F: 'a + TimeFormatter<'a>, S: AsRef<str>> NoneWrapper<F, S> {
29 pub const fn new(inner: F, none_text: S) -> Self {
33 NoneWrapper(inner, none_text)
34 }
35}
36
37impl DashWrapper {
38 pub const fn new<'a, F: 'a + TimeFormatter<'a>>(inner: F) -> NoneWrapper<F, &'static str> {
40 NoneWrapper::new(inner, DASH)
41 }
42}
43
44impl EmptyWrapper {
45 pub const fn new<'a, F: 'a + TimeFormatter<'a>>(inner: F) -> NoneWrapper<F, &'static str> {
47 NoneWrapper::new(inner, "")
48 }
49}
50
51impl<'a, F: 'a + TimeFormatter<'a>, S: 'a + AsRef<str>> TimeFormatter<'a> for NoneWrapper<F, S> {
52 type Inner = Inner<'a, F, S>;
53
54 fn format<T>(&'a self, time: T) -> Self::Inner
55 where
56 T: Into<Option<TimeSpan>>,
57 {
58 Inner {
59 time: time.into(),
60 wrapper: self,
61 }
62 }
63}
64
65impl<'a, F: TimeFormatter<'a>, S: 'a + AsRef<str>> Display for Inner<'a, F, S> {
66 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
67 if self.time.is_none() {
68 self.wrapper.1.as_ref().fmt(f)
69 } else {
70 self.wrapper.0.format(self.time).fmt(f)
71 }
72 }
73}