ftml/parsing/check_step.rs
1/*
2 * parsing/check_step.rs
3 *
4 * ftml - Library to parse Wikidot text
5 * Copyright (C) 2019-2025 Wikijump Team
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21use super::{ExtractedToken, ParseError, Parser, Token};
22
23/// Helper function to assert that the current token matches, then step.
24///
25/// # Returns
26/// The `ExtractedToken` which was checked and stepped over.
27///
28/// # Panics
29/// Since an assert is used, this function will panic
30/// if the extracted token does not match the one specified.
31#[inline]
32pub fn check_step<'r, 't>(
33 parser: &mut Parser<'r, 't>,
34 token: Token,
35) -> Result<&'r ExtractedToken<'t>, ParseError> {
36 let current = parser.current();
37
38 assert_eq!(current.token, token, "Opening token isn't {}", token.name());
39
40 parser.step()?;
41
42 Ok(current)
43}
44
45#[test]
46#[should_panic]
47fn check_step_fail() {
48 use crate::data::PageInfo;
49 use crate::layout::Layout;
50 use crate::settings::{WikitextMode, WikitextSettings};
51
52 let page_info = PageInfo::dummy();
53 let settings = WikitextSettings::from_mode(WikitextMode::Page, Layout::Wikidot);
54 let tokenization = crate::tokenize("**Apple** banana");
55 let mut parser = Parser::new(&tokenization, &page_info, &settings);
56
57 let _ = check_step(&mut parser, Token::Italics);
58}