1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/// A trait for tokens that can be compared for equivalence against a reference.
/// A helper trait for ergonomically requiring specific token shapes.
///
/// `Require` is intended for tiny wrappers (e.g., `Dot`, `Comma`, `ParenOpen`) that want a
/// `try_into`-style API without consuming the token stream. Implementors typically return
/// `Ok(output)` when the token matches the desired pattern, or `Err(Self::Err)` to hand the
/// original token (or a custom error type) back to the caller so other logic can handle it.
///
/// ## Example
///
/// ```rust
/// use tokit::{Require, IdentifierToken};
///
/// #[derive(Debug, Clone)]
/// pub enum Punct {
/// Dot,
/// Comma,
/// Other(String),
/// }
///
/// #[derive(Debug, Clone)]
/// pub struct Dot(pub Punct);
///
/// impl Require<Dot> for Punct {
/// type Err = Self;
///
/// fn require(self) -> Result<Dot, Self::Err> {
/// match &self {
/// Punct::Dot => Ok(Dot(Self::Dot)),
/// _ => Err(self),
/// }
/// }
/// }
/// ```