aboutsummaryrefslogtreecommitdiff
path: root/arg_picker/src/builtin/pick_numbers.rs
blob: a5ab0a985c2b090eb6b63b49b9523cc97e9d6b5f (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
use crate::{BoundaryCheck, SinglePickable, pickable_needed::*};

macro_rules! impl_single_pickable_num {
    ($($t:ty),+ $(,)?) => {
        $(impl SinglePickable for $t {
            fn pick_single(str: Option<&str>) -> PickerArgResult<Self> {
                match str {
                    Some(s) => s.parse::<$t>().map(PickerArgResult::Parsed).unwrap_or(PickerArgResult::NotFound),
                    None => PickerArgResult::NotFound,
                }
            }
        })+
    };
}

/// Returns `true` if `raw` looks like an integer (digits, optional `+`/`-` prefix,
/// no decimal point or exponent).
fn is_int_like(raw: &str) -> bool {
    let s = raw.trim();
    let bytes = s.as_bytes();
    if bytes.is_empty() {
        return false;
    }
    let mut i = 0;
    if bytes[0] == b'-' || bytes[0] == b'+' {
        i = 1;
    }
    if i >= bytes.len() {
        return false;
    }
    for &b in &bytes[i..] {
        if !b.is_ascii_digit() {
            return false;
        }
    }
    true
}

/// Returns `true` if `raw` looks like a float (contains `.`, `e`, or `E`).
fn is_float_like(raw: &str) -> bool {
    let s = raw.trim();
    s.contains('.') || s.contains('e') || s.contains('E')
}

impl_single_pickable_num! {
    i8, i16, i32, i64, i128, isize,
    u8, u16, u32, u64, u128, usize,
    f32, f64,
}

// Integer boundary: only accept strings that look like integers.
// Float-like strings trigger a boundary.
macro_rules! impl_boundary_check_int {
    ($($t:ty),+ $(,)?) => {
        $(impl BoundaryCheck for $t {
            fn check_boundary(raw: &str) -> bool {
                !is_int_like(raw)
            }
        })+
    };
}

impl_boundary_check_int! {
    i8, i16, i32, i64, i128, isize,
    u8, u16, u32, u64, u128, usize,
}

// Float boundary: only accept strings that look like floats.
// Integer-like strings trigger a boundary.
impl BoundaryCheck for f32 {
    fn check_boundary(raw: &str) -> bool {
        !is_float_like(raw) || raw.parse::<f32>().is_err()
    }
}

impl BoundaryCheck for f64 {
    fn check_boundary(raw: &str) -> bool {
        !is_float_like(raw) || raw.parse::<f64>().is_err()
    }
}