tex2typst_rs/
definitions.rs

1use std::collections::HashMap;
2
3// Control: {, }, _, ^, &, \
4// Element: [, ],
5#[derive(Debug, PartialEq, Clone)]
6pub enum TexTokenType {
7    Element,
8    Command,
9    Text,
10    Comment,
11    Space,
12    Newline,
13    Control,
14    Unknown,
15    NoBreakSpace,
16}
17
18#[derive(Debug, PartialEq, Clone)]
19pub struct TexToken {
20    pub token_type: TexTokenType,
21    pub value: String,
22}
23
24impl TexToken {
25    pub fn new(token_type: TexTokenType, value: String) -> Self {
26        TexToken { token_type, value }
27    }
28}
29
30type TexArrayData = Vec<Vec<TexNode>>;
31
32// element: 0-9, a-z, A-Z, punctuations such as +-/*,:; etc.
33// symbol: LaTeX macro with no parameter. e.g. \sin \cos \int \sum
34// unaryFunc: LaTeX macro with 1 parameter. e.g. \sqrt{3} \log{x} \exp{x}
35// binaryFunc: LaTeX macro with 2 parameters. e.g. \frac{1}{2}
36// text: text enclosed by braces. e.g. \text{hello world}
37// empty: special type when something is empty. e.g. the base of _{a} or ^{a}
38// whitespace: space, tab, newline
39#[derive(Debug, PartialEq, Clone)]
40pub enum TexNodeType {
41    Element,
42    Text,
43    Comment,
44    Whitespace,
45    Control,
46    Ordgroup,
47    SupSub,
48    UnaryFunc,
49    BinaryFunc,
50    OptionBinaryFunc,
51    Leftright,
52    BeginEnd,
53    Symbol,
54    Empty,
55    UnknownMacro,
56    NoBreakSpace,
57    Unknown,
58}
59
60#[derive(Debug, PartialEq, Clone)]
61pub struct TexNode {
62    pub node_type: TexNodeType,
63    pub content: String,
64    pub args: Option<Vec<TexNode>>,   // when node_type is Command, args is the parameters
65    pub data: Option<Box<TexNodeData>>,  // for stuff like begin-end, array, etc.
66}
67
68#[derive(Debug, PartialEq, Clone)]
69pub enum TexNodeData {
70    Supsub(TexSupsubData),
71    Array(TexArrayData),
72}
73
74#[derive(Clone, Debug, PartialEq)]
75pub struct TexSupsubData {
76    pub base: TexNode,
77    pub sup: Option<TexNode>,
78    pub sub: Option<TexNode>,
79}
80
81impl TexNode {
82    pub fn new(
83        node_type: TexNodeType,
84        content: String,
85        args: Option<Vec<TexNode>>,
86        data: Option<Box<TexNodeData>>,
87    ) -> Self {
88        TexNode {
89            node_type,
90            content,
91            args,
92            data,
93        }
94    }
95}
96
97#[derive(Debug, PartialEq, Clone)]
98pub enum TypstTokenType {
99    Symbol,
100    Element,
101    Text,
102    Comment,
103    Control,
104}
105
106#[derive(Debug, PartialEq)]
107pub enum TypstNodeType {
108    Atom,
109    Symbol,
110    Text,
111    Comment,
112    Whitespace,
113    Empty,
114    Group,
115    Supsub,
116    FuncCall,
117    Fraction,
118    Align,
119    Matrix,
120    Unknown,
121    NoBreakSpace,
122}
123
124#[derive(Debug, PartialEq, Clone)]
125pub struct TypstToken {
126    pub token_type: TypstTokenType,
127    pub value: String,
128}
129
130impl TypstToken {
131    pub fn new(token_type: TypstTokenType, value: String) -> Self {
132        TypstToken { token_type, value }
133    }
134
135    pub fn to_string(&self) -> String {
136        match self.token_type {
137            TypstTokenType::Text => format!("\"{}\"", self.value),
138            TypstTokenType::Comment => format!("//{}", self.value),
139            _ => self.value.clone(),
140        }
141    }
142}
143
144#[derive(Debug)]
145pub struct TypstNode {
146    pub node_type: TypstNodeType,
147    pub content: String,
148    pub args: Option<Vec<TypstNode>>,
149    pub data: Option<Box<TypstNodeData>>,
150    pub options: Option<TypstNamedParams>,
151}
152
153impl TypstNode {
154    pub fn new(
155        node_type: TypstNodeType,
156        content: String,
157        args: Option<Vec<TypstNode>>,
158        data: Option<Box<TypstNodeData>>,
159    ) -> Self {
160        TypstNode {
161            node_type,
162            content,
163            args,
164            data,
165            options: None,
166        }
167    }
168
169    pub fn set_options(&mut self, options: TypstNamedParams) {
170        self.options = Some(options);
171    }
172}
173
174impl PartialEq for TypstNode {
175    fn eq(&self, other: &TypstNode) -> bool {
176        self.node_type == other.node_type && self.content == other.content
177    }
178}
179
180pub type TypstNamedParams = HashMap<String, String>;
181
182#[derive(Debug, PartialEq)]
183pub enum TypstNodeData {
184    Supsub(TypstSupsubData),
185    Array(TypstArrayData),
186}
187
188#[derive(Debug, PartialEq)]
189pub struct TypstSupsubData {
190    pub base: TypstNode,
191    pub sup: Option<TypstNode>,
192    pub sub: Option<TypstNode>,
193}
194
195type TypstArrayData = Vec<Vec<TypstNode>>;