1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use std::fmt::{self, Write};
use crate::bound::Bound;
use crate::r#type::Type;
const DEFAULT_INDENT: usize = 4;
#[derive(Debug)]
pub struct Formatter<'a> {
dst: &'a mut String,
spaces: usize,
indent: usize,
}
impl<'a> Formatter<'a> {
pub fn new(dst: &'a mut String) -> Self {
Formatter {
dst,
spaces: 0,
indent: DEFAULT_INDENT,
}
}
pub fn block<F>(&mut self, f: F) -> fmt::Result
where
F: FnOnce(&mut Self) -> fmt::Result,
{
if !self.is_start_of_line() {
write!(self, " ")?;
}
write!(self, "{{\n")?;
self.indent(f)?;
write!(self, "}}\n")?;
Ok(())
}
pub fn indent<F, R>(&mut self, f: F) -> R
where
F: FnOnce(&mut Self) -> R,
{
self.spaces += self.indent;
let ret = f(self);
self.spaces -= self.indent;
ret
}
pub fn is_start_of_line(&self) -> bool {
self.dst.is_empty() || self.dst.as_bytes().last() == Some(&b'\n')
}
fn push_spaces(&mut self) {
for _ in 0..self.spaces {
self.dst.push_str(" ");
}
}
}
impl<'a> fmt::Write for Formatter<'a> {
fn write_str(&mut self, s: &str) -> fmt::Result {
let mut first = true;
let mut should_indent = self.is_start_of_line();
for line in s.lines() {
if !first {
self.dst.push_str("\n");
}
first = false;
let do_indent = should_indent && !line.is_empty() && line.as_bytes()[0] != b'\n';
if do_indent {
self.push_spaces();
}
should_indent = true;
self.dst.push_str(line);
}
if s.as_bytes().last() == Some(&b'\n') {
self.dst.push_str("\n");
}
Ok(())
}
}
pub fn fmt_generics(generics: &[String], fmt: &mut Formatter<'_>) -> fmt::Result {
if !generics.is_empty() {
write!(fmt, "<")?;
for (i, ty) in generics.iter().enumerate() {
if i != 0 {
write!(fmt, ", ")?
}
write!(fmt, "{}", ty)?;
}
write!(fmt, ">")?;
}
Ok(())
}
pub fn fmt_bounds(bounds: &[Bound], fmt: &mut Formatter<'_>) -> fmt::Result {
if !bounds.is_empty() {
write!(fmt, "\n")?;
write!(fmt, "where {}: ", bounds[0].name)?;
fmt_bound_rhs(&bounds[0].bound, fmt)?;
write!(fmt, ",\n")?;
for bound in &bounds[1..] {
write!(fmt, " {}: ", bound.name)?;
fmt_bound_rhs(&bound.bound, fmt)?;
write!(fmt, ",\n")?;
}
}
Ok(())
}
pub fn fmt_bound_rhs(tys: &[Type], fmt: &mut Formatter<'_>) -> fmt::Result {
for (i, ty) in tys.iter().enumerate() {
if i != 0 {
write!(fmt, " + ")?
}
ty.fmt(fmt)?;
}
Ok(())
}