Skip to content

Commit 6673446

Browse files
authored
Merge pull request #157 from KSXGitHub/use-fromstr-again
Derive `thiserror::Error`
2 parents 0740742 + 5a68a31 commit 6673446

File tree

12 files changed

+36
-64
lines changed

12 files changed

+36
-64
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ cli-completions = ["cli"]
5151
pipe-trait = "^0.4.0"
5252
smart-default = "^0.6.0"
5353
derive_more = "^0.99.17"
54+
thiserror = "^1.0.32"
5455
rayon = "^1.5.3"
5556
text-block-macros = "^0.1.1"
5657
rounded-div = "^0.1.2"

exports/completion.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ _pdu() {
1919

2020
case "${cmd}" in
2121
pdu)
22-
opts="-h --help --json-input --json-output --bytes-format --top-down --align-left --quantity --max-depth --total-width --column-width --min-ratio --no-sort --silent-errors --progress <FILES>..."
22+
opts="-h -V --help --version --json-input --json-output --bytes-format --top-down --align-left --quantity --max-depth --total-width --column-width --min-ratio --no-sort --silent-errors --progress <FILES>..."
2323
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
2424
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
2525
return 0

exports/completion.elv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ set edit:completion:arg-completer[pdu] = {|@words|
2626
cand --min-ratio 'Minimal size proportion required to appear'
2727
cand -h 'Print help information'
2828
cand --help 'Print help information'
29+
cand -V 'Print version information'
30+
cand --version 'Print version information'
2931
cand --json-input 'Read JSON data from stdin'
3032
cand --json-output 'Print JSON data instead of an ASCII chart'
3133
cand --top-down 'Print the tree top-down instead of bottom-up'

exports/completion.fish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ complete -c pdu -l total-width -d 'Width of the visualization' -r
55
complete -c pdu -l column-width -d 'Maximum widths of the tree column and width of the bar column' -r
66
complete -c pdu -l min-ratio -d 'Minimal size proportion required to appear' -r
77
complete -c pdu -s h -l help -d 'Print help information'
8+
complete -c pdu -s V -l version -d 'Print version information'
89
complete -c pdu -l json-input -d 'Read JSON data from stdin'
910
complete -c pdu -l json-output -d 'Print JSON data instead of an ASCII chart'
1011
complete -c pdu -l top-down -d 'Print the tree top-down instead of bottom-up'

exports/completion.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Register-ArgumentCompleter -Native -CommandName 'pdu' -ScriptBlock {
2929
[CompletionResult]::new('--min-ratio', 'min-ratio', [CompletionResultType]::ParameterName, 'Minimal size proportion required to appear')
3030
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information')
3131
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information')
32+
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version information')
33+
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version information')
3234
[CompletionResult]::new('--json-input', 'json-input', [CompletionResultType]::ParameterName, 'Read JSON data from stdin')
3335
[CompletionResult]::new('--json-output', 'json-output', [CompletionResultType]::ParameterName, 'Print JSON data instead of an ASCII chart')
3436
[CompletionResult]::new('--top-down', 'top-down', [CompletionResultType]::ParameterName, 'Print the tree top-down instead of bottom-up')

exports/completion.zsh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ blksize\:"Measure block sizes, equivalent to the \[blksize\](std::os::unix::prel
2323
blocks\:"Count numbers of blocks, equivalent to the \[blocks\](std::os::unix::prelude::MetadataExt::blocks) method (POSIX only)"))' \
2424
'--max-depth=[Maximum depth to display the data (must be greater than 0)]:MAX_DEPTH: ' \
2525
'(--column-width)--total-width=[Width of the visualization]:TOTAL_WIDTH: ' \
26-
'*--column-width=[Maximum widths of the tree column and width of the bar column]:tree-width: :tree-width: ' \
27-
'*--min-ratio=[Minimal size proportion required to appear]:MIN_RATIO: ' \
26+
'*--column-width=[Maximum widths of the tree column and width of the bar column]:TREE_WIDTH: :TREE_WIDTH: ' \
27+
'--min-ratio=[Minimal size proportion required to appear]:MIN_RATIO: ' \
2828
'-h[Print help information]' \
2929
'--help[Print help information]' \
30+
'-V[Print version information]' \
31+
'--version[Print version information]' \
3032
'(--quantity)--json-input[Read JSON data from stdin]' \
3133
'--json-output[Print JSON data instead of an ASCII chart]' \
3234
'--top-down[Print the tree top-down instead of bottom-up]' \

