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
151
152
153
154
pub mod csharp;
pub mod dart;
pub mod go;
pub mod java;
pub mod js;
pub mod python;
pub mod rust;
pub mod swift;
pub use self::csharp::Csharp;
pub use self::dart::Dart;
pub use self::go::Go;
pub use self::java::Java;
pub use self::js::JavaScript;
pub use self::python::Python;
pub use self::rust::Rust;
pub use self::swift::Swift;
use crate::fmt;
use crate::Tokens;
use std::any::Any;
use std::rc::Rc;
pub trait Lang
where
Self: 'static + Sized,
{
type Config;
type Format: Default;
type Import: ?Sized;
fn default_indentation() -> usize {
4
}
fn quote_string(out: &mut fmt::Formatter<'_>, input: &str) -> fmt::Result {
out.write_str(input)
}
fn format_file(
tokens: &Tokens<Self>,
out: &mut fmt::Formatter<'_>,
config: &Self::Config,
) -> fmt::Result {
let format = Self::Format::default();
tokens.format(out, config, &format)
}
}
impl Lang for () {
type Config = ();
type Format = ();
type Import = ();
}
pub trait LangItem<L>
where
Self: Any,
L: Lang,
{
fn format(
&self,
out: &mut fmt::Formatter<'_>,
config: &L::Config,
format: &L::Format,
) -> fmt::Result;
fn eq(&self, other: &dyn LangItem<L>) -> bool;
fn as_any(&self) -> &dyn Any;
fn as_import(&self) -> Option<&L::Import> {
None
}
}
pub struct LangBox<L> {
inner: Rc<dyn LangItem<L>>,
}
impl<L> Clone for LangBox<L>
where
L: Lang,
{
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<L> std::fmt::Debug for LangBox<L>
where
L: Lang,
{
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "LangBox")
}
}
impl<L> LangItem<L> for LangBox<L>
where
L: Lang,
{
fn format(
&self,
out: &mut fmt::Formatter<'_>,
config: &L::Config,
format: &L::Format,
) -> fmt::Result {
self.inner.format(out, config, format)
}
fn eq(&self, other: &dyn LangItem<L>) -> bool {
self.inner.eq(other)
}
fn as_any(&self) -> &dyn Any {
self.inner.as_any()
}
fn as_import(&self) -> Option<&L::Import> {
self.inner.as_import()
}
}
impl<L> From<Rc<dyn LangItem<L>>> for LangBox<L>
where
L: Lang,
{
fn from(value: Rc<dyn LangItem<L>>) -> Self {
Self { inner: value }
}
}