parallel_disk_usage/args/
depth.rs1use derive_more::{Display, Error};
2use std::{
3 num::{NonZeroU64, ParseIntError, TryFromIntError},
4 str::FromStr,
5};
6
7const INFINITE: &str = "inf";
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Display)]
11pub enum Depth {
12 #[display("{INFINITE}")]
13 Infinite,
14 Finite(NonZeroU64),
15}
16
17impl Depth {
18 pub(crate) fn get(self) -> u64 {
20 match self {
21 Depth::Infinite => u64::MAX,
22 Depth::Finite(value) => value.get(),
23 }
24 }
25}
26
27#[derive(Debug, Display, Clone, PartialEq, Eq, Error)]
29#[non_exhaustive]
30pub enum FromStrError {
31 #[display("Value is neither {INFINITE:?} nor a positive integer: {_0}")]
32 InvalidSyntax(ParseIntError),
33}
34
35impl FromStr for Depth {
36 type Err = FromStrError;
37 fn from_str(text: &str) -> Result<Self, Self::Err> {
38 let text = text.trim();
39 if text == INFINITE {
40 return Ok(Depth::Infinite);
41 }
42 text.parse()
43 .map_err(FromStrError::InvalidSyntax)
44 .map(Depth::Finite)
45 }
46}
47
48impl TryFrom<u64> for Depth {
49 type Error = TryFromIntError;
50 fn try_from(value: u64) -> Result<Self, Self::Error> {
51 value.try_into().map(Depth::Finite)
52 }
53}