texlang_stdlib/
texcraft.rs

1//! The Texcraft primitive, which returns the word Texcraft as eight separate letter tokens
2//!
3//! This primitive is essentially equivalent to `\def\Texcraft{Texcraft}`.
4//! It was implemented to be a simple example of a custom expansion primitive.
5
6use texlang::*;
7
8/// Get the `\texcraft` expansion primitive.
9pub fn get_texcraft<S>() -> command::BuiltIn<S> {
10    command::BuiltIn::new_expansion(texcraft_primitive_fn)
11}
12
13pub fn texcraft_primitive_fn<S>(
14    token: token::Token,
15    _: &mut vm::ExpansionInput<S>,
16) -> command::Result<Vec<token::Token>> {
17    Ok(vec![
18        token::Token::new_letter('T', token.trace_key()),
19        token::Token::new_letter('e', token.trace_key()),
20        token::Token::new_letter('x', token.trace_key()),
21        token::Token::new_letter('c', token.trace_key()),
22        token::Token::new_letter('r', token.trace_key()),
23        token::Token::new_letter('a', token.trace_key()),
24        token::Token::new_letter('f', token.trace_key()),
25        token::Token::new_letter('t', token.trace_key()),
26    ])
27}
28
29#[cfg(test)]
30mod tests {
31    use std::collections::HashMap;
32
33    use super::*;
34    use crate::testing::*;
35
36    fn initial_commands() -> HashMap<&'static str, command::BuiltIn<State>> {
37        HashMap::from([("texcraft", get_texcraft())])
38    }
39
40    test_suite![expansion_equality_tests((
41        texcraft,
42        r"\texcraft",
43        r"Texcraft"
44    ),),];
45}