Skip to main content

malachite_base/foer_sequences/
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::foer_sequences::FoerSequence;
10use crate::strings::typst::ToTypst;
11use core::fmt::{Formatter, Result};
12
13impl<T: ToTypst + Eq> ToTypst for FoerSequence<T> {
14    /// Writes a [`FoerSequence`] as a Typst math-mode fragment.
15    ///
16    /// The elements' fragments are separated by commas and wrapped in square brackets, as a
17    /// sequence's are, and the repeating part, if there is one, is written under a vinculum: the
18    /// overline that marks a repeating decimal. A sequence with nothing but a repeating part is all
19    /// vinculum, and one with none is an ordinary bracketed list.
20    ///
21    /// Typst grows a matched pair of delimiters to fit what is between them, so the brackets fit an
22    /// element that is taller than one line without being asked to.
23    ///
24    /// Inside the vinculum the elements are separated by Typst's `comma` symbol rather than by a
25    /// literal comma. `overline` takes a single body, and a literal comma there would be read as
26    /// the separator between two arguments and rejected; `comma` renders the same.
27    ///
28    /// # Worst-case complexity
29    /// $T(n) = O(n + \sum_{i=0}^{n-1}T^\prime(i))$
30    ///
31    /// $M(n) = O(\max_{i=0}^{n-1}M^\prime(i))$
32    ///
33    /// where $T$ is time, $M$ is additional memory, $n$ is `self.component_len()`, $i$ is an
34    /// element's index, and $T^\prime$ and $M^\prime$ are the time and memory functions of
35    /// `fmt_typst` for `T`.
36    ///
37    /// # Examples
38    /// ```
39    /// use malachite_base::foer_sequences::FoerSequence;
40    /// use malachite_base::strings::typst::ToTypst;
41    ///
42    /// let empty = FoerSequence::<u8>::from_vecs(vec![], vec![]);
43    /// assert_eq!(empty.to_typst_string(), "[]");
44    ///
45    /// let finite = FoerSequence::<u8>::from_vecs(vec![1, 2], vec![]);
46    /// assert_eq!(finite.to_typst_string(), "[1, 2]");
47    ///
48    /// let repeating = FoerSequence::<u8>::from_vecs(vec![], vec![3, 4]);
49    /// assert_eq!(repeating.to_typst_string(), "[overline(3 comma 4)]");
50    ///
51    /// let both = FoerSequence::<u8>::from_vecs(vec![1, 2], vec![3, 4]);
52    /// assert_eq!(both.to_typst_string(), "[1, 2, overline(3 comma 4)]");
53    /// ```
54    ///
55    /// | value            | fragment                      |
56    /// |------------------|-------------------------------|
57    /// | `[]`             | `[]`                          |
58    /// | `[1, 2]`         | `[1, 2]`                      |
59    /// | `[[3, 4]]`       | `[overline(3 comma 4)]`       |
60    /// | `[1, 2, [3, 4]]` | `[1, 2, overline(3 comma 4)]` |
61    fn fmt_typst(&self, f: &mut Formatter) -> Result {
62        f.write_str("[")?;
63        for (i, x) in self.non_repeating.iter().enumerate() {
64            if i != 0 {
65                f.write_str(", ")?;
66            }
67            x.fmt_typst(f)?;
68        }
69        if !self.repeating.is_empty() {
70            if !self.non_repeating.is_empty() {
71                f.write_str(", ")?;
72            }
73            f.write_str("overline(")?;
74            for (i, x) in self.repeating.iter().enumerate() {
75                if i != 0 {
76                    f.write_str(" comma ")?;
77                }
78                x.fmt_typst(f)?;
79            }
80            f.write_str(")")?;
81        }
82        f.write_str("]")
83    }
84}