1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::{collections::HashMap, str::FromStr};

use anyhow::{Context, Error};

/// Helper methods for reading arguments.
#[derive(Debug, Clone, PartialEq)]
#[repr(transparent)]
pub struct Arguments(pub HashMap<String, String>);

impl Arguments {
    pub fn parse<T>(&self, name: &str) -> Result<T, Error>
    where
        T: FromStr,
        T::Err: std::error::Error + Send + Sync + 'static,
    {
        match self.0.get(name) {
            Some(value) => value.parse::<T>().with_context(|| {
                format!(
                    "Unable to parse {:?} as the \"{}\" argument",
                    value, name
                )
            }),
            None => {
                Err(anyhow::anyhow!("The \"{}\" argument wasn't set", name))
            },
        }
    }

    pub fn parse_or_default<T>(
        &self,
        name: &str,
        default: T,
    ) -> Result<T, Error>
    where
        T: FromStr,
        T::Err: std::error::Error + Send + Sync + 'static,
    {
        match self.0.get(name) {
            Some(value) => value.parse::<T>().with_context(|| {
                format!(
                    "Unable to parse {:?} as the \"{}\" argument",
                    value, name
                )
            }),
            None => Ok(default),
        }
    }
}