boxworks_texlang/
text.rs

1use boxworks_text as bwt;
2use texlang::prelude as txl;
3use texlang::traits::*;
4use texlang::*;
5
6pub struct Component {
7    text_preprocessor: bwt::TextPreprocessorImpl,
8}
9
10impl Default for Component {
11    fn default() -> Self {
12        Self {
13            text_preprocessor: bwt::TextPreprocessorImpl::new(bwt::Params::plain_tex_defaults()),
14        }
15    }
16}
17
18/// Get the `\sfcode` command.
19pub fn get_sfcode<S: HasComponent<Component>>() -> command::BuiltIn<S> {
20    variable::Command::new_array(
21        ref_fn,
22        mut_fn,
23        variable::IndexResolver::Dynamic(resolver_fn),
24    )
25    .into()
26}
27
28fn ref_fn<S: HasComponent<Component>>(state: &S, index: variable::Index) -> &i32 {
29    state
30        .component()
31        .text_preprocessor
32        .params
33        .space_factor_codes
34        .0
35        .get(index.0)
36        .unwrap()
37}
38
39fn mut_fn<S: HasComponent<Component>>(state: &mut S, index: variable::Index) -> &mut i32 {
40    state
41        .component_mut()
42        .text_preprocessor
43        .params
44        .space_factor_codes
45        .0
46        .get_mut(index.0)
47        .unwrap()
48}
49
50fn resolver_fn<S: HasComponent<Component>>(
51    _: token::Token,
52    input: &mut vm::ExpandedStream<S>,
53) -> txl::Result<variable::Index> {
54    let index = parse::Uint::<256>::parse(input)?;
55    Ok(index.0.into())
56}