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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use super::*;
impl<'i, T> Debug for ParseResult<'i, T>
where
T: Debug,
{
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
ParseResult::Pending(s, v) => f
.debug_struct("Pending")
.field("value", v)
.field("rest_text", &s.residual)
.field("start_offset", &s.start_offset)
.field("stop_reason", &s.stop_reason)
.finish(),
ParseResult::Stop(e) => f.debug_struct("Stop").field("reason", e).finish(),
}
}
}
impl<'i, T> ParseResult<'i, T> {
/// Map inner value
///
/// ```
/// # use pex::{ParseResult, ParseState};
/// let state = ParseState::new("hello");
/// let result = state.finish(());
/// assert_eq!(result.map_inner(|_| 1), ParseResult::Pending(state, 1));
/// ```
#[inline(always)]
pub fn map_inner<F, U>(self, mut f: F) -> ParseResult<'i, U>
where
F: FnMut(T) -> U,
{
match self {
Self::Pending(state, value) => ParseResult::Pending(state, f(value)),
Self::Stop(reason) => ParseResult::Stop(reason),
}
}
/// Map inner value into target
///
/// ```
/// # use pex::{ParseResult, ParseState};
/// let state = ParseState::new("hello");
/// let result = state.finish(());
/// assert_eq!(result.map_value(1), ParseResult::Pending(state, 1));
/// ```
#[inline(always)]
pub fn map_value<U>(self, value: U) -> ParseResult<'i, U> {
match self {
Self::Pending(state, _) => ParseResult::Pending(state, value),
Self::Stop(reason) => ParseResult::Stop(reason),
}
}
/// Map inner value into target
///
/// ```
/// # use pex::{ParseResult, ParseState};
/// let state = ParseState::new("hello");
/// let result = state.finish(());
/// assert_eq!(result.map_inner(|_| 1), ParseResult::Pending(state, 1));
/// ```
#[inline(always)]
pub fn map_into<U>(self) -> ParseResult<'i, U>
where
T: Into<U>,
{
self.map_inner(Into::into)
}
/// Dispatch branch events based on the result
///
/// # Examples
///
/// ```
/// # use pex::{ParseResult, ParseState};
/// let state = ParseState::new("hello");
/// let result = state.finish(());
/// result.dispatch(|ok| println!("ok: {:?}", ok), |fail| println!("fail: {:?}", fail));
/// ```
#[inline(always)]
pub fn dispatch<F, G>(self, mut ok: F, mut fail: G) -> Self
where
F: FnMut(ParseState),
G: FnMut(StopBecause),
{
match &self {
ParseResult::Pending(data, _) => ok(*data),
ParseResult::Stop(stop) => fail(*stop),
}
self
}
/// Convert a parse [`Result`](Self) to a std [`Result`]
///
/// # Examples
///
/// ```
/// # use pex::{ParseResult, ParseState};
/// let state = ParseState::new("hello");
/// let result = state.finish(());
/// assert_eq!(result.as_result(), Ok((state, ())));
/// ```
#[inline(always)]
#[allow(clippy::wrong_self_convention)]
pub fn as_result(self) -> Result<Parsed<'i, T>, StopBecause> {
match self {
Self::Pending(state, value) => Ok((state, value)),
Self::Stop(reason) => Err(reason),
}
}
/// Returns the contained [`ParseResult::Pending`] value, drop current state, panic if state reach stopped.
///
///
/// # Examples
///
/// ```
/// # use pex::{ParseResult, ParseState};
/// let state = ParseState::new("hello");
/// let result = state.finish(());
/// assert_eq!(result.as_result(), Ok((state, ())));
/// ```
#[track_caller]
#[inline(always)]
pub fn unwrap(self) -> T {
match self {
ParseResult::Pending(_, v) => v,
ParseResult::Stop(e) => panic!("{e:?}"),
}
}
/// Check whether a match is successful, note that an empty match is always successful.
///
/// # Examples
///
/// ```
/// # use pex::{ParseResult, ParseState};
/// # use pex::helpers::{decimal_string, quotation_pair};
/// let state = ParseState::new("'hello'");
/// assert!(state.match_fn(|s| quotation_pair(s, '"', '"')).is_failure());
/// assert!(state.match_fn(decimal_string).is_failure());
/// ```
#[inline(always)]
pub fn is_success(&self) -> bool {
match self {
Self::Pending(..) => true,
Self::Stop(..) => false,
}
}
/// Check whether a match is failed, note that an empty match never fails.
///
/// # Examples
///
/// ```
/// # use pex::{ParseResult, ParseState};
/// # use pex::helpers::{decimal_string, quotation_pair};
/// let state = ParseState::new("'hello'");
/// assert!(!state.match_fn(|s| quotation_pair(s, '\'', '\'')).is_failure());
/// assert!(state.match_fn(decimal_string).is_failure());
/// ```
#[inline(always)]
pub fn is_failure(&self) -> bool {
match self {
Self::Pending(..) => false,
Self::Stop(..) => true,
}
}
}