parallel_disk_usage/args/
threads.rs

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