texlang/types/mathcode.rs
1use crate::prelude as txl;
2use crate::{parse, traits::*};
3
4/// A math code.
5///
6/// The inner value is in the range [0, 32768).
7///
8/// See chapter 17 of the TeXBook for information on this type.
9/// The TeXBook presents an _interpretation_ of the inner value.
10/// For example, the highest 3 bits represent the "class" of the math character.
11/// From Texlang's perspective, however, a math character is just a regular integer and
12/// such interpretations are scoped to algorithms that consumes math characters.
13#[derive(Clone, Copy, Debug, Default)]
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15pub struct MathCode(pub u16);
16
17impl MathCode {
18 /// The maximum value of the inner value. This is 2^15-1.
19 pub const MAX: usize = 32767;
20}
21
22impl Parsable for MathCode {
23 fn parse_impl<S: TexlangState>(input: &mut crate::vm::ExpandedStream<S>) -> txl::Result<Self> {
24 let raw = parse::Uint::<{ MathCode::MAX + 1 }>::parse(input)?;
25 Ok(MathCode(raw.0.try_into().unwrap()))
26 }
27}