Skip to main content

pddl_core/pretty_print/
number.rs

1//! Pretty-printing of numbers.
2
3use crate::pretty_print::{sealed, PrettyRenderer};
4use crate::types::Number;
5use crate::visitor::Visitor;
6use pretty::RcDoc;
7
8impl sealed::Sealed for Number {}
9
10impl Visitor<Number, RcDoc<'static>> for PrettyRenderer {
11    fn visit(&self, value: &Number) -> RcDoc<'static> {
12        // Adding `0.0` turns `-0` into `0`, which is the same number but parses.
13        RcDoc::text(format!("{}", **value + 0.0))
14    }
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20    use crate::pretty_print::prettify;
21    use crate::visitor::Accept;
22    use assert2::check;
23
24    #[test]
25    fn number_works() {
26        let x = Number::from(42);
27        check!(prettify!(x, 10) == "42");
28    }
29
30    #[test]
31    fn float_number_works() {
32        let x = Number::from(2.71);
33        check!(prettify!(x, 10) == "2.71");
34    }
35
36    #[test]
37    fn large_number_prints_without_exponent() {
38        // PDDL numbers have no exponent syntax, so the long form is the only valid one.
39        let x = Number::from(1e300);
40        let text = prettify!(x, 10);
41        check!(text.len() == 301);
42        check!(text.bytes().all(|b| b.is_ascii_digit()));
43        check!((text.parse::<Number>().unwrap()) == x);
44    }
45}