-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(iterator-ext): more fun with Token-Iterators
* attaches constructor utilities to all `Iterator<Item=Token>` to ease using them. It's similar to (former) `IteratorExt` which would put `chain()` and `nth` for instance. Closes #3
- Loading branch information
Showing
3 changed files
with
36 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use super::{Token, FilterTypedKeyValuePairs, TokenType, TokenReader}; | ||
|
||
|
||
/// Applies convenience constructors to all `Iterator<Item=Token>` types | ||
pub trait IteratorExt: Iterator<Item=Token> { | ||
|
||
/// Returns an Iterator which filters key=value pairs, if `value.kind` matches | ||
/// the given `token_type`. | ||
/// | ||
/// It is useful, for example, to get rid of `null` values on a lexical level. | ||
fn filter_key_value_by_type(self, token_type: TokenType) -> FilterTypedKeyValuePairs<Self> | ||
where Self: Sized { | ||
FilterTypedKeyValuePairs::new(self, token_type) | ||
} | ||
|
||
/// Returns a `TokenReader` to produce a byte stream from `Token` instances | ||
/// | ||
/// # Arguments | ||
/// * `source` - an optional, original string from which the tokens were | ||
/// generated. This offers the best performance when | ||
/// serializing tokens, as they can refer to their original | ||
/// `&str` slice. | ||
fn reader<'a>(self, source: Option<&'a str>) -> TokenReader<Self> | ||
where Self: Sized { | ||
TokenReader::new(self, source) | ||
} | ||
} | ||
|
||
impl<T: Iterator<Item=Token>> IteratorExt for T {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters