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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
use std::fmt;
use crate::Error;
use crate::Packet;
use crate::packet::Tag;
use crate::cert::bundle::{
SubkeyBundle,
UserIDBundle,
UserAttributeBundle,
UnknownBundle,
};
use crate::packet::key;
// The type of the parser's input.
//
// The parser iterators over tuples consisting of the token's starting
// position, the token itself, and the token's ending position.
pub(crate) type LexerItem<Tok, Loc, Error>
= ::std::result::Result<(Loc, Tok, Loc), Error>;
/// The components of an OpenPGP Message.
#[derive(Debug, Clone, PartialEq)]
pub enum Token {
/// A `PublicKey` packet.
PublicKey(Option<Packet>),
/// A `SecretKey` packet.
SecretKey(Option<Packet>),
/// A `PublicSubkey` packet.
PublicSubkey(Option<Packet>),
/// A `SecretSubkey` packet.
SecretSubkey(Option<Packet>),
/// A `UserID` packet.
UserID(Option<Packet>),
/// A `UserAttribute` packet.
UserAttribute(Option<Packet>),
/// A `Signature` packet.
Signature(Option<Packet>),
/// A `Trust` packet
Trust(Option<Packet>),
/// An `Unknown` packet.
Unknown(Tag, Option<Packet>),
}
assert_send_and_sync!(Token);
/// Internal data-structure used by the parser.
///
/// Due to the way the parser code is generated, it must be marked as
/// public. But, since this module is not public, it will not
/// actually be exported to used of the library.
pub enum Component {
SubkeyBundle(SubkeyBundle<key::PublicParts>),
UserIDBundle(UserIDBundle),
UserAttributeBundle(UserAttributeBundle),
UnknownBundle(UnknownBundle),
}
assert_send_and_sync!(Component);
impl<'a> From<&'a Token> for Tag {
fn from(token: &'a Token) -> Self {
match token {
Token::PublicKey(_) => Tag::PublicKey,
Token::SecretKey(_) => Tag::SecretKey,
Token::PublicSubkey(_) => Tag::PublicSubkey,
Token::SecretSubkey(_) => Tag::SecretSubkey,
Token::UserID(_) => Tag::UserID,
Token::UserAttribute(_) => Tag::UserAttribute,
Token::Signature(_) => Tag::Signature,
Token::Trust(_) => Tag::Trust,
Token::Unknown(tag, _) => *tag,
}
}
}
impl From<Token> for Tag {
fn from(token: Token) -> Self {
(&token).into()
}
}
impl From<Token> for Option<Packet> {
fn from(token: Token) -> Self {
match token {
Token::PublicKey(p @ Some(_)) => p,
Token::SecretKey(p @ Some(_)) => p,
Token::PublicSubkey(p @ Some(_)) => p,
Token::SecretSubkey(p @ Some(_)) => p,
Token::UserID(p @ Some(_)) => p,
Token::UserAttribute(p @ Some(_)) => p,
Token::Signature(p @ Some(_)) => p,
Token::Trust(p @ Some(_)) => p,
Token::Unknown(_, p @ Some(_)) => p,
Token::PublicKey(None)
| Token::SecretKey(None)
| Token::PublicSubkey(None)
| Token::SecretSubkey(None)
| Token::UserID(None)
| Token::UserAttribute(None)
| Token::Signature(None)
| Token::Trust(None)
| Token::Unknown(_, None)
=> None,
}
}
}
impl From<Packet> for Option<Token> {
fn from(p: Packet) -> Self {
match p {
p @ Packet::PublicKey(_) => Some(Token::PublicKey(Some(p))),
p @ Packet::SecretKey(_) => Some(Token::SecretKey(Some(p))),
p @ Packet::PublicSubkey(_) => Some(Token::PublicSubkey(Some(p))),
p @ Packet::SecretSubkey(_) => Some(Token::SecretSubkey(Some(p))),
p @ Packet::UserID(_) => Some(Token::UserID(Some(p))),
p @ Packet::UserAttribute(_) => Some(Token::UserAttribute(Some(p))),
p @ Packet::Signature(_) => Some(Token::Signature(Some(p))),
p @ Packet::Trust(_) => Some(Token::Trust(Some(p))),
p @ Packet::Unknown(_) => Some(Token::Unknown(p.tag(), Some(p))),
_ => None,
}
}
}
impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
pub(crate) struct Lexer<'input> {
iter: Box<dyn Iterator<Item=(usize, &'input Token)> + 'input>,
}
impl<'input> Iterator for Lexer<'input> {
type Item = LexerItem<Token, usize, Error>;
fn next(&mut self) -> Option<Self::Item> {
let n = self.iter.next().map(|(pos, tok)| (pos, tok.clone()));
if let Some((pos, tok)) = n {
Some(Ok((pos, tok, pos)))
} else {
None
}
}
}
impl<'input> Lexer<'input> {
/// Uses a raw sequence of tokens as input to the parser.
pub(crate) fn from_tokens(raw: &'input [Token]) -> Self {
Lexer {
iter: Box::new(raw.iter().enumerate())
}
}
}
|