use alloc::string::String;
use core::time::Duration;
pub trait MillisecondFormatter {
type Output;
fn pretty_with(&self, opt: MillisecondOption) -> Self::Output;
fn pretty(&self) -> Self::Output {
self.pretty_with(MillisecondOption::default())
}
#[deprecated(since = "0.4.0", note = "use the `pretty` instead")]
fn to_short_string(&self) -> Self::Output {
self.pretty()
}
#[deprecated(since = "0.4.0", note = "use the `pretty_with` function instead")]
fn to_long_string(&self) -> Self::Output {
self.pretty_with(MillisecondOption::long())
}
}
impl MillisecondFormatter for Duration {
type Output = String;
fn pretty_with(&self, opt: MillisecondOption) -> Self::Output {
let parts = super::parser::parse_duration(self, &opt);
super::parser::ms_parts_to_string(&parts, &opt)
}
}
#[derive(Debug, Copy, Clone, Default)]
pub struct MillisecondOption {
pub long: bool,
pub days_instead_of_years: bool,
pub dominant_only: bool,
pub format_sub_milliseconds: bool,
pub seconds: SecondsOptions,
}
impl MillisecondOption {
pub fn long() -> Self {
Self {
long: true,
..Default::default()
}
}
pub fn sub_milliseconds() -> Self {
Self {
format_sub_milliseconds: true,
..Default::default()
}
}
#[cfg(test)]
pub(crate) fn backward_compatible() -> Self {
Self {
format_sub_milliseconds: true,
seconds: SecondsOptions::Separate,
..Self::default()
}
}
}
#[derive(Debug, Copy, Clone, Default)]
pub enum SecondsOptions {
#[default]
Separate,
Combine,
CombineWith {
precision: u8,
fixed_width: bool,
},
Hide,
}
impl SecondsOptions {
pub fn precision(&self) -> u8 {
let p = match self {
Self::CombineWith { precision, .. } => (*precision).clamp(1, 3),
_ => 1,
};
if self.is_fixed_width() { p.min(3) } else { p }
}
pub fn is_fixed_width(&self) -> bool {
match self {
Self::CombineWith { fixed_width, .. } => *fixed_width,
_ => false,
}
}
}