Skip to main content

malachite_base/rounding_modes/
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::rounding_modes::RoundingMode::{self, *};
10use crate::strings::typst::ToTypst;
11use core::fmt::{Formatter, Result};
12
13impl ToTypst for RoundingMode {
14    /// Writes a [`RoundingMode`] as a Typst math-mode fragment.
15    ///
16    /// Each mode becomes its own name, in capitals, inside a string, which Typst typesets upright.
17    /// There is no set of symbols for these that a reader would recognize, so the names stand.
18    ///
19    /// # Worst-case complexity
20    /// Constant time and additional memory.
21    ///
22    /// # Examples
23    /// ```
24    /// use malachite_base::rounding_modes::RoundingMode::*;
25    /// use malachite_base::strings::typst::ToTypst;
26    ///
27    /// assert_eq!(Down.to_typst_string(), r#""DOWN""#);
28    /// assert_eq!(Up.to_typst_string(), r#""UP""#);
29    /// assert_eq!(Floor.to_typst_string(), r#""FLOOR""#);
30    /// assert_eq!(Ceiling.to_typst_string(), r#""CEILING""#);
31    /// assert_eq!(Nearest.to_typst_string(), r#""NEAREST""#);
32    /// assert_eq!(Exact.to_typst_string(), r#""EXACT""#);
33    /// ```
34    ///
35    /// | value     | fragment    |
36    /// |-----------|-------------|
37    /// | `Down`    | `"DOWN"`    |
38    /// | `Up`      | `"UP"`      |
39    /// | `Floor`   | `"FLOOR"`   |
40    /// | `Ceiling` | `"CEILING"` |
41    /// | `Nearest` | `"NEAREST"` |
42    /// | `Exact`   | `"EXACT"`   |
43    #[inline]
44    fn fmt_typst(&self, f: &mut Formatter) -> Result {
45        f.write_str(match self {
46            Down => "\"DOWN\"",
47            Up => "\"UP\"",
48            Floor => "\"FLOOR\"",
49            Ceiling => "\"CEILING\"",
50            Nearest => "\"NEAREST\"",
51            Exact => "\"EXACT\"",
52        })
53    }
54}