prodash/unit/
range.rs

1use std::{fmt, hash::Hasher};
2
3use crate::{progress::Step, unit::DisplayValue};
4
5/// A helper for formatting numbers representing ranges in renderers as in `2 of 5 steps`.
6#[derive(Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Debug)]
7pub struct Range {
8    /// The name of the unit to be appended to the range.
9    pub name: &'static str,
10}
11
12impl Range {
13    /// A convenience method to create a new instance of `name`.
14    pub fn new(name: &'static str) -> Self {
15        Range { name }
16    }
17}
18
19impl DisplayValue for Range {
20    fn display_current_value(&self, w: &mut dyn fmt::Write, value: Step, _upper: Option<Step>) -> fmt::Result {
21        w.write_fmt(format_args!("{}", value + 1))
22    }
23    fn separator(&self, w: &mut dyn fmt::Write, _value: Step, _upper: Option<Step>) -> fmt::Result {
24        w.write_str(" of ")
25    }
26
27    fn dyn_hash(&self, state: &mut dyn Hasher) {
28        self.name.dyn_hash(state)
29    }
30
31    fn display_unit(&self, w: &mut dyn fmt::Write, _value: Step) -> fmt::Result {
32        w.write_str(self.name)
33    }
34}