Skip to content

Commit

Permalink
Add Token::is_next method (#82)
Browse files Browse the repository at this point in the history
* adds `Token::is_next method
  • Loading branch information
asmello authored Oct 21, 2024
1 parent c6ceb4b commit 9a15d91
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Adds method `into_buf` for `Box<Pointer>` and `impl From<PathBuf> for Box<Pointer>`.
- Adds unsafe associated methods `Pointer::new_unchecked` and `PointerBuf::new_unchecked` for
external zero-cost construction.
- Adds `Token::is_next` for checking if a token represents the `-` character.

### Changed

Expand Down
19 changes: 19 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ impl<'a> Token<'a> {
pub fn to_index(&self) -> Result<Index, ParseIndexError> {
self.try_into()
}

/// Returns if the `Token` is `-`, which stands for the next array index.
///
/// See also [`Self::to_index`].
pub fn is_next(&self) -> bool {
matches!(self.to_index(), Ok(Index::Next))
}
}

macro_rules! impl_from_num {
Expand Down Expand Up @@ -491,4 +498,16 @@ mod tests {
]
});
}

#[test]
fn is_next() {
let token = Token::new("-");
assert!(token.is_next());
let token = Token::new("0");
assert!(!token.is_next());
let token = Token::new("a");
assert!(!token.is_next());
let token = Token::new("");
assert!(!token.is_next());
}
}

0 comments on commit 9a15d91

Please sign in to comment.