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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//! Represents tokens used in parsing SMILES strings.
use core::ops::Range;
use crate::{
atom::Atom,
bond::{BondDescriptor, ring_num::RingNum},
};
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
/// Lightweight token category used when only syntax class matters.
pub enum TokenKind {
/// `.` separator
NonBond,
/// Atom token
Atom,
/// Explicit bond token
Bond,
/// `(`
LeftParentheses,
/// `)`
RightParentheses,
/// Ring closure token
RingClosure,
}
#[derive(Copy, Debug, PartialEq, Clone, Eq, Hash)]
/// Represents a token in a molecular formula.
pub enum Token {
/// Represented with a `.`
NonBond,
/// Parsed atom token, preserving whether it came from bracket or
/// organic-subset syntax inside the stored [`Atom`] value.
Atom(Atom),
/// The parsed bond. Single bonds are implicit between atoms if not
/// explicated as a bond, e.g. `CC` would be `C-C`.
Bond(BondDescriptor),
/// Token for left parentheses, used for branching `(`
LeftParentheses,
/// Token for right parentheses, used for ending a branch `)`
RightParentheses,
/// Ring number markers occur outside of `[]` and may be of type `%` and
/// `0-99`, and `%` may be omitted.
RingClosure(RingNum),
}
#[derive(Debug, PartialEq, Clone, Eq, Hash)]
/// A parsed token and its relative location in the string
pub struct TokenWithSpan {
/// The parsed token
token: Token,
/// The starting to ending points of the token in the string
span: Range<usize>,
}
impl TokenWithSpan {
/// Generates a new token with a specified span
///
/// # Examples
///
/// ```
/// use smiles_rs::{
/// bond::Bond,
/// token::{Token, TokenWithSpan},
/// };
///
/// let token = TokenWithSpan::new(Token::Bond(Bond::Double.into()), 1, 2);
/// assert_eq!(token.token(), Token::Bond(Bond::Double.into()));
/// ```
#[must_use]
pub fn new(token: Token, start: usize, end: usize) -> Self {
Self { token, span: start..end }
}
/// Returns the token
///
/// # Examples
///
/// ```
/// use smiles_rs::{
/// bond::Bond,
/// token::{Token, TokenWithSpan},
/// };
///
/// let token = TokenWithSpan::new(Token::Bond(Bond::Single.into()), 0, 1);
/// assert_eq!(token.token(), Token::Bond(Bond::Single.into()));
/// ```
#[must_use]
pub fn token(&self) -> Token {
self.token
}
/// Returns the token kind without the payload.
///
/// # Examples
///
/// ```
/// use smiles_rs::token::{Token, TokenKind, TokenWithSpan};
///
/// let token = TokenWithSpan::new(Token::LeftParentheses, 2, 3);
/// assert_eq!(token.token_kind(), TokenKind::LeftParentheses);
/// ```
#[must_use]
pub fn token_kind(&self) -> TokenKind {
self.token.kind()
}
/// Returns the span as [`Range`]
///
/// # Examples
///
/// ```
/// use smiles_rs::token::{Token, TokenWithSpan};
///
/// let token = TokenWithSpan::new(Token::RightParentheses, 4, 5);
/// assert_eq!(token.span(), 4..5);
/// ```
#[must_use]
pub fn span(&self) -> Range<usize> {
self.span.start..self.span.end
}
/// Returns the start of the span as [`usize`]
///
/// # Examples
///
/// ```
/// use smiles_rs::token::{Token, TokenWithSpan};
///
/// let token = TokenWithSpan::new(Token::NonBond, 6, 7);
/// assert_eq!(token.start(), 6);
/// ```
#[must_use]
pub fn start(&self) -> usize {
self.span.start
}
/// Returns the end of the span as [`usize`]
///
/// # Examples
///
/// ```
/// use smiles_rs::token::{Token, TokenWithSpan};
///
/// let token = TokenWithSpan::new(Token::NonBond, 6, 7);
/// assert_eq!(token.end(), 7);
/// ```
#[must_use]
pub fn end(&self) -> usize {
self.span.end
}
/// Returns whether the token is a bond
///
/// # Examples
///
/// ```
/// use smiles_rs::{
/// bond::Bond,
/// token::{Token, TokenWithSpan},
/// };
///
/// let bond = TokenWithSpan::new(Token::Bond(Bond::Triple.into()), 1, 2);
/// let atom = TokenWithSpan::new(Token::NonBond, 3, 4);
///
/// assert!(bond.is_bond());
/// assert!(!atom.is_bond());
/// ```
#[must_use]
pub fn is_bond(&self) -> bool {
self.token_kind() == TokenKind::Bond
}
}
impl Token {
/// Returns the payload-free category of the token.
///
/// # Examples
///
/// ```
/// use smiles_rs::{
/// bond::Bond,
/// token::{Token, TokenKind},
/// };
///
/// assert_eq!(Token::Bond(Bond::Single.into()).kind(), TokenKind::Bond);
/// assert_eq!(Token::NonBond.kind(), TokenKind::NonBond);
/// ```
#[inline]
#[must_use]
pub fn kind(self) -> TokenKind {
match self {
Self::NonBond => TokenKind::NonBond,
Self::Atom(_) => TokenKind::Atom,
Self::Bond(_) => TokenKind::Bond,
Self::LeftParentheses => TokenKind::LeftParentheses,
Self::RightParentheses => TokenKind::RightParentheses,
Self::RingClosure(_) => TokenKind::RingClosure,
}
}
}
#[cfg(test)]
mod tests {
use elements_rs::Element;
use super::{Token, TokenKind, TokenWithSpan};
use crate::{
atom::{Atom, atom_symbol::AtomSymbol},
bond::{Bond, ring_num::RingNum},
};
#[test]
fn token_variants_can_be_constructed_and_compared() {
let bracket_atom = Atom::builder().with_symbol(AtomSymbol::Element(Element::C)).build();
let organic_atom = Atom::new_organic_subset(AtomSymbol::Element(Element::O), false);
let ring_num = RingNum::try_new(12).unwrap();
let cases = [
Token::NonBond,
Token::Atom(bracket_atom),
Token::Atom(organic_atom),
Token::Bond(Bond::Double.into()),
Token::LeftParentheses,
Token::RightParentheses,
Token::RingClosure(ring_num),
];
assert_eq!(cases[0], Token::NonBond);
assert_eq!(cases[1], Token::Atom(bracket_atom));
assert_eq!(cases[2], Token::Atom(organic_atom));
assert_eq!(cases[3], Token::Bond(Bond::Double.into()));
assert_eq!(cases[4], Token::LeftParentheses);
assert_eq!(cases[5], Token::RightParentheses);
assert_eq!(cases[6], Token::RingClosure(ring_num));
}
#[test]
fn token_with_span_new_and_accessors_work() {
let token = Token::Bond(Bond::Triple.into());
let token_with_span = TokenWithSpan::new(token, 3, 7);
assert_eq!(token_with_span.token(), Token::Bond(Bond::Triple.into()));
assert_eq!(token_with_span.span(), 3..7);
assert_eq!(token_with_span.start(), 3);
assert_eq!(token_with_span.end(), 7);
}
#[test]
fn token_with_span_preserves_complex_token_variants() {
let bracket_atom = Atom::builder().with_symbol(AtomSymbol::Element(Element::N)).build();
let ring_num = RingNum::try_new(9).unwrap();
let bracketed = TokenWithSpan::new(Token::Atom(bracket_atom), 0, 3);
let ring = TokenWithSpan::new(Token::RingClosure(ring_num), 5, 6);
let non_bond = TokenWithSpan::new(Token::NonBond, 10, 11);
assert_eq!(bracketed.token(), Token::Atom(bracket_atom));
assert_eq!(bracketed.span(), 0..3);
assert_eq!(ring.token(), Token::RingClosure(ring_num));
assert_eq!(ring.start(), 5);
assert_eq!(ring.end(), 6);
assert_eq!(non_bond.token(), Token::NonBond);
assert_eq!(non_bond.span(), 10..11);
}
#[test]
fn token_with_span_clone_and_eq_behave_as_expected() {
let original = TokenWithSpan::new(Token::LeftParentheses, 2, 3);
let cloned = original.clone();
assert_eq!(original, cloned);
assert_eq!(original.token(), Token::LeftParentheses);
assert_eq!(cloned.span(), 2..3);
}
#[test]
fn token_kind_and_is_bond_helpers_cover_all_paths() {
let bond = TokenWithSpan::new(Token::Bond(Bond::Triple.into()), 1, 2);
assert_eq!(bond.token_kind(), TokenKind::Bond);
assert!(bond.is_bond());
let atom = TokenWithSpan::new(
Token::Atom(Atom::new_organic_subset(AtomSymbol::Element(Element::C), false)),
0,
1,
);
assert_eq!(atom.token_kind(), TokenKind::Atom);
assert!(!atom.is_bond());
assert_eq!(Token::NonBond.kind(), TokenKind::NonBond);
assert_eq!(
Token::Atom(Atom::new_organic_subset(AtomSymbol::Element(Element::N), false)).kind(),
TokenKind::Atom
);
assert_eq!(Token::Bond(Bond::Double.into()).kind(), TokenKind::Bond);
assert_eq!(Token::LeftParentheses.kind(), TokenKind::LeftParentheses);
assert_eq!(Token::RightParentheses.kind(), TokenKind::RightParentheses);
assert_eq!(Token::RingClosure(RingNum::try_new(1).unwrap()).kind(), TokenKind::RingClosure);
}
}