Skip to main content

elm_ast/
display.rs

1//! `Display` implementations for AST types.
2//!
3//! These use the printer to produce valid Elm source text.
4//! Requires the `printing` feature.
5
6use std::fmt;
7
8use crate::declaration::Declaration;
9use crate::expr::Expr;
10use crate::file::ElmModule;
11use crate::node::Spanned;
12use crate::pattern::Pattern;
13use crate::print::{PrintConfig, Printer};
14use crate::type_annotation::TypeAnnotation;
15
16impl fmt::Display for ElmModule {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        let output = Printer::new(PrintConfig::default()).print_module(self);
19        f.write_str(&output)
20    }
21}
22
23impl fmt::Display for Expr {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        let mut p = Printer::new(PrintConfig::default());
26        p.write_expr(self);
27        f.write_str(&p.finish())
28    }
29}
30
31impl fmt::Display for Pattern {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        let mut p = Printer::new(PrintConfig::default());
34        p.write_pattern(self);
35        f.write_str(&p.finish())
36    }
37}
38
39impl fmt::Display for TypeAnnotation {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        let mut p = Printer::new(PrintConfig::default());
42        p.write_type(self);
43        f.write_str(&p.finish())
44    }
45}
46
47impl fmt::Display for Declaration {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        let mut p = Printer::new(PrintConfig::default());
50        p.write_declaration(self);
51        f.write_str(&p.finish())
52    }
53}
54
55// Display for Spanned<T> delegates to the inner value.
56impl<T: fmt::Display> fmt::Display for Spanned<T> {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        self.value.fmt(f)
59    }
60}