use super::{TimeFormatter, DASH};
use crate::TimeSpan;
use core::fmt::{Display, Formatter, Result};
pub struct Inner<'a, F, S> {
time: Option<TimeSpan>,
wrapper: &'a NoneWrapper<F, S>,
}
pub struct NoneWrapper<F, S>(F, S);
pub struct DashWrapper;
pub struct EmptyWrapper;
impl<'a, F: 'a + TimeFormatter<'a>, S: AsRef<str>> NoneWrapper<F, S> {
pub const fn new(inner: F, none_text: S) -> Self {
NoneWrapper(inner, none_text)
}
}
impl DashWrapper {
pub const fn new<'a, F: 'a + TimeFormatter<'a>>(inner: F) -> NoneWrapper<F, &'static str> {
NoneWrapper::new(inner, DASH)
}
}
impl EmptyWrapper {
pub const fn new<'a, F: 'a + TimeFormatter<'a>>(inner: F) -> NoneWrapper<F, &'static str> {
NoneWrapper::new(inner, "")
}
}
impl<'a, F: 'a + TimeFormatter<'a>, S: 'a + AsRef<str>> TimeFormatter<'a> for NoneWrapper<F, S> {
type Inner = Inner<'a, F, S>;
fn format<T>(&'a self, time: T) -> Self::Inner
where
T: Into<Option<TimeSpan>>,
{
Inner {
time: time.into(),
wrapper: self,
}
}
}
impl<'a, F: TimeFormatter<'a>, S: 'a + AsRef<str>> Display for Inner<'a, F, S> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
if self.time.is_none() {
self.wrapper.1.as_ref().fmt(f)
} else {
self.wrapper.0.format(self.time).fmt(f)
}
}
}