malachite_base/tuples/typst.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::strings::typst::ToTypst;
10use alloc::string::{String, ToString};
11use core::fmt::{Display, Formatter, Result};
12
13// Writes some values as one parenthesized, comma-separated Typst math-mode fragment.
14//
15// The elements arrive as trait objects rather than through a generic parameter apiece, so that
16// every arity shares one writer. `fmt_typst` is object-safe, which is what allows it.
17fn fmt_typst_parts(parts: &[&dyn ToTypst], f: &mut Formatter) -> Result {
18 f.write_str("(")?;
19 for (i, x) in parts.iter().enumerate() {
20 if i != 0 {
21 f.write_str(", ")?;
22 }
23 x.fmt_typst(f)?;
24 }
25 f.write_str(")")
26}
27
28struct TupleParts<'a>(&'a [&'a dyn ToTypst]);
29
30impl Display for TupleParts<'_> {
31 #[inline]
32 fn fmt(&self, f: &mut Formatter) -> Result {
33 fmt_typst_parts(self.0, f)
34 }
35}
36
37/// Converts some values to one parenthesized, comma-separated Typst math-mode fragment.
38///
39/// This is what the [`typst_tuple`](crate::typst_tuple) macro calls. It is also the way to build a
40/// fragment for a tuple of more than eight elements: the orphan rule keeps a crate other than this
41/// one from implementing [`ToTypst`] for such a tuple, but nothing keeps it from writing the
42/// fragment.
43///
44/// # Worst-case complexity
45/// $T(n) = O(n + \sum_{i=0}^{n-1}T^\prime(i))$
46///
47/// $M(n) = O(n + \max_{i=0}^{n-1}M^\prime(i))$
48///
49/// where $T$ is time, $M$ is additional memory, $n$ is `parts.len()`, $i$ is an element's index,
50/// and $T^\prime$ and $M^\prime$ are the time and memory functions of `fmt_typst` for that element.
51///
52/// # Examples
53/// ```
54/// use malachite_base::tuples::typst::typst_tuple;
55///
56/// assert_eq!(typst_tuple(&[&1u8, &2u8]), "(1, 2)");
57/// assert_eq!(typst_tuple(&[]), "()");
58/// ```
59#[inline]
60pub fn typst_tuple(parts: &[&dyn ToTypst]) -> String {
61 TupleParts(parts).to_string()
62}
63
64macro_rules! impl_to_typst_for_tuple {
65 ($($t:ident: $i:tt),+) => {
66 impl<$($t: ToTypst),+> ToTypst for ($($t,)+) {
67 /// Writes a tuple as a Typst math-mode fragment.
68 ///
69 /// See [here](super::typst#fmt_typst).
70 #[inline]
71 fn fmt_typst(&self, f: &mut Formatter) -> Result {
72 fmt_typst_parts(&[$(&self.$i as &dyn ToTypst),+], f)
73 }
74 }
75 };
76}
77impl_to_typst_for_tuple!(A: 0);
78impl_to_typst_for_tuple!(A: 0, B: 1);
79impl_to_typst_for_tuple!(A: 0, B: 1, C: 2);
80impl_to_typst_for_tuple!(A: 0, B: 1, C: 2, D: 3);
81impl_to_typst_for_tuple!(A: 0, B: 1, C: 2, D: 3, E: 4);
82impl_to_typst_for_tuple!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5);
83impl_to_typst_for_tuple!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6);
84impl_to_typst_for_tuple!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7);
85
86impl ToTypst for () {
87 /// Writes the unit type as a Typst math-mode fragment.
88 ///
89 /// The fragment is `()`, an empty pair of parentheses, since the unit type is the tuple of no
90 /// elements.
91 ///
92 /// # Worst-case complexity
93 /// Constant time and additional memory.
94 ///
95 /// # Examples
96 /// ```
97 /// use malachite_base::strings::typst::ToTypst;
98 ///
99 /// assert_eq!(().to_typst_string(), "()");
100 /// ```
101 ///
102 /// | value | fragment |
103 /// |-------|----------|
104 /// | `()` | `()` |
105 #[inline]
106 fn fmt_typst(&self, f: &mut Formatter) -> Result {
107 f.write_str("()")
108 }
109}
110
111/// Converts some values to one parenthesized, comma-separated Typst math-mode fragment.
112///
113/// [`ToTypst`](crate::strings::typst::ToTypst) is implemented for tuples of up to eight elements.
114/// The orphan rule keeps a crate other than this one from implementing it for a longer tuple, so
115/// this macro is the way to write such a tuple's fragment: give it the elements, and it produces
116/// what the implementation would have.
117///
118/// # Worst-case complexity
119/// $T(n) = O(n + \sum_{i=0}^{n-1}T^\prime(i))$
120///
121/// $M(n) = O(n + \max_{i=0}^{n-1}M^\prime(i))$
122///
123/// where $T$ is time, $M$ is additional memory, $n$ is the number of elements, $i$ is an element's
124/// index, and $T^\prime$ and $M^\prime$ are the time and memory functions of `fmt_typst` for that
125/// element.
126///
127/// # Examples
128/// ```
129/// use malachite_base::typst_tuple;
130///
131/// let t = (1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8, 9u8);
132/// assert_eq!(
133/// typst_tuple!(t.0, t.1, t.2, t.3, t.4, t.5, t.6, t.7, t.8),
134/// "(1, 2, 3, 4, 5, 6, 7, 8, 9)"
135/// );
136/// ```
137#[macro_export]
138macro_rules! typst_tuple {
139 ($($x:expr),* $(,)?) => {
140 $crate::tuples::typst::typst_tuple(&[$(&$x as &dyn $crate::strings::typst::ToTypst),*])
141 };
142}