rust_texas/component/
builtin.rs

1use std::fmt::Display;
2
3use crate::prelude::*;
4
5#[derive(Debug, Clone)]
6pub struct Builtin {
7    typ: BuiltinType,
8}
9impl AsLatex for Builtin {
10    fn to_string(&self) -> String {
11        format!("{}", self.typ.to_string())
12    }
13}
14impl Builtin {
15    pub fn new(typ: BuiltinType) -> Self {
16        Self { typ }
17    }
18}
19#[derive(Debug, Clone)]
20pub enum BuiltinType {
21    EnsureMath(TextChunk),
22    Sin(TextChunk),
23    Cos(TextChunk),
24    Tan(TextChunk),
25    Log(TextChunk),
26    Ln(TextChunk),
27    Lg(TextChunk),
28    Sum(TextChunk, TextChunk),
29    Prod(TextChunk, TextChunk),
30    Arg(TextChunk),
31    Min(TextChunk),
32    Max(TextChunk),
33    Character(String), // As in greek, but also stuff like \infty, etc.
34    Surround(char, TextChunk, char),
35}
36impl Display for BuiltinType {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        write!(
39            f,
40            "{}",
41            match self {
42                BuiltinType::EnsureMath(s) => format!("\\ensuremath{{{}}}", s.to_string()),
43                BuiltinType::Sin(s) => format!("\\sin{{{}}}", s.to_string()),
44                BuiltinType::Cos(s) => format!("\\cos{{{}}}", s.to_string()),
45                BuiltinType::Tan(s) => format!("\\tan{{{}}}", s.to_string()),
46                BuiltinType::Log(s) => format!("\\log{{{}}}", s.to_string()),
47                BuiltinType::Ln(s) => format!("\\ln{{{}}}", s.to_string()),
48                BuiltinType::Lg(s) => format!("\\lg{{{}}}", s.to_string()),
49                BuiltinType::Sum(down, up) =>
50                    format!("\\sum_{{{}}}^{{{}}}", down.to_string(), up.to_string()),
51                BuiltinType::Prod(down, up) =>
52                    format!("\\prod_{{{}}}^{{{}}}", down.to_string(), up.to_string()),
53                BuiltinType::Arg(s) => format!("\\arg{{{}}}", s.to_string()),
54                BuiltinType::Min(s) => format!("\\min{{{}}}", s.to_string()),
55                BuiltinType::Max(s) => format!("\\max{{{}}}", s.to_string()),
56                BuiltinType::Character(s) => format!("\\{}", s),
57                BuiltinType::Surround(left, content, right) =>
58                    format!("\\left{left} {} \\right{right}", content.to_string()),
59            }
60        )?;
61
62        Ok(())
63    }
64}