humfmt 0.4.0

Ergonomic human-readable formatting toolkit for Rust
Documentation
use core::fmt;

use crate::duration::{duration_with, DurationLike, DurationOptions};
use crate::locale::{English, Locale};

/// `Display` wrapper for relative time output (e.g. `"1m 30s ago"`).
///
/// Instances of this type are created via [`crate::ago`] and [`crate::ago_with`].
/// It builds on the duration formatter and appends the locale-specific "ago"
/// word.
///
/// This type does not allocate on its own; allocation only happens if the caller
/// requests an owned `String` via `.to_string()` / `format!(...)`.
#[derive(Copy, Clone, Debug)]
pub struct AgoDisplay<L: Locale = English> {
    value: core::time::Duration,
    options: DurationOptions<L>,
}

impl<L: Locale> AgoDisplay<L> {
    pub(crate) fn new<T: DurationLike>(value: T, options: DurationOptions<L>) -> Self {
        Self {
            value: value.into_duration(),
            options,
        }
    }
}

impl<L: Locale> fmt::Display for AgoDisplay<L> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} {}",
            duration_with(self.value, self.options),
            self.options.locale.ago_word()
        )
    }
}