1use crate::error::{MyError, MyResult};
2use regex::{Match, Regex};
3use std::num::ParseIntError;
4
5#[derive(Clone, Default)]
6pub struct RecurseDepth {
7 pub min_depth: Option<usize>,
8 pub max_depth: Option<usize>,
9}
10
11impl RecurseDepth {
12 pub fn from_str(value: &str) -> MyResult<Self> {
13 match value.parse::<usize>() {
14 Ok(value) => Ok(Self::from_pair(Some(1), Some(value + 1))),
15 Err(_) => {
16 let re = Regex::new("^(\\d+)?-(\\d+)?$")?;
17 match re.captures(value) {
18 Some(captures) => {
19 let min_depth = parse_integer(captures.get(1))?;
20 let max_depth = parse_integer(captures.get(2))?;
21 let min_depth = min_depth.map(|x| x + 1).or(Some(1));
22 let max_depth = max_depth.map(|x| x + 1);
23 Ok(Self::from_pair(min_depth, max_depth))
24 }
25 None => Err(MyError::create_clap("depth", value)),
26 }
27 }
28 }
29 }
30
31 fn from_pair(min_depth: Option<usize>, max_depth: Option<usize>) -> Self {
32 Self { min_depth, max_depth }
33 }
34}
35
36fn parse_integer(matched: Option<Match>) -> Result<Option<usize>, ParseIntError> {
37 match matched {
38 Some(m) => m.as_str().parse().map(|x| Some(x)),
39 None => Ok(None),
40 }
41}