forked from mozilla/rust-code-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformats.rs
More file actions
235 lines (195 loc) · 6.1 KB
/
Copy pathformats.rs
File metadata and controls
235 lines (195 loc) · 6.1 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use std::fs::{File, create_dir_all};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use serde::Serialize;
#[derive(Debug, Clone)]
pub enum Format {
Cbor,
Json,
Toml,
Yaml,
}
impl Format {
pub const fn all() -> &'static [&'static str] {
&["cbor", "json", "toml", "yaml"]
}
pub fn dump_formats<T: Serialize>(
&self,
space: T,
path: PathBuf,
output_path: Option<&PathBuf>,
pretty: bool,
) {
if let Some(output_path) = output_path {
match self {
Self::Cbor => Cbor::with_writer(space, path, output_path),
Self::Json => Json::with_pretty_writer(space, path, output_path, pretty),
Self::Toml => Toml::with_pretty_writer(space, path, output_path, pretty),
Self::Yaml => Yaml::with_writer(space, path, output_path),
}
} else {
match self {
Self::Json => Json::write_on_stdout_pretty(space, pretty),
Self::Toml => Toml::write_on_stdout_pretty(space, pretty),
Self::Yaml => Yaml::write_on_stdout(space),
Self::Cbor => panic!("Cbor format cannot be printed to stdout"),
}
}
}
}
impl FromStr for Format {
type Err = String;
fn from_str(format: &str) -> Result<Self, Self::Err> {
match format {
"cbor" => Ok(Self::Cbor),
"json" => Ok(Self::Json),
"toml" => Ok(Self::Toml),
"yaml" => Ok(Self::Yaml),
format => Err(format!("{format:?} is not a supported format")),
}
}
}
#[inline(always)]
fn print_on_stdout(content: String) {
writeln!(std::io::stdout().lock(), "{content}").unwrap();
}
trait WriteOnStdout {
#[inline(always)]
fn write_on_stdout<T: Serialize>(content: T) {
print_on_stdout(Self::format(content));
}
fn format<T: Serialize>(content: T) -> String;
}
trait WritePrettyOnStdout: WriteOnStdout {
fn write_on_stdout_pretty<T: Serialize>(content: T, pretty: bool) {
print_on_stdout(if pretty {
Self::format_pretty(content)
} else {
Self::format(content)
});
}
fn format_pretty<T: Serialize>(content: T) -> String;
}
fn handle_path(path: PathBuf, output_path: &Path, extension: &str) -> PathBuf {
// Remove root /
let path = path.as_path().strip_prefix("/").unwrap_or(path.as_path());
// Remove root ./
let path = path.strip_prefix("./").unwrap_or(path);
// Replace .. with . to keep files inside the output folder
let cleaned_path: Vec<&str> = path
.iter()
.map(|os_str| {
let s_str = os_str.to_str().unwrap();
if s_str == ".." { "." } else { s_str }
})
.collect();
// Create the filename
let filename = cleaned_path.join("/") + extension;
// Build the file path
output_path.join(filename)
}
trait WriteFile {
const EXTENSION: &'static str;
fn open_file(path: PathBuf, output_path: &Path) -> File {
// Handle output path
let format_path = handle_path(path, output_path, Self::EXTENSION);
// Create directories
create_dir_all(format_path.parent().unwrap()).unwrap();
File::create(format_path).unwrap()
}
fn with_writer<T: Serialize>(content: T, path: PathBuf, output_path: &Path);
}
trait WritePrettyFile: WriteFile {
fn with_pretty_writer<T: Serialize>(
content: T,
path: PathBuf,
output_path: &Path,
pretty: bool,
);
}
struct Json;
impl WriteOnStdout for Json {
fn format<T: Serialize>(content: T) -> String {
serde_json::to_string(&content).unwrap()
}
}
impl WritePrettyOnStdout for Json {
fn format_pretty<T: Serialize>(content: T) -> String {
serde_json::to_string_pretty(&content).unwrap()
}
}
impl WriteFile for Json {
const EXTENSION: &'static str = ".json";
fn with_writer<T: Serialize>(content: T, path: PathBuf, output_path: &Path) {
serde_json::to_writer(Self::open_file(path, output_path), &content).unwrap()
}
}
impl WritePrettyFile for Json {
fn with_pretty_writer<T: Serialize>(
content: T,
path: PathBuf,
output_path: &Path,
pretty: bool,
) {
if pretty {
serde_json::to_writer_pretty(Self::open_file(path, output_path), &content).unwrap();
} else {
Self::with_writer(content, path, output_path);
}
}
}
struct Toml;
impl WriteOnStdout for Toml {
fn format<T: Serialize>(content: T) -> String {
toml::to_string(&content).unwrap()
}
}
impl WritePrettyOnStdout for Toml {
fn format_pretty<T: Serialize>(content: T) -> String {
toml::to_string_pretty(&content).unwrap()
}
}
impl WriteFile for Toml {
const EXTENSION: &'static str = ".toml";
fn with_writer<T: Serialize>(content: T, path: PathBuf, output_path: &Path) {
Self::open_file(path, output_path)
.write_all(Self::format(content).as_bytes())
.unwrap();
}
}
impl WritePrettyFile for Toml {
fn with_pretty_writer<T: Serialize>(
content: T,
path: PathBuf,
output_path: &Path,
pretty: bool,
) {
if pretty {
Self::open_file(path, output_path)
.write_all(Self::format_pretty(&content).as_bytes())
.unwrap();
} else {
Self::with_writer(content, path, output_path);
}
}
}
struct Yaml;
impl WriteOnStdout for Yaml {
fn format<T: Serialize>(content: T) -> String {
serde_yml::to_string(&content).unwrap()
}
}
impl WriteFile for Yaml {
const EXTENSION: &'static str = ".yml";
fn with_writer<T: Serialize>(content: T, path: PathBuf, output_path: &Path) {
serde_yml::to_writer(Self::open_file(path, output_path), &content).unwrap()
}
}
struct Cbor;
impl WriteFile for Cbor {
const EXTENSION: &'static str = ".cbor";
fn with_writer<T: Serialize>(content: T, path: PathBuf, output_path: &Path) {
serde_cbor::to_writer(Self::open_file(path, output_path), &content).unwrap()
}
}