1 //! Custom input types
2 //!
3 
4 use traits::{AsBytes, AtEof, Compare, CompareResult, FindSubstring, FindToken, InputIter, InputLength, InputTake, Offset, ParseTo, Slice};
5 
6 #[cfg(feature = "alloc")]
7 use traits::ExtendInto;
8 
9 use lib::std::iter::{Enumerate, Map};
10 use lib::std::ops::{Deref, Range, RangeFrom, RangeFull, RangeTo};
11 use lib::std::slice::Iter;
12 use lib::std::str::{self, CharIndices, Chars, FromStr};
13 use lib::std::convert::From;
14 use lib::std::fmt::{Display, Formatter, Result};
15 #[cfg(feature = "alloc")]
16 use lib::std::string::String;
17 
18 /// Holds a complete String, for which the `at_eof` method always returns true
19 ///
20 /// This means that this input type will completely avoid nom's streaming features
21 /// and `Incomplete` results.
22 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
23 pub struct CompleteStr<'a>(pub &'a str);
24 
25 impl<'a> From<&'a str> for CompleteStr<'a> {
from(src: &'a str) -> Self26   fn from(src: &'a str) -> Self {
27     CompleteStr(src)
28   }
29 }
30 
31 impl<'a, 'b> From<&'b &'a str> for CompleteStr<'a> {
from(src: &'b &'a str) -> Self32   fn from(src: &'b &'a str) -> Self {
33     CompleteStr(*src)
34   }
35 }
36 
37 impl<'a> Display for CompleteStr<'a> {
fmt(&self, f: &mut Formatter) -> Result38   fn fmt(&self, f: &mut Formatter) -> Result {
39     self.0.fmt(f)
40   }
41 }
42 
43 impl<'a> AsRef<str> for CompleteStr<'a> {
as_ref(&self) -> &str44   fn as_ref(&self) -> &str {
45     self.0
46   }
47 }
48 
49 impl<'a> Deref for CompleteStr<'a> {
50   type Target = &'a str;
51 
52   #[inline]
deref(&self) -> &Self::Target53   fn deref(&self) -> &Self::Target {
54     &self.0
55   }
56 }
57 
58 impl<'a> AtEof for CompleteStr<'a> {
59   #[inline]
at_eof(&self) -> bool60   fn at_eof(&self) -> bool {
61     true
62   }
63 }
64 
65 impl<'a> Slice<Range<usize>> for CompleteStr<'a> {
66   #[inline]
slice(&self, range: Range<usize>) -> Self67   fn slice(&self, range: Range<usize>) -> Self {
68     CompleteStr(self.0.slice(range))
69   }
70 }
71 
72 impl<'a> Slice<RangeTo<usize>> for CompleteStr<'a> {
73   #[inline]
slice(&self, range: RangeTo<usize>) -> Self74   fn slice(&self, range: RangeTo<usize>) -> Self {
75     CompleteStr(self.0.slice(range))
76   }
77 }
78 
79 impl<'a> Slice<RangeFrom<usize>> for CompleteStr<'a> {
80   #[inline]
slice(&self, range: RangeFrom<usize>) -> Self81   fn slice(&self, range: RangeFrom<usize>) -> Self {
82     CompleteStr(self.0.slice(range))
83   }
84 }
85 
86 impl<'a> Slice<RangeFull> for CompleteStr<'a> {
87   #[inline]
slice(&self, range: RangeFull) -> Self88   fn slice(&self, range: RangeFull) -> Self {
89     CompleteStr(self.0.slice(range))
90   }
91 }
92 
93 impl<'a> InputIter for CompleteStr<'a> {
94   type Item = char;
95   type RawItem = char;
96   type Iter = CharIndices<'a>;
97   type IterElem = Chars<'a>;
98 
iter_indices(&self) -> Self::Iter99   fn iter_indices(&self) -> Self::Iter {
100     self.0.iter_indices()
101   }
iter_elements(&self) -> Self::IterElem102   fn iter_elements(&self) -> Self::IterElem {
103     self.0.iter_elements()
104   }
position<P>(&self, predicate: P) -> Option<usize> where P: Fn(Self::RawItem) -> bool,105   fn position<P>(&self, predicate: P) -> Option<usize>
106   where
107     P: Fn(Self::RawItem) -> bool,
108   {
109     self.0.position(predicate)
110   }
slice_index(&self, count: usize) -> Option<usize>111   fn slice_index(&self, count: usize) -> Option<usize> {
112     self.0.slice_index(count)
113   }
114 }
115 
116 impl<'a> InputTake for CompleteStr<'a> {
take(&self, count: usize) -> Self117   fn take(&self, count: usize) -> Self {
118     CompleteStr(self.0.take(count))
119   }
120 
take_split(&self, count: usize) -> (Self, Self)121   fn take_split(&self, count: usize) -> (Self, Self) {
122     let (left, right) = self.0.take_split(count);
123     (CompleteStr(left), CompleteStr(right))
124   }
125 }
126 
127 impl<'a> InputLength for CompleteStr<'a> {
input_len(&self) -> usize128   fn input_len(&self) -> usize {
129     self.0.input_len()
130   }
131 }
132 
133 impl<'a, 'b> Compare<&'b str> for CompleteStr<'a> {
compare(&self, t: &'b str) -> CompareResult134   fn compare(&self, t: &'b str) -> CompareResult {
135     self.0.compare(t)
136   }
compare_no_case(&self, t: &'b str) -> CompareResult137   fn compare_no_case(&self, t: &'b str) -> CompareResult {
138     self.0.compare_no_case(t)
139   }
140 }
141 
142 impl<'a, 'b> FindSubstring<&'b str> for CompleteStr<'a> {
find_substring(&self, substr: &'b str) -> Option<usize>143   fn find_substring(&self, substr: &'b str) -> Option<usize> {
144     self.0.find_substring(substr)
145   }
146 }
147 
148 impl<'a> FindToken<char> for CompleteStr<'a> {
find_token(&self, token: char) -> bool149   fn find_token(&self, token: char) -> bool {
150     self.0.find_token(token)
151   }
152 }
153 
154 impl<'a> FindToken<u8> for CompleteStr<'a> {
find_token(&self, token: u8) -> bool155   fn find_token(&self, token: u8) -> bool {
156     self.0.find_token(token)
157   }
158 }
159 
160 impl<'a, 'b> FindToken<&'a u8> for CompleteStr<'b> {
find_token(&self, token: &u8) -> bool161   fn find_token(&self, token: &u8) -> bool {
162     self.0.find_token(token)
163   }
164 }
165 
166 impl<'a, R: FromStr> ParseTo<R> for CompleteStr<'a> {
parse_to(&self) -> Option<R>167   fn parse_to(&self) -> Option<R> {
168     self.0.parse().ok()
169   }
170 }
171 
172 impl<'a> Offset for CompleteStr<'a> {
offset(&self, second: &CompleteStr<'a>) -> usize173   fn offset(&self, second: &CompleteStr<'a>) -> usize {
174     self.0.offset(second.0)
175   }
176 }
177 
178 impl<'a> AsBytes for CompleteStr<'a> {
as_bytes(&self) -> &[u8]179   fn as_bytes(&self) -> &[u8] {
180     AsBytes::as_bytes(self.0)
181   }
182 }
183 
184 #[cfg(feature = "alloc")]
185 impl<'a> ExtendInto for CompleteStr<'a> {
186   type Item = char;
187   type Extender = String;
188 
189   #[inline]
new_builder(&self) -> String190   fn new_builder(&self) -> String {
191     String::new()
192   }
193   #[inline]
extend_into(&self, acc: &mut String)194   fn extend_into(&self, acc: &mut String) {
195     acc.extend(self.0.chars());
196   }
197 }
198 
199 /// Holds a complete byte array, for which the `at_eof` method always returns true
200 ///
201 /// This means that this input type will completely avoid nom's streaming features
202 /// and `Incomplete` results.
203 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
204 pub struct CompleteByteSlice<'a>(pub &'a [u8]);
205 
206 impl<'a> From<&'a [u8]> for CompleteByteSlice<'a> {
from(src: &'a [u8]) -> Self207   fn from(src: &'a [u8]) -> Self {
208     CompleteByteSlice(src)
209   }
210 }
211 
212 impl<'a, 'b> From<&'b &'a [u8]> for CompleteByteSlice<'a> {
from(src: &'b &'a [u8]) -> Self213   fn from(src: &'b &'a [u8]) -> Self {
214     CompleteByteSlice(*src)
215   }
216 }
217 
218 impl<'a> Deref for CompleteByteSlice<'a> {
219   type Target = &'a [u8];
220 
221   #[inline]
deref(&self) -> &Self::Target222   fn deref(&self) -> &Self::Target {
223     &self.0
224   }
225 }
226 
227 impl<'a> AtEof for CompleteByteSlice<'a> {
228   #[inline]
at_eof(&self) -> bool229   fn at_eof(&self) -> bool {
230     true
231   }
232 }
233 
234 impl<'a> Slice<Range<usize>> for CompleteByteSlice<'a> {
235   #[inline]
slice(&self, range: Range<usize>) -> Self236   fn slice(&self, range: Range<usize>) -> Self {
237     CompleteByteSlice(self.0.slice(range))
238   }
239 }
240 
241 impl<'a> Slice<RangeTo<usize>> for CompleteByteSlice<'a> {
242   #[inline]
slice(&self, range: RangeTo<usize>) -> Self243   fn slice(&self, range: RangeTo<usize>) -> Self {
244     CompleteByteSlice(self.0.slice(range))
245   }
246 }
247 
248 impl<'a> Slice<RangeFrom<usize>> for CompleteByteSlice<'a> {
249   #[inline]
slice(&self, range: RangeFrom<usize>) -> Self250   fn slice(&self, range: RangeFrom<usize>) -> Self {
251     CompleteByteSlice(self.0.slice(range))
252   }
253 }
254 
255 impl<'a> Slice<RangeFull> for CompleteByteSlice<'a> {
256   #[inline]
slice(&self, range: RangeFull) -> Self257   fn slice(&self, range: RangeFull) -> Self {
258     CompleteByteSlice(self.0.slice(range))
259   }
260 }
261 
262 impl<'a> InputIter for CompleteByteSlice<'a> {
263   type Item = u8;
264   type RawItem = u8;
265   type Iter = Enumerate<Self::IterElem>;
266   type IterElem = Map<Iter<'a, Self::Item>, fn(&u8) -> u8>; //Iter<'a, Self::RawItem>;
267 
iter_indices(&self) -> Self::Iter268   fn iter_indices(&self) -> Self::Iter {
269     self.0.iter_indices()
270   }
iter_elements(&self) -> Self::IterElem271   fn iter_elements(&self) -> Self::IterElem {
272     self.0.iter_elements()
273   }
position<P>(&self, predicate: P) -> Option<usize> where P: Fn(Self::RawItem) -> bool,274   fn position<P>(&self, predicate: P) -> Option<usize>
275   where
276     P: Fn(Self::RawItem) -> bool,
277   {
278     self.0.position(predicate)
279   }
slice_index(&self, count: usize) -> Option<usize>280   fn slice_index(&self, count: usize) -> Option<usize> {
281     self.0.slice_index(count)
282   }
283 }
284 
285 impl<'a> InputTake for CompleteByteSlice<'a> {
take(&self, count: usize) -> Self286   fn take(&self, count: usize) -> Self {
287     CompleteByteSlice(self.0.take(count))
288   }
289 
take_split(&self, count: usize) -> (Self, Self)290   fn take_split(&self, count: usize) -> (Self, Self) {
291     let (left, right) = self.0.take_split(count);
292     (CompleteByteSlice(left), CompleteByteSlice(right))
293   }
294 }
295 
296 impl<'a> InputLength for CompleteByteSlice<'a> {
input_len(&self) -> usize297   fn input_len(&self) -> usize {
298     self.0.input_len()
299   }
300 }
301 
302 impl<'a, 'b> Compare<&'b [u8]> for CompleteByteSlice<'a> {
compare(&self, t: &'b [u8]) -> CompareResult303   fn compare(&self, t: &'b [u8]) -> CompareResult {
304     self.0.compare(t)
305   }
compare_no_case(&self, t: &'b [u8]) -> CompareResult306   fn compare_no_case(&self, t: &'b [u8]) -> CompareResult {
307     self.0.compare_no_case(t)
308   }
309 }
310 
311 impl<'a, 'b> Compare<&'b str> for CompleteByteSlice<'a> {
compare(&self, t: &'b str) -> CompareResult312   fn compare(&self, t: &'b str) -> CompareResult {
313     self.0.compare(t)
314   }
compare_no_case(&self, t: &'b str) -> CompareResult315   fn compare_no_case(&self, t: &'b str) -> CompareResult {
316     self.0.compare_no_case(t)
317   }
318 }
319 
320 impl<'a, 'b> FindSubstring<&'b [u8]> for CompleteByteSlice<'a> {
find_substring(&self, substr: &'b [u8]) -> Option<usize>321   fn find_substring(&self, substr: &'b [u8]) -> Option<usize> {
322     self.0.find_substring(substr)
323   }
324 }
325 
326 impl<'a, 'b> FindSubstring<&'b str> for CompleteByteSlice<'a> {
find_substring(&self, substr: &'b str) -> Option<usize>327   fn find_substring(&self, substr: &'b str) -> Option<usize> {
328     self.0.find_substring(substr)
329   }
330 }
331 
332 impl<'a> FindToken<char> for CompleteByteSlice<'a> {
find_token(&self, token: char) -> bool333   fn find_token(&self, token: char) -> bool {
334     self.0.find_token(token)
335   }
336 }
337 
338 impl<'a> FindToken<u8> for CompleteByteSlice<'a> {
find_token(&self, token: u8) -> bool339   fn find_token(&self, token: u8) -> bool {
340     self.0.find_token(token)
341   }
342 }
343 
344 impl<'a, 'b> FindToken<&'a u8> for CompleteByteSlice<'b> {
find_token(&self, token: &u8) -> bool345   fn find_token(&self, token: &u8) -> bool {
346     self.0.find_token(token)
347   }
348 }
349 
350 impl<'a, R: FromStr> ParseTo<R> for CompleteByteSlice<'a> {
parse_to(&self) -> Option<R>351   fn parse_to(&self) -> Option<R> {
352     self.0.parse_to()
353   }
354 }
355 
356 impl<'a> Offset for CompleteByteSlice<'a> {
offset(&self, second: &CompleteByteSlice<'a>) -> usize357   fn offset(&self, second: &CompleteByteSlice<'a>) -> usize {
358     self.0.offset(second.0)
359   }
360 }
361 
362 impl<'a> AsBytes for CompleteByteSlice<'a> {
as_bytes(&self) -> &[u8]363   fn as_bytes(&self) -> &[u8] {
364     self.0.as_bytes()
365   }
366 }
367 
368 #[cfg(feature = "std")]
369 impl<'a> super::util::HexDisplay for CompleteByteSlice<'a> {
to_hex(&self, chunk_size: usize) -> String370   fn to_hex(&self, chunk_size: usize) -> String {
371     self.0.to_hex(chunk_size)
372   }
373 
to_hex_from(&self, chunk_size: usize, from: usize) -> String374   fn to_hex_from(&self, chunk_size: usize, from: usize) -> String {
375     self.0.to_hex_from(chunk_size, from)
376   }
377 }
378 
379 #[derive(Clone, Copy, Debug, PartialEq, Hash)]
380 pub struct Input<T> {
381   pub inner: T,
382   pub at_eof: bool,
383 }
384 
385 impl<T> AtEof for Input<T> {
at_eof(&self) -> bool386   fn at_eof(&self) -> bool {
387     self.at_eof
388   }
389 }
390 
391 impl<T: Slice<Range<usize>>> Slice<Range<usize>> for Input<T> {
slice(&self, range: Range<usize>) -> Self392   fn slice(&self, range: Range<usize>) -> Self {
393     Input {
394       inner: self.inner.slice(range),
395       at_eof: self.at_eof,
396     }
397   }
398 }
399 
400 impl<T: Slice<RangeTo<usize>>> Slice<RangeTo<usize>> for Input<T> {
slice(&self, range: RangeTo<usize>) -> Self401   fn slice(&self, range: RangeTo<usize>) -> Self {
402     Input {
403       inner: self.inner.slice(range),
404       at_eof: self.at_eof,
405     }
406   }
407 }
408 
409 impl<T: Slice<RangeFrom<usize>>> Slice<RangeFrom<usize>> for Input<T> {
slice(&self, range: RangeFrom<usize>) -> Self410   fn slice(&self, range: RangeFrom<usize>) -> Self {
411     Input {
412       inner: self.inner.slice(range),
413       at_eof: self.at_eof,
414     }
415   }
416 }
417 
418 impl<T: Slice<RangeFull>> Slice<RangeFull> for Input<T> {
slice(&self, range: RangeFull) -> Self419   fn slice(&self, range: RangeFull) -> Self {
420     Input {
421       inner: self.inner.slice(range),
422       at_eof: self.at_eof,
423     }
424   }
425 }
426 
427 impl<T: InputIter> InputIter for Input<T> {
428   type Item = <T as InputIter>::Item;
429   type RawItem = <T as InputIter>::RawItem;
430   type Iter = <T as InputIter>::Iter;
431   type IterElem = <T as InputIter>::IterElem;
432 
iter_indices(&self) -> Self::Iter433   fn iter_indices(&self) -> Self::Iter {
434     self.inner.iter_indices()
435   }
iter_elements(&self) -> Self::IterElem436   fn iter_elements(&self) -> Self::IterElem {
437     self.inner.iter_elements()
438   }
position<P>(&self, predicate: P) -> Option<usize> where P: Fn(Self::RawItem) -> bool,439   fn position<P>(&self, predicate: P) -> Option<usize>
440   where
441     P: Fn(Self::RawItem) -> bool,
442   {
443     self.inner.position(predicate)
444   }
slice_index(&self, count: usize) -> Option<usize>445   fn slice_index(&self, count: usize) -> Option<usize> {
446     self.inner.slice_index(count)
447   }
448 }
449 
450 impl<T: InputTake> InputTake for Input<T> {
take(&self, count: usize) -> Self451   fn take(&self, count: usize) -> Self {
452     Input {
453       inner: self.inner.take(count),
454       at_eof: self.at_eof,
455     }
456   }
457 
take_split(&self, count: usize) -> (Self, Self)458   fn take_split(&self, count: usize) -> (Self, Self) {
459     let (left, right) = self.inner.take_split(count);
460     (
461       Input {
462         inner: left,
463         at_eof: self.at_eof,
464       },
465       Input {
466         inner: right,
467         at_eof: self.at_eof,
468       },
469     )
470   }
471 }
472 
473 impl<T: InputLength> InputLength for Input<T> {
input_len(&self) -> usize474   fn input_len(&self) -> usize {
475     self.inner.input_len()
476   }
477 }
478 
479 impl<'b, T: Compare<&'b str>> Compare<&'b str> for Input<T> {
compare(&self, t: &'b str) -> CompareResult480   fn compare(&self, t: &'b str) -> CompareResult {
481     self.inner.compare(t)
482   }
compare_no_case(&self, t: &'b str) -> CompareResult483   fn compare_no_case(&self, t: &'b str) -> CompareResult {
484     self.inner.compare_no_case(t)
485   }
486 }
487 
488 impl<'b, T: FindSubstring<&'b str>> FindSubstring<&'b str> for Input<T> {
find_substring(&self, substr: &'b str) -> Option<usize>489   fn find_substring(&self, substr: &'b str) -> Option<usize> {
490     self.inner.find_substring(substr)
491   }
492 }
493 
494 impl<T: FindToken<char>> FindToken<char> for Input<T> {
find_token(&self, token: char) -> bool495   fn find_token(&self, token: char) -> bool {
496     self.inner.find_token(token)
497   }
498 }
499 
500 impl<T: FindToken<u8>> FindToken<u8> for Input<T> {
find_token(&self, token: u8) -> bool501   fn find_token(&self, token: u8) -> bool {
502     self.inner.find_token(token)
503   }
504 }
505 
506 impl<'a, T: FindToken<&'a u8>> FindToken<&'a u8> for Input<T> {
find_token(&self, token: &'a u8) -> bool507   fn find_token(&self, token: &'a u8) -> bool {
508     self.inner.find_token(token)
509   }
510 }
511 
512 impl<'a, R: FromStr, T: ParseTo<R>> ParseTo<R> for Input<T> {
parse_to(&self) -> Option<R>513   fn parse_to(&self) -> Option<R> {
514     self.inner.parse_to()
515   }
516 }
517 
518 impl<T: Offset> Offset for Input<T> {
offset(&self, second: &Input<T>) -> usize519   fn offset(&self, second: &Input<T>) -> usize {
520     self.inner.offset(&second.inner)
521   }
522 }
523 
524 impl<T: AsBytes> AsBytes for Input<T> {
as_bytes(&self) -> &[u8]525   fn as_bytes(&self) -> &[u8] {
526     AsBytes::as_bytes(&self.inner)
527   }
528 }
529