malachite_nz/integer_vector/conversion/string/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::integer_vector::IntegerVector;
10use core::fmt::{Formatter, Result};
11use malachite_base::strings::typst::ToTypst;
12
13impl ToTypst for IntegerVector {
14 /// Writes an [`IntegerVector`] as a Typst math-mode fragment.
15 ///
16 /// The elements' fragments are separated by commas and enclosed in parentheses, as a vector's
17 /// coordinates are written by hand; Typst grows the parentheses to fit their contents. The
18 /// 0-dimensional vector is `()`.
19 ///
20 /// # Worst-case complexity
21 /// $T(n) = O(n (\log n)^2 \log\log n)$
22 ///
23 /// $M(n) = O(n \log n)$
24 ///
25 /// where $T$ is time, $M$ is additional memory, and $n$ is the sum of the bits of the elements.
26 ///
27 /// # Examples
28 /// ```
29 /// use malachite_base::strings::typst::ToTypst;
30 /// use malachite_nz::integer::Integer;
31 /// use malachite_nz::integer_vector::IntegerVector;
32 ///
33 /// let v = IntegerVector {
34 /// elements: vec![Integer::from(1u32), Integer::from(2u32), Integer::from(3u32)],
35 /// };
36 /// assert_eq!(v.to_typst_string(), "(1, 2, 3)");
37 /// assert_eq!(
38 /// IntegerVector {
39 /// elements: vec![Integer::from(5u32)]
40 /// }
41 /// .to_typst_string(),
42 /// "(5)"
43 /// );
44 /// assert_eq!(
45 /// IntegerVector {
46 /// elements: Vec::new()
47 /// }
48 /// .to_typst_string(),
49 /// "()"
50 /// );
51 /// ```
52 ///
53 /// The value column holds each vector as [`Display`](core::fmt::Display) writes it.
54 ///
55 /// | value | fragment |
56 /// |-------------|-------------|
57 /// | `(1, 2, 3)` | `(1, 2, 3)` |
58 /// | `(5)` | `(5)` |
59 /// | `()` | `()` |
60 fn fmt_typst(&self, f: &mut Formatter) -> Result {
61 f.write_str("(")?;
62 for (i, x) in self.elements.iter().enumerate() {
63 if i != 0 {
64 f.write_str(", ")?;
65 }
66 x.fmt_typst(f)?;
67 }
68 f.write_str(")")
69 }
70}