parallel_disk_usage/
bytes_format.rs

1pub mod formatter;
2pub mod output;
3pub mod parsed_value;
4pub mod scale_base;
5
6pub use formatter::Formatter;
7pub use output::Output;
8pub use parsed_value::ParsedValue;
9
10use pipe_trait::Pipe;
11
12#[cfg(feature = "cli")]
13use clap::ValueEnum;
14
15/// The [`DisplayFormat`](crate::size::Size::DisplayFormat) type of [`Bytes`](crate::size::Bytes).
16#[derive(Debug, Clone, Copy)]
17#[cfg_attr(feature = "cli", derive(ValueEnum))]
18pub enum BytesFormat {
19    /// Display the value as-is.
20    #[cfg_attr(
21        feature = "cli",
22        clap(name = "plain", help = "Display plain number of bytes without units")
23    )]
24    PlainNumber,
25    /// Display the value with a unit suffix in [metric scale](formatter::METRIC).
26    #[cfg_attr(
27        feature = "cli",
28        clap(
29            name = "metric",
30            help = "Use metric scale, i.e. 1K = 1000B, 1M = 1000K, and so on",
31        )
32    )]
33    MetricUnits,
34    /// Display the value with a unit suffix in [binary scale](formatter::BINARY).
35    #[cfg_attr(
36        feature = "cli",
37        clap(
38            name = "binary",
39            help = "Use binary scale, i.e. 1K = 1024B, 1M = 1024K, and so on",
40        )
41    )]
42    BinaryUnits,
43}
44
45impl BytesFormat {
46    /// Format a quantity of bytes according to the settings.
47    pub fn format(self, bytes: u64) -> Output {
48        use formatter::{BINARY, METRIC};
49        use BytesFormat::*;
50        match self {
51            PlainNumber => Output::PlainNumber(bytes),
52            MetricUnits => METRIC.parse_value(bytes).pipe(Output::Units),
53            BinaryUnits => BINARY.parse_value(bytes).pipe(Output::Units),
54        }
55    }
56}