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 serve as a simple example of a custom expansion primitive.
5
6use texlang::prelude as txl;
7use texlang::*;
8
9/// Get the `\texcraft` expansion primitive.
10pub fn get_texcraft<S>() -> command::BuiltIn<S> {
11    command::BuiltIn::new_expansion(texcraft_primitive_fn)
12}
13
14pub fn texcraft_primitive_fn<S>(
15    token: token::Token,
16    input: &mut vm::ExpansionInput<S>,
17) -> txl::Result<()> {
18    let expansions = input.expansions_mut();
19    for c in "Texcraft".chars().rev() {
20        expansions.push(token::Token::new_letter(c, token.trace_key()))
21    }
22    Ok(())
23}
24
25#[cfg(test)]
26mod tests {
27    use std::collections::HashMap;
28
29    use super::*;
30    use texlang_testing::State;
31
32    fn built_in_commands() -> HashMap<&'static str, command::BuiltIn<State>> {
33        HashMap::from([("texcraft", get_texcraft())])
34    }
35
36    texlang_testing::test_suite![expansion_equality_tests((
37        texcraft,
38        r"\texcraft",
39        r"Texcraft"
40    ),),];
41}