/rust/registry/src/index.crates.io-1949cf8c6b5b557f/combine-4.6.7/src/stream/span.rs
Line | Count | Source |
1 | | use crate::lib::marker::PhantomData; |
2 | | |
3 | | use crate::{ |
4 | | error::{ParseErrorInto, ParseResult, StreamErrorInto}, |
5 | | stream::{ResetStream, StreamErrorFor}, |
6 | | Positioned, RangeStream, RangeStreamOnce, StreamOnce, |
7 | | }; |
8 | | |
9 | | #[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)] |
10 | | pub struct Span<P> { |
11 | | pub start: P, |
12 | | pub end: P, |
13 | | } |
14 | | |
15 | | impl<P> From<P> for Span<P> |
16 | | where |
17 | | P: Clone, |
18 | | { |
19 | | #[inline] |
20 | 0 | fn from(p: P) -> Self { |
21 | 0 | Self { |
22 | 0 | start: p.clone(), |
23 | 0 | end: p, |
24 | 0 | } |
25 | 0 | } |
26 | | } |
27 | | |
28 | | impl<P> Span<P> { |
29 | 0 | pub fn map<Q>(self, mut f: impl FnMut(P) -> Q) -> Span<Q> { |
30 | 0 | Span { |
31 | 0 | start: f(self.start), |
32 | 0 | end: f(self.end), |
33 | 0 | } |
34 | 0 | } |
35 | | } |
36 | | |
37 | | #[derive(PartialEq, Eq, Copy, Clone, Debug)] |
38 | | pub struct Stream<S, E>(pub S, PhantomData<fn(E) -> E>); |
39 | | |
40 | | impl<S, E> From<S> for Stream<S, E> { |
41 | 0 | fn from(stream: S) -> Self { |
42 | 0 | Stream(stream, PhantomData) |
43 | 0 | } |
44 | | } |
45 | | |
46 | | impl<S, E> ResetStream for Stream<S, E> |
47 | | where |
48 | | S: ResetStream + Positioned, |
49 | | S::Token: PartialEq, |
50 | | S::Range: PartialEq, |
51 | | E: crate::error::ParseError<S::Token, S::Range, Span<S::Position>>, |
52 | | S::Error: ParseErrorInto<S::Token, S::Range, S::Position>, |
53 | | <S::Error as crate::error::ParseError<S::Token, S::Range, S::Position>>::StreamError: |
54 | | StreamErrorInto<S::Token, S::Range>, |
55 | | { |
56 | | type Checkpoint = S::Checkpoint; |
57 | | |
58 | | #[inline] |
59 | 0 | fn checkpoint(&self) -> Self::Checkpoint { |
60 | 0 | self.0.checkpoint() |
61 | 0 | } |
62 | | |
63 | | #[inline] |
64 | 0 | fn reset(&mut self, checkpoint: Self::Checkpoint) -> Result<(), Self::Error> { |
65 | 0 | self.0 |
66 | 0 | .reset(checkpoint) |
67 | 0 | .map_err(ParseErrorInto::into_other_error) |
68 | 0 | } |
69 | | } |
70 | | |
71 | | impl<S, E> StreamOnce for Stream<S, E> |
72 | | where |
73 | | S: StreamOnce + Positioned, |
74 | | S::Token: PartialEq, |
75 | | S::Range: PartialEq, |
76 | | E: crate::error::ParseError<S::Token, S::Range, Span<S::Position>>, |
77 | | S::Error: ParseErrorInto<S::Token, S::Range, S::Position>, |
78 | | <S::Error as crate::error::ParseError<S::Token, S::Range, S::Position>>::StreamError: |
79 | | StreamErrorInto<S::Token, S::Range>, |
80 | | { |
81 | | type Token = S::Token; |
82 | | type Range = S::Range; |
83 | | type Position = Span<S::Position>; |
84 | | type Error = E; |
85 | | |
86 | | #[inline] |
87 | 0 | fn uncons(&mut self) -> Result<Self::Token, StreamErrorFor<Self>> { |
88 | 0 | self.0.uncons().map_err(StreamErrorInto::into_other_error) |
89 | 0 | } |
90 | | |
91 | | #[inline] |
92 | 0 | fn is_partial(&self) -> bool { |
93 | 0 | self.0.is_partial() |
94 | 0 | } |
95 | | } |
96 | | |
97 | | impl<S, E> RangeStreamOnce for Stream<S, E> |
98 | | where |
99 | | S: RangeStream, |
100 | | S::Token: PartialEq, |
101 | | S::Range: PartialEq, |
102 | | E: crate::error::ParseError<S::Token, S::Range, Span<S::Position>>, |
103 | | S::Error: ParseErrorInto<S::Token, S::Range, S::Position>, |
104 | | <S::Error as crate::error::ParseError<S::Token, S::Range, S::Position>>::StreamError: |
105 | | StreamErrorInto<S::Token, S::Range>, |
106 | | { |
107 | | #[inline] |
108 | 0 | fn uncons_range(&mut self, size: usize) -> Result<Self::Range, StreamErrorFor<Self>> { |
109 | 0 | self.0 |
110 | 0 | .uncons_range(size) |
111 | 0 | .map_err(StreamErrorInto::into_other_error) |
112 | 0 | } |
113 | | |
114 | | #[inline] |
115 | 0 | fn uncons_while<F>(&mut self, f: F) -> Result<Self::Range, StreamErrorFor<Self>> |
116 | 0 | where |
117 | 0 | F: FnMut(Self::Token) -> bool, |
118 | | { |
119 | 0 | self.0 |
120 | 0 | .uncons_while(f) |
121 | 0 | .map_err(StreamErrorInto::into_other_error) |
122 | 0 | } |
123 | | |
124 | | #[inline] |
125 | 0 | fn uncons_while1<F>(&mut self, f: F) -> ParseResult<Self::Range, StreamErrorFor<Self>> |
126 | 0 | where |
127 | 0 | F: FnMut(Self::Token) -> bool, |
128 | | { |
129 | 0 | self.0 |
130 | 0 | .uncons_while1(f) |
131 | 0 | .map_err(StreamErrorInto::into_other_error) |
132 | 0 | } |
133 | | |
134 | | #[inline] |
135 | 0 | fn distance(&self, end: &Self::Checkpoint) -> usize { |
136 | 0 | self.0.distance(end) |
137 | 0 | } |
138 | | |
139 | 0 | fn range(&self) -> Self::Range { |
140 | 0 | self.0.range() |
141 | 0 | } |
142 | | } |
143 | | |
144 | | impl<S, E> Positioned for Stream<S, E> |
145 | | where |
146 | | S: StreamOnce + Positioned, |
147 | | S::Token: PartialEq, |
148 | | S::Range: PartialEq, |
149 | | E: crate::error::ParseError<S::Token, S::Range, Span<S::Position>>, |
150 | | S::Error: ParseErrorInto<S::Token, S::Range, S::Position>, |
151 | | <S::Error as crate::error::ParseError<S::Token, S::Range, S::Position>>::StreamError: |
152 | | StreamErrorInto<S::Token, S::Range>, |
153 | | { |
154 | 0 | fn position(&self) -> Span<S::Position> { |
155 | 0 | Span::from(self.0.position()) |
156 | 0 | } |
157 | | } |