wdxtools 0.4.2

Everyday formatting tools for the command line
Documentation
/// Controls how the output is framed (absolute vs relative, and to what).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Syntax {
    /// Auto-detect: relative to now unless an explicit "other" time is given.
    #[default]
    RelativeAuto,
    /// Bare duration with no temporal modifier.
    Absolute,
    /// Appends "ago" or "from now".
    RelativeToNow,
    /// Appends "before" or "after".
    RelativeToOther,
}

/// Bitmask flags controlling formatting details.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Options(u32);

impl Options {
    pub const NONE: Options = Options(0);
    /// Shows "1 second" instead of "0 seconds" when the diff is zero.
    pub const NO_ZERO_DIFF: Options = Options(1 << 0);
    /// Shows "just now" for zero diffs in relative-to-now mode.
    pub const JUST_NOW: Options = Options(1 << 1);
    /// Uses "yesterday"/"tomorrow" for 1-day diffs.
    pub const ONE_DAY_WORDS: Options = Options(1 << 2);
    /// Uses "before yesterday"/"after tomorrow" for 2-day diffs.
    pub const TWO_DAY_WORDS: Options = Options(1 << 3);
    /// Stops collecting parts at the first zero-value gap.
    pub const SEQUENTIAL_PARTS_ONLY: Options = Options(1 << 4);

    #[inline]
    pub const fn contains(self, other: Options) -> bool {
        self.0 & other.0 != 0
    }
}

impl std::ops::BitOr for Options {
    type Output = Options;
    #[inline]
    fn bitor(self, rhs: Options) -> Options {
        Options(self.0 | rhs.0)
    }
}

impl std::ops::BitOrAssign for Options {
    #[inline]
    fn bitor_assign(&mut self, rhs: Options) {
        self.0 |= rhs.0;
    }
}

/// Identifies a time unit, used with [`Config::skip`](crate::timediff::Config::skip)
/// to exclude units from the output.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(usize)]
pub enum Unit {
    Year = 0,
    Month = 1,
    Week = 2,
    Day = 3,
    Hour = 4,
    Minute = 5,
    Second = 6,
}

/// Formatting configuration for [`diff_for_humans`](crate::timediff::diff_for_humans).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Config {
    pub syntax: Syntax,
    /// Abbreviated unit names, e.g. "2h" instead of "2 hours".
    pub short: bool,
    /// Maximum number of time units to display (1-7).
    pub parts: usize,
    pub options: Options,
    /// Bitset of skipped units, indexed by [`Unit`].
    pub(crate) skip: u8,
}

impl Default for Config {
    fn default() -> Self {
        Self::new()
    }
}

impl Config {
    pub const fn new() -> Self {
        Self {
            syntax: Syntax::RelativeAuto,
            short: false,
            parts: 1,
            options: Options::NONE,
            skip: 0,
        }
    }

    pub const fn syntax(mut self, s: Syntax) -> Self {
        self.syntax = s;
        self
    }

    pub const fn short(mut self, short: bool) -> Self {
        self.short = short;
        self
    }

    /// Clamped to 1-7.
    pub const fn parts(mut self, n: usize) -> Self {
        self.parts = if n < 1 {
            1
        } else if n > 7 {
            7
        } else {
            n
        };
        self
    }

    pub const fn options(mut self, o: Options) -> Self {
        self.options = o;
        self
    }

    /// Excludes the given units from the output, cascading their values to the
    /// next smaller unit.
    pub fn skip(mut self, units: &[Unit]) -> Self {
        for u in units {
            self.skip |= 1 << (*u as usize);
        }
        self
    }
}