rust_texas/
macros.rs

1/// Sugar for \part{} creation
2#[macro_export]
3macro_rules! part {
4    ($i:literal) => {
5        Component::Part(Part::new($i))
6    };
7    ($i:expr) => {
8        Component::Part(Part::new($i))
9    };
10}
11
12/// Sugar for \chapter{} creation
13#[macro_export]
14macro_rules! chapter {
15    ($i:literal) => {
16        Component::Chapter(Chapter::new($i))
17    };
18    ($i:expr) => {
19        Component::Chapter(Chapter::new($i))
20    };
21}
22
23/// Sugar for image creation
24#[macro_export]
25macro_rules! image {
26    ($i:literal) => {
27        Component::Image(Image::new($i))
28    };
29    ($i:expr) => {
30        Component::Image(Image::new($i))
31    };
32}
33
34#[macro_export]
35macro_rules! figure {
36    ($i:literal, $c:literal) => {
37        Component::Figure(Figure::new($i, $c))
38    };
39    ($i:expr, $c:expr) => {
40        Component::Figure(Figure::new($i, $c))
41    };
42
43    ($i:expr) => {
44        Component::Figure(Figure::new($i, "".to_string()))
45    };
46}
47
48/// Sugar for \section{} creation
49#[macro_export]
50macro_rules! section {
51    ($i:literal) => {
52        Component::Section(Section::new($i))
53    };
54    ($i:expr) => {
55        Component::Section(Section::new($i))
56    };
57}
58
59/// Sugar for environment creation.
60#[macro_export]
61macro_rules! environment {
62    ($i:literal) => {
63        Component::Environment(Environment::new($i))
64    };
65    ($i:expr) => {
66        Component::Environment(Environment::new($i))
67    };
68}
69
70/// This could've gotten real ugly if you had to do it yourself.
71/// So whenever you've got a latex macro you defined earlier, and want to use it, use this macro.
72/// ```rust
73/// use rust_texas::*;
74/// fn dummy() -> Result<(), Box<dyn std::error::Error>> {
75///     let mut doc = document!("article");
76///     command!(doc, "<your command/macro name>", "<appropriate arguments to the command>");
77///     Ok(())
78/// }
79/// ```
80///
81/// doc-tests need all the stuff above to pass
82#[macro_export]
83macro_rules! command {
84    ($doc:ident, $cmd:literal, $( $x:expr ),*) => {
85        Component::Command($doc.get_command($cmd)?.call(vec![$($x, )*])?)
86    };
87    ($doc:ident, $cmd:ident, $( $x:expr ),*) => {
88        Component::Command($doc.get_command($cmd)?.call(vec![$($x, )*])?)
89    };
90}
91
92/// Sugar for document creation
93#[macro_export]
94macro_rules! document {
95    ($l:literal) => {
96        Document::new(DocumentClass::new($l))
97    };
98    ($l:expr) => {
99        Document::new(DocumentClass::new($l))
100    };
101}
102
103/// This, too, could've gotten ugly.
104/// Package creation with options.
105/// ```rust
106/// use rust_texas::prelude::*;
107/// package!("amsmath", "<whatever options you want, as literals>");
108/// ```
109#[macro_export]
110macro_rules! package {
111    ($pkg:literal$(,)? $( $opt:literal ),*) => {{
112        #[allow(unused_mut)]
113        let mut package = Package::new($pkg);
114        $(package.add_option($opt);)*
115        package
116    }};
117    ($pkg:ident$(,)? $( $opt:ident ),*) => {{
118        #[allow(unused_mut)]
119        let mut package = Package::new($pkg);
120        $(package.add_option($opt);)*
121        package
122    }};
123}
124
125/// StackOverflow: https://stackoverflow.com/a/58243493
126#[macro_export]
127macro_rules! unwrap {
128    ($enum:path, $expr:expr) => {{
129        if let $enum(item) = $expr {
130            item
131        } else {
132            panic!()
133        }
134    }};
135}
136
137/// Basically `vec![]`
138#[macro_export]
139macro_rules! row {
140    ( $( $item:literal ),* ) => {{
141        let mut r = Row::new();
142        $(r.attach(textchunk!($item, ""))?;)*
143        r
144    }};
145    ( $( $item:ident ),* ) => {{
146        let mut r = Row::new();
147        $(r.attach($item);)*
148        r
149    }};
150    ( $( $item:expr ),* ) => {{
151        let mut r = Row::new();
152        $(r.attach($item);)*
153        r
154    }};
155}
156
157/// Convenient, I hope?
158#[macro_export]
159macro_rules! textchunk {
160    ($txt:expr, $mode:literal) => {{
161        let typ = TextType::from($mode);
162        Component::TextChunk(TextChunk::new($txt, typ))
163    }};
164    ($txt:expr) => {{
165        Component::TextChunk(TextChunk::new($txt, TextType::Normal))
166    }};
167}
168
169/// Sugar for table creation.
170/// Different name coz `diesel` uses the `table!()` macro to do some wizardry.
171#[macro_export]
172macro_rules! tabular {
173    ($i:literal, $h:ident) => {
174        Component::Table(Table::new($i, $h))
175    };
176}
177
178// Cannot think of a way to do this cleanly. Ideas would be nice. Hit up the issues page.
179#[macro_export]
180macro_rules! builtin {
181    ($arg:expr) => {
182        Component::Builtin(Builtin::new($arg))
183    };
184}
185
186#[macro_export]
187macro_rules! frame {
188    ($title:expr) => {
189        Component::Frame(Frame::new($title))
190    };
191}
192
193#[macro_export]
194macro_rules! label {
195    ($label:literal) => {
196        Component::Label(<&str as Into<Label>>::into($label))
197    };
198}
199
200#[macro_export]
201macro_rules! reference {
202    ($reference:literal) => {
203        Component::Reference(<&str as Into<Reference>>::into($reference))
204    };
205}