Skip to main content

moxy_fmt/ast/
leaf.rs

1use moxy_ast::*;
2use moxy_token::Ident;
3
4use crate::{FmtError, Format, Formatter};
5
6impl Format for Ident {
7    fn format(&self, f: &mut Formatter) -> Result<(), FmtError> {
8        f.text(self)
9    }
10}
11
12impl Format for Label {
13    fn format(&self, f: &mut Formatter) -> Result<(), FmtError> {
14        self.name.format(f)?;
15        f.text(":")
16    }
17}
18
19impl Format for Abi {
20    fn format(&self, f: &mut Formatter) -> Result<(), FmtError> {
21        f.text("extern")?;
22
23        if let Some(name) = &self.name {
24            f.text(" ")?;
25            f.text(format!("\"{}\"", name))?;
26        }
27
28        Ok(())
29    }
30}
31
32impl Format for Visibility {
33    fn format(&self, f: &mut Formatter) -> Result<(), FmtError> {
34        match self {
35            Self::Inherited => Ok(()),
36            Self::Public { .. } => f.text("pub"),
37            Self::Crate { .. } => f.text("pub(crate)"),
38            Self::SelfValue { .. } => f.text("pub(self)"),
39            Self::Super { .. } => f.text("pub(super)"),
40            Self::Restricted { path, .. } => {
41                f.text("pub(in ")?;
42                path.inner.1.format(f)?;
43                f.text(")")
44            }
45        }
46    }
47}
48
49impl Format for BinOp {
50    fn format(&self, f: &mut Formatter) -> Result<(), FmtError> {
51        f.text(self)
52    }
53}
54
55impl Format for UnOp {
56    fn format(&self, f: &mut Formatter) -> Result<(), FmtError> {
57        f.text(self)
58    }
59}
60
61impl Format for RangeLimits {
62    fn format(&self, f: &mut Formatter) -> Result<(), FmtError> {
63        match self {
64            Self::Closed(_) => f.text("..="),
65            Self::HalfOpen(_) => f.text(".."),
66        }
67    }
68}
69
70impl Format for moxy_ast::Safety {
71    fn format(&self, f: &mut Formatter) -> Result<(), FmtError> {
72        match self {
73            Self::Safe(_) => f.text("safe"),
74            Self::Unsafe(_) => f.text("unsafe"),
75        }
76    }
77}
78
79impl Format for moxy_ast::PatRangeLimits {
80    fn format(&self, f: &mut Formatter) -> Result<(), FmtError> {
81        match self {
82            Self::Closed(_) => f.text("..="),
83            Self::HalfOpen(_) => f.text(".."),
84            Self::Obsolete(_) => f.text("..."),
85        }
86    }
87}
88
89impl Format for PointerMutability {
90    fn format(&self, f: &mut Formatter) -> Result<(), FmtError> {
91        match self {
92            Self::Const(v) => v.format(f),
93            Self::Mut(v) => v.format(f),
94        }
95    }
96}