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
use crate;
use PhantomData;
/// Gets a type that parses `Self` with a `parse_with` method.
///
/// Implementing this trait allows parsing a type with the [`parse_type`] macro.
///
/// # Implementing this trait
///
/// You can implement this trait like this:
/// ```rust
/// # struct SomeType;
/// # struct SomeParser;
/// # use konst::parsing::HasParser;
/// impl HasParser for SomeType {
/// // This is usually `Self` for user-defined types.
/// type Parser = SomeParser;
/// }
/// ```
/// Then `SomeParser` is expected to have a `parse_with` associated function with this signature:
/// ```rust
/// # struct SomeParser;
/// # struct This;
/// # struct SomeErrorType;
/// impl SomeParser {
/// const fn parse_with<'p>(
/// _: &mut konst::Parser<'p>
/// ) -> Result<This, SomeErrorType>
/// # { unimplemented!() }
/// }
/// ```
///
/// # Example
///
/// ```rust
/// use konst::{result, try_};
///
/// use konst::parsing::{HasParser, Parser, ParseError, parse_type};
///
/// const PAIR: Pair = {
/// let mut parser = Parser::new("100,200");
/// result::unwrap!(parse_type!(parser, Pair))
/// };
///
/// assert_eq!(PAIR, Pair(100, 200));
///
///
/// #[derive(Debug, PartialEq)]
/// struct Pair(u32, u64);
///
/// impl HasParser for Pair {
/// type Parser = Self;
/// }
///
/// impl Pair {
/// const fn parse_with<'p>(parser: &mut Parser<'p>) -> Result<Self, ParseError<'p>> {
/// let left = try_!(parse_type!(parser, u32));
/// try_!(parser.strip_prefix(','));
/// let right = try_!(parse_type!(parser, u64));
///
/// Ok(Pair(left, right))
/// }
/// }
/// ```
///
/// [`parse_type`]: crate::parsing::parse_type
/// [`HasParser::Parser`]: #associatedtype.Parser
///
////////////////////////////////////////////////////////////////////////////////
/// Parses a standard library type out of a [`Parser`],
/// determined by the `StdType` type parameter.
///
/// Note: since this uses [`Parser`], it doesn't parse the entire string,
/// it parses the starting bytes that successfully parse as the target type.
///
;
impl_std_parser!