From 14e3f50651deeedc1e2754645e4cfb7fa8465e72 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Wed, 23 Feb 2022 21:24:41 -0500 Subject: [PATCH] df: move BlockSize to a new module, blocks.rs --- src/uu/df/src/blocks.rs | 56 +++++++++++++++++++++++++++++++++++++++++ src/uu/df/src/df.rs | 51 ++----------------------------------- 2 files changed, 58 insertions(+), 49 deletions(-) create mode 100644 src/uu/df/src/blocks.rs diff --git a/src/uu/df/src/blocks.rs b/src/uu/df/src/blocks.rs new file mode 100644 index 000000000..bb6a86df5 --- /dev/null +++ b/src/uu/df/src/blocks.rs @@ -0,0 +1,56 @@ +// * This file is part of the uutils coreutils package. +// * +// * For the full copyright and license information, please view the LICENSE +// * file that was distributed with this source code. +//! Types for representing and displaying block sizes. +use crate::{OPT_HUMAN_READABLE, OPT_HUMAN_READABLE_2}; +use clap::ArgMatches; + +/// A block size to use in condensing the display of a large number of bytes. +/// +/// The [`BlockSize::Bytes`] variant represents a static block +/// size. The [`BlockSize::HumanReadableDecimal`] and +/// [`BlockSize::HumanReadableBinary`] variants represent dynamic +/// block sizes: as the number of bytes increases, the divisor +/// increases as well (for example, from 1 to 1,000 to 1,000,000 and +/// so on in the case of [`BlockSize::HumanReadableDecimal`]). +/// +/// The default variant is `Bytes(1024)`. +pub(crate) enum BlockSize { + /// A fixed number of bytes. + /// + /// The number must be positive. + Bytes(u64), + + /// Use the largest divisor corresponding to a unit, like B, K, M, G, etc. + /// + /// This variant represents powers of 1,000. Contrast with + /// [`BlockSize::HumanReadableBinary`], which represents powers of + /// 1,024. + HumanReadableDecimal, + + /// Use the largest divisor corresponding to a unit, like B, K, M, G, etc. + /// + /// This variant represents powers of 1,024. Contrast with + /// [`BlockSize::HumanReadableDecimal`], which represents powers + /// of 1,000. + HumanReadableBinary, +} + +impl Default for BlockSize { + fn default() -> Self { + Self::Bytes(1024) + } +} + +impl From<&ArgMatches> for BlockSize { + fn from(matches: &ArgMatches) -> Self { + if matches.is_present(OPT_HUMAN_READABLE) { + Self::HumanReadableBinary + } else if matches.is_present(OPT_HUMAN_READABLE_2) { + Self::HumanReadableDecimal + } else { + Self::default() + } + } +} diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index 6371bb0be..5e3812375 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -6,6 +6,7 @@ // For the full copyright and license information, please view the LICENSE file // that was distributed with this source code. // spell-checker:ignore itotal iused iavail ipcent pcent tmpfs squashfs +mod blocks; mod table; #[cfg(unix)] @@ -21,6 +22,7 @@ use std::iter::FromIterator; #[cfg(windows)] use std::path::Path; +use crate::blocks::BlockSize; use crate::table::{DisplayRow, Header, Row}; static ABOUT: &str = "Show information about the file system on which each FILE resides,\n\ @@ -57,55 +59,6 @@ struct FsSelector { exclude: HashSet, } -/// A block size to use in condensing the display of a large number of bytes. -/// -/// The [`BlockSize::Bytes`] variant represents a static block -/// size. The [`BlockSize::HumanReadableDecimal`] and -/// [`BlockSize::HumanReadableBinary`] variants represent dynamic -/// block sizes: as the number of bytes increases, the divisor -/// increases as well (for example, from 1 to 1,000 to 1,000,000 and -/// so on in the case of [`BlockSize::HumanReadableDecimal`]). -/// -/// The default variant is `Bytes(1024)`. -enum BlockSize { - /// A fixed number of bytes. - /// - /// The number must be positive. - Bytes(u64), - - /// Use the largest divisor corresponding to a unit, like B, K, M, G, etc. - /// - /// This variant represents powers of 1,000. Contrast with - /// [`BlockSize::HumanReadableBinary`], which represents powers of - /// 1,024. - HumanReadableDecimal, - - /// Use the largest divisor corresponding to a unit, like B, K, M, G, etc. - /// - /// This variant represents powers of 1,024. Contrast with - /// [`BlockSize::HumanReadableDecimal`], which represents powers - /// of 1,000. - HumanReadableBinary, -} - -impl Default for BlockSize { - fn default() -> Self { - Self::Bytes(1024) - } -} - -impl From<&ArgMatches> for BlockSize { - fn from(matches: &ArgMatches) -> Self { - if matches.is_present(OPT_HUMAN_READABLE) { - Self::HumanReadableBinary - } else if matches.is_present(OPT_HUMAN_READABLE_2) { - Self::HumanReadableDecimal - } else { - Self::default() - } - } -} - /// Parameters that control the behavior of `df`. /// /// Most of these parameters control which rows and which columns are