Skip to main content

malachite_base/num/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::float::NiceFloat;
10use crate::strings::typst::ToTypst;
11use alloc::string::ToString;
12use core::fmt::{Display, Formatter, Result, Write};
13
14macro_rules! impl_to_typst {
15    ($t:ident) => {
16        impl ToTypst for $t {
17            /// Writes a primitive integer as a Typst math-mode fragment.
18            ///
19            /// The fragment is the integer's decimal digits, preceded by a minus sign if it is
20            /// negative, which is what Typst math mode already writes a number as.
21            ///
22            /// # Worst-case complexity
23            /// $T(n) = O(n)$
24            ///
25            /// $M(n) = O(1)$
26            ///
27            /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
28            ///
29            /// # Examples
30            /// See [here](super::typst#fmt_typst).
31            #[inline]
32            fn fmt_typst(&self, f: &mut Formatter) -> Result {
33                Display::fmt(self, f)
34            }
35        }
36    };
37}
38apply_to_primitive_ints!(impl_to_typst);
39
40macro_rules! impl_to_typst_primitive_float {
41    ($t:ident) => {
42        impl ToTypst for $t {
43            /// Writes a primitive float as a Typst math-mode fragment.
44            ///
45            /// A NaN becomes `"NaN"` and the infinities become `infinity` and `-infinity`. A finite
46            /// float is written as its [`NiceFloat`] representation, with the exponent, if there is
47            /// one, lifted into a real power of ten: `1.23e-3` becomes `1.23 times 10^(-3)`.
48            ///
49            /// # Worst-case complexity
50            /// Constant time and additional memory.
51            ///
52            /// # Examples
53            /// See [here](super::typst#fmt_typst).
54            fn fmt_typst(&self, f: &mut Formatter) -> Result {
55                if self.is_nan() {
56                    return f.write_str("\"NaN\"");
57                } else if self.is_infinite() {
58                    return f.write_str(if self.is_sign_positive() {
59                        "infinity"
60                    } else {
61                        "-infinity"
62                    });
63                }
64                let s = NiceFloat(*self).to_string();
65                let Some(e_index) = s.find('e') else {
66                    return f.write_str(&s);
67                };
68                let (mantissa, exponent) = s.split_at(e_index);
69                let exponent = &exponent[1..];
70                f.write_str(mantissa)?;
71                f.write_str(" times 10^(")?;
72                f.write_str(exponent)?;
73                f.write_char(')')
74            }
75        }
76    };
77}
78apply_to_primitive_floats!(impl_to_typst_primitive_float);