parallel_disk_usage/args/
threads.rs

1use derive_more::{Display, Error};
2use std::{num::ParseIntError, str::FromStr};
3
4const AUTO: &str = "auto";
5const MAX: &str = "max";
6
7/// Number of rayon threads.
8#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Display)]
9pub enum Threads {
10    #[default]
11    #[display("{AUTO}")]
12    Auto,
13    #[display("{MAX}")]
14    Max,
15    Fixed(usize),
16}
17
18/// Error that occurs when converting a string to an instance of [`Threads`].
19#[derive(Debug, Display, Clone, PartialEq, Eq, Error)]
20pub enum FromStrError {
21    #[display("Value is neither {AUTO:?}, {MAX:?}, nor a number: {_0}")]
22    InvalidSyntax(ParseIntError),
23}
24
25impl FromStr for Threads {
26    type Err = FromStrError;
27    fn from_str(value: &str) -> Result<Self, Self::Err> {
28        let value = value.trim();
29        match value {
30            AUTO => return Ok(Threads::Auto),
31            MAX => return Ok(Threads::Max),
32            _ => {}
33        };
34        value
35            .parse()
36            .map_err(FromStrError::InvalidSyntax)
37            .map(Threads::Fixed)
38    }
39}