rust_texas/document/
doc_class.rs

1use crate::prelude::*;
2use std::fmt::Display;
3
4/// Currently, only these few types are supported.
5/// There is also nothing preventing you from putting a \part{} in a document of class "part",
6/// but latex will show an error. If you want those restrictions to be implemented, please put
7/// up an issue
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum DocumentClassType {
10    Article,
11    Amsart,
12    Part,
13    Report,
14    Book,
15    Beamer,
16}
17impl Display for DocumentClassType {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        write!(
20            f,
21            "{}",
22            match &self {
23                Self::Article => "article",
24                Self::Part => "part",
25                Self::Report => "report",
26                Self::Book => "book",
27                Self::Amsart => "amsart",
28                Self::Beamer => "beamer",
29            }
30        )?;
31
32        Ok(())
33    }
34}
35impl From<&str> for DocumentClassType {
36    fn from(value: &str) -> Self {
37        match value {
38            "article" => Self::Article,
39            "part" => Self::Part,
40            "book" => Self::Book,
41            "report" => Self::Report,
42            "amsart" => Self::Amsart,
43            "beamer" => Self::Beamer,
44            _ => Self::Article,
45        }
46    }
47}
48
49/// Wrapper around `DocumentClassType`, contains whatever options you want to add.
50/// Nothing prevents you from adding absolute gibberish as an option. If you want those
51/// restrictions implemented, please put up an issue.
52#[derive(Debug, Clone)]
53pub struct DocumentClass {
54    pub(crate) typ: DocumentClassType,
55    pub(crate) opt: Vec<String>,
56}
57impl DocumentClass {
58    pub fn new(typ: &str) -> Self {
59        Self {
60            typ: typ.into(),
61            opt: vec![],
62        }
63    }
64}
65impl AsLatex for DocumentClass {
66    fn to_string(&self) -> String {
67        let options = self
68            .opt
69            .iter()
70            .map(|s| format!("{}, ", s))
71            .collect::<String>();
72        format!("\\documentclass[{}]{{{}}}", options, self.typ.to_string())
73    }
74}
75impl Opt for DocumentClass {
76    fn add_option(&mut self, opt: &str) {
77        self.opt.push(opt.to_string())
78    }
79}