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(
23            name = "plain",
24            help = "Display plain number of bytes without units",
25            alias = "1"
26        )
27    )]
28    PlainNumber,
29    /// Display the value with a unit suffix in [metric scale](formatter::METRIC).
30    #[cfg_attr(
31        feature = "cli",
32        clap(
33            name = "metric",
34            help = "Use metric scale, i.e. 1K = 1000B, 1M = 1000K, and so on",
35            alias = "1000"
36        )
37    )]
38    MetricUnits,
39    /// Display the value with a unit suffix in [binary scale](formatter::BINARY).
40    #[cfg_attr(
41        feature = "cli",
42        clap(
43            name = "binary",
44            help = "Use binary scale, i.e. 1K = 1024B, 1M = 1024K, and so on",
45            alias = "1024"
46        )
47    )]
48    BinaryUnits,
49}
50
51impl BytesFormat {
52    /// Format a quantity of bytes according to the settings.
53    pub fn format(self, bytes: u64) -> Output {
54        use formatter::{BINARY, METRIC};
55        use BytesFormat::*;
56        match self {
57            PlainNumber => Output::PlainNumber(bytes),
58            MetricUnits => METRIC.parse_value(bytes).pipe(Output::Units),
59            BinaryUnits => BINARY.parse_value(bytes).pipe(Output::Units),
60        }
61    }
62}