src/args.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use text_block_macros::text_block;
1515
#[clap(
1616
name = "pdu",
1717
18+
version,
19+
1820
long_about = text_block! {
1921
"Summarize disk usage of the set of files, recursively for directories."
2022
""
@@ -108,11 +110,11 @@ pub struct Args {
108110
pub total_width: Option<usize>,
109111

110112
/// Maximum widths of the tree column and width of the bar column.
111-
#[clap(long, number_of_values = 2, value_names = &["tree-width", "bar-width"])]
113+
#[clap(long, number_of_values = 2, value_names = &["TREE_WIDTH", "BAR_WIDTH"])]
112114
pub column_width: Option<Vec<usize>>,
113115

114116
/// Minimal size proportion required to appear.
115-
#[clap(long, default_value = "0.01", value_parser)]
117+
#[clap(long, default_value = "0.01")]
116118
pub min_ratio: Fraction,
117119

118120
/// Preserve order of entries.

src/args/fraction.rs

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
use clap::builder::{TypedValueParser, ValueParserFactory};
21
use derive_more::{AsRef, Deref, Display, Into};
32
use std::{
43
convert::{TryFrom, TryInto},
54
num::ParseFloatError,
65
str::FromStr,
76
};
7+
use thiserror::Error;
88

99
/// Floating-point value that is greater than or equal to 0 and less than 1.
1010
#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd, AsRef, Deref, Display, Into)]
1111
pub struct Fraction(f32);
1212

1313
/// Error that occurs when calling [`Fraction::new`].
14-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display)]
14+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
1515
pub enum ConversionError {
1616
/// Provided value is greater than or equal to 1.
17-
#[display(fmt = "greater than or equal to 1")]
17+
#[error("greater than or equal to 1")]
1818
UpperBound,
1919
/// Provided value is less than 0.
20-
#[display(fmt = "less than 0")]
20+
#[error("less than 0")]
2121
LowerBound,
2222
}
2323

@@ -42,7 +42,8 @@ impl TryFrom<f32> for Fraction {
4242
}
4343
}
4444

45-
#[derive(Debug, Clone, PartialEq, Eq, Display)]
45+
#[derive(Debug, Clone, PartialEq, Eq, Error)]
46+
#[error("{_0}")]
4647
pub enum FromStrError {
4748
ParseFloatError(ParseFloatError),
4849
Conversion(ConversionError),
@@ -57,39 +58,3 @@ impl FromStr for Fraction {
5758
.map_err(FromStrError::Conversion)
5859
}
5960
}
60-
61-
impl ValueParserFactory for Fraction {
62-
type Parser = FractionValueParser;
63-
fn value_parser() -> Self::Parser {
64-
FractionValueParser
65-
}
66-
}
67-
68-
/// The [parser](ValueParserFactory::Parser) of [`Fraction`].
69-
#[derive(Debug, Clone)]
70-
#[non_exhaustive]
71-
pub struct FractionValueParser;
72-
73-
impl TypedValueParser for FractionValueParser {
74-
type Value = Fraction;
75-
76-
fn parse_ref(
77-
&self,
78-
_: &clap::Command,
79-
arg: Option<&clap::Arg>,
80-
value: &std::ffi::OsStr,
81-
) -> Result<Self::Value, clap::Error> {
82-
let value = value
83-
.to_str()
84-
.ok_or_else(|| clap::Error::raw(clap::ErrorKind::InvalidUtf8, "Invalid UTF-8"))?;
85-
value.parse().map_err(|error| {
86-
clap::Error::raw(
87-
clap::ErrorKind::ValueValidation,
88-
format_args!(
89-
"Invalid value {value:?} for '{arg}': {error}\n\nFor more information try --help\n",
90-
arg = arg.map_or_else(|| "...".to_string(), |arg| arg.to_string()),
91-
),
92-
)
93-
})
94-
}
95-
}

src/bytes_format/parsed_value.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
use derive_more::Display;
1+
use thiserror::Error;
22

33
/// Return value of [`Formatter::parse_value`](super::Formatter::parse_value).
4-
#[derive(Debug, Display, Clone, Copy)]
4+
#[derive(Debug, Clone, Copy, Error)]
55
pub enum ParsedValue {
66
/// When input value is less than `scale_base`.
7-
#[display(fmt = "{} ", value)]
7+
#[error("{value} ")]
88
Small {
99
/// Input value that is less than `scale_base`.
1010
value: u16,
1111
},
1212
/// When input value is greater than `scale_base`.
13-
#[display(fmt = "{:.1}{}", coefficient, unit)]
13+
#[error("{coefficient:.1}{unit}")]
1414
Big {
1515
/// The visible part of the number.
1616
coefficient: f32,

0 commit comments

Comments
 (0)