summaryrefslogtreecommitdiff
path: root/src/value.rs
blob: 77254904048ba05141e5501c9cf3610cec90cf5e (plain) (blame)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use crate::*;

use std::path::PathBuf;


macro_rules! as_number {
    ($type:ty, $name:expr, $error:ty) => {
        paste::paste! {
            pub fn [< as_ $type >](&mut self) -> $type {
                self.[< as_ $type _opt >]().unwrap_or_else(|| self.missing("number"))
            }
            pub fn [< as_ $type _opt>](&mut self) -> Option<$type> {
                self.value().as_ref().map(|v| v.trim().parse().unwrap_or_else(
                    |e: $error| self.error(&v, $name, e.to_string())))
            }
        }
    };
}


pub struct QueriedValue {
    pub doc: Option<String>,
    pub name: String,
    pub variant: QueryVariant,
    /// Some if switch passed, then some if value passed.
    pub value: Option<Option<String>>,
}

pub enum QueryVariant {
    Positional(usize),
    Named(String),
}

impl QueriedValue {
    pub fn value(&self) -> Option<&String> {
        match &self.value {
            Some(value) => value.as_ref(),
            None => None,
        }
    }

    pub fn as_bool(self) -> bool {
        if let Some(value) = self.value {
            if let Some(value) = value {
                match value.to_lowercase().as_str() {
                    "y"|"yes"|"t"|"true"  => true,
                    "n"|"no" |"f"|"false" => false,
                    _ => true,
                }
            } else {
                true
            }
        } else {
            false
        }
    }

    pub fn as_path(&mut self) -> PathBuf {
        self.as_path_opt().unwrap_or_else(|| self.missing("path"))
    }
    pub fn as_path_opt(&mut self) -> Option<PathBuf> {
        if let Some(value) = self.value() {
            if !value.is_empty() {
                return Some(PathBuf::from(value));
            }
        }
        return None;
    }

    pub fn as_str(&mut self) -> &str {
        self.value().map(|v| v.as_str()).unwrap_or_else(|| self.missing("string"))
    }
    pub fn as_str_opt(&mut self) -> Option<&str> {
        self.value().map(|v| v.as_str())
    }

    pub fn as_string(&mut self) -> String {
        self.as_str().to_string()
    }
    pub fn as_string_opt(&mut self) -> Option<String> {
        self.as_str_opt().map(|s| s.to_string())
    }

    as_number!{ f32,   "f32"  , std::num::ParseFloatError }
    as_number!{ f64,   "f64"  , std::num::ParseFloatError }
    as_number!{ u8,    "u8"   , std::num::ParseIntError   }
    as_number!{ u16,   "u16"  , std::num::ParseIntError   }
    as_number!{ u32,   "u32"  , std::num::ParseIntError   }
    as_number!{ u64,   "u64"  , std::num::ParseIntError   }
    as_number!{ usize, "usize", std::num::ParseIntError   }
    as_number!{ i8,    "i8"   , std::num::ParseIntError   }
    as_number!{ i16,   "i16"  , std::num::ParseIntError   }
    as_number!{ i32,   "i32"  , std::num::ParseIntError   }
    as_number!{ i64,   "i64"  , std::num::ParseIntError   }
    as_number!{ isize, "isize", std::num::ParseIntError   }


    fn locator(&self) -> String {
        match &self.variant {
            QueryVariant::Positional(_) => {
                let name = &self.name;
                format!("<{name}> argument")
            }
            QueryVariant::Named(name) => {
                format!("{name} switch")
            }
        }
    }

    fn error(&self, value: &str, ty: &str, err: String) -> ! {
        let locator = self.locator();
        fatal!("The value {value:?} passed to the {locator} could not be parsed as a {ty}: {err}");
    }

    fn missing(&self, _ty: &str) -> ! {
        let locator = self.locator();
        fatal!("The {locator} is required");
    }
}