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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use std::marker::PhantomData;
use nom::bytes::complete::tag;
use nom::character::complete::{char, one_of};
use nom::combinator::{opt, recognize};
use nom::error::{ErrorKind, FromExternalError};
use nom::multi::{many0, many1};
use nom::sequence::{preceded, separated_pair, terminated};
use nom::IResult;
use super::CommandThen;
use crate::error::CmdErrorKind;
use crate::{ArgumentMarkerDefaultImpl, ChildUsage, CommandArgument, CommandError, IntoMultipleUsage, Then};
pub struct NumberArgument<N, S> {
pub(crate) name: &'static str,
pub(crate) min: N,
pub(crate) max: N,
pub(crate) parse: fn(&str) -> IResult<&str, N, CommandError>,
pub(crate) source: PhantomData<S>,
}
impl<N, S> NumberArgument<N, S>
where
N: PartialOrd,
{
pub fn min(mut self, min: N) -> Self {
debug_assert!(self.max >= min);
self.min = min;
self
}
pub fn max(mut self, max: N) -> Self {
debug_assert!(self.min <= max);
self.max = max;
self
}
}
impl<S, N> CommandArgument<S, N> for NumberArgument<N, S>
where
N: PartialOrd,
{
fn parse<'a>(&self, _source: S, input: &'a str) -> nom::IResult<&'a str, N, CommandError<'a>> {
let (input, out) = (self.parse)(input)?;
if out > self.max || out < self.min {
Err(nom::Err::Failure(CommandError::from_external_error(input, ErrorKind::MapRes, CmdErrorKind::OutOfBounds)))
} else {
Ok((input, out))
}
}
}
impl<E, N, S> Then<E> for NumberArgument<N, S> {
type Output = CommandThen<Self, E, N, S>;
fn then(self, executor: E) -> Self::Output {
CommandThen {
argument: self,
executor,
output: PhantomData,
source: PhantomData,
}
}
}
impl<N, S> IntoMultipleUsage for NumberArgument<N, S> {
type Item = <[&'static str; 3] as IntoMultipleUsage>::Item;
fn usage_gen(&self) -> Self::Item { self.usage_child().usage_gen() }
}
impl<N, S> ChildUsage for NumberArgument<N, S> {
type Child = [&'static str; 3];
fn usage_child(&self) -> Self::Child { ["<", self.name, ">"] }
}
impl<N, S> ArgumentMarkerDefaultImpl for NumberArgument<N, S> {}
fn decimal(input: &str) -> IResult<&str, &str, CommandError> {
recognize(preceded(opt(tag("-")), many1(terminated(one_of("0123456789"), many0(char('_'))))))(input)
}
fn float(input: &str) -> IResult<&str, &str, CommandError> {
recognize(preceded(
opt(tag("-")),
separated_pair(
many1(terminated(one_of("0123456789"), many0(char('_')))),
opt(char('.')),
opt(many1(terminated(one_of("0123456789"), many0(char('_'))))),
),
))(input)
}
macro_rules! impl_num {
($num:ty => $name:ident = $parse:ident) => {
impl_num!($num => $name = $parse + decimal);
};
($num:ty => $name:ident = $parse:ident + $num_parse:ident) => {
#[doc = stringify!(Create a $num argument parser.)]
pub fn $name<S>(name: &'static str) -> NumberArgument<$num, S> {
NumberArgument {
name,
min: <$num>::MIN,
max: <$num>::MAX,
parse: $parse,
source: PhantomData,
}
}
fn $parse(input: &str) -> IResult<&str, $num, CommandError> {
let (input, number) = $num_parse(input)?;
match ::std::str::FromStr::from_str(number) {
Err(e) => Err(nom::Err::Failure(CommandError::from_external_error(input, ::nom::error::ErrorKind::MapRes, e))),
Ok(v) => Ok((input, v)),
}
}
};
}
impl_num!(i8 => integer_i8 = parse_i8);
impl_num!(i16 => integer_i16 = parse_i16);
impl_num!(i32 => integer_i32 = parse_i32);
impl_num!(i64 => integer_i64 = parse_i64);
impl_num!(u8 => integer_u8 = parse_u8);
impl_num!(u16 => integer_u16 = parse_u16);
impl_num!(u32 => integer_u32 = parse_u32);
impl_num!(u64 => integer_u64 = parse_u64);
impl_num!(f32 => float_32 = parse_f32 + float);
impl_num!(f64 => float_64 = parse_f64 + float);