json_tools/iter_ext.rs
1use super::{FilterTypedKeyValuePairs, Token, TokenReader, TokenType};
2
3/// Applies convenience constructors to all `Iterator<Item=Token>` types
4pub trait IteratorExt: Iterator<Item = Token> {
5 /// Returns an Iterator which filters key=value pairs, if `value.kind` matches
6 /// the given `token_type`.
7 ///
8 /// It is useful, for example, to get rid of `null` values on a lexical level.
9 fn filter_key_value_by_type(self, token_type: TokenType) -> FilterTypedKeyValuePairs<Self>
10 where
11 Self: Sized,
12 {
13 FilterTypedKeyValuePairs::new(self, token_type)
14 }
15
16 /// Returns a `TokenReader` to produce a byte stream from `Token` instances
17 ///
18 /// # Arguments
19 /// * `source` - an optional, original string from which the tokens were
20 /// generated. This offers the best performance when
21 /// serializing tokens, as they can refer to their original
22 /// `&str` slice.
23 fn reader(self, source: Option<&str>) -> TokenReader<Self>
24 where
25 Self: Sized,
26 {
27 TokenReader::new(self, source)
28 }
29}
30
31impl<T: Iterator<Item = Token>> IteratorExt for T {}