sequoia_openpgp/message/
lexer.rs1use std::fmt;
2
3pub(crate) type LexerItem<Tok, Loc, Error>
8 = ::std::result::Result<(Loc, Tok, Loc), Error>;
9
10#[non_exhaustive]
12#[derive(Debug, Clone, Copy, PartialEq)]
13pub enum Token {
14 Literal,
16 CompressedData,
18
19 SKESK,
21 PKESK,
23 SEIPv1,
25 SEIPv2,
27 MDC,
29 AED,
31
32 OPS,
34 SIG,
36
37 Pop,
40
41 OpaqueContent,
43}
44assert_send_and_sync!(Token);
45
46impl fmt::Display for Token {
47 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48 write!(f, "{:?}", self)
49 }
50}
51
52#[derive(Debug, Clone)]
53pub enum LexicalError {
54 }
56assert_send_and_sync!(LexicalError);
57
58impl fmt::Display for LexicalError {
59 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60 write!(f, "{:?}", self)
61 }
62}
63
64pub(crate) struct Lexer<'input> {
65 iter: Box<dyn Iterator<Item=(usize, &'input Token)> + 'input>,
66}
67
68impl<'input> Iterator for Lexer<'input> {
69 type Item = LexerItem<Token, usize, LexicalError>;
70
71 fn next(&mut self) -> Option<Self::Item> {
72 let n = self.iter.next().map(|(pos, tok)| (pos, *tok));
73 if let Some((pos, tok)) = n {
74 Some(Ok((pos, tok, pos)))
75 } else {
76 None
77 }
78 }
79}
80
81impl<'input> Lexer<'input> {
82 pub(crate) fn from_tokens(raw: &'input [Token]) -> Self {
84 Lexer {
85 iter: Box::new(raw.iter().enumerate())
86 }
87 }
88}