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