This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
204 lines (175 loc) · 4.88 KB
/
build.rs
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
use std::{fs, env};
use std::path::Path;
use std::fmt::Write;
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
generate_mime(&out_dir);
}
// (const, canHaveCharsetUtf8, [types..], [extensions..])
const MIMES: &[(&str, bool, &[&str], &[&str])] = &[
// text
("TEXT", true, &["text/plain"], &["txt"]),
("HTML", true, &["text/html"], &["html"]),
("JS", true, &["application/javascript"], &["js"]),
("CSS", true, &["text/css"], &["css"]),
("JSON", true, &["application/json"], &["json"]),
("CSV", true, &["text/csv"], &["csv"]),
("DOC", false, &["application/msword"], &["doc"]),
("DOCX", false,
&["application/vnd.openxmlformats-officedocument.wordprocessingml.document"],
&["docx"]
),
("PDF", false, &["application/pdf"], &["pdf"]),
("PHP", true, &["application/php"], &["php"]),
("RTF", false, &["application/rtf"], &["rtf"]),
("SH", false, &["application/x-sh"], &["sh"]),
("VSD", false, &["application/vnd.visio"], &["vsd"]),
("XML", true, &["text/xml"], &["xml"]),
// imgs
("JPG", false, &["image/jpeg"], &["jpg"]),
("PNG", false, &["image/png"], &["png"]),
("GIF", false, &["image/gif"], &["gif"]),
("SVG", false, &["image/svg+xml"], &["svg"]),
("ICO", false, &["image/vnd.microsoft.icon"], &["ico"]),
("TIFF", false, &["image/tiff"], &["tiff"]),
("WBP", false, &["image/webp"], &["webp"]),
// fonts
("EOT", false, &["application/vnd.ms-fontobject"], &["eot"]),
("TTF", false, &["font/ttf"], &["ttf"]),
("WOFF", false, &["font/woff"], &["woff"]),
("WOOF2", false, &["font/woff2"], &["woff2"]),
// video
("AVI", false, &["video/x-msvideo"], &["avi"]),
("OGV", false, &["video/ogg"], &["ogv"]),
("WEBM", false, &["video/webm"], &["webm"]),
("MP4", false, &["video/mp4"], &["mp4"]),
// audio
("AAC", false, &["audio/aac"], &["aac"]),
("MP3", false, &["audio/mpeg"], &["mp3"]),
("OGA", false, &["audio/ogg"], &["oga"]),
("WAV", false, &["audio/wav"], &["wav"]),
("WEBA", false, &["audio/webm"], &["weba"]),
// Archives
("RAR", false, &["application/vnd.rar"], &["rar"]),
("TAR", false, &["application/x-tar"], &["tar"]),
("ZIP", false, &["application/zip"], &["zip"]),
("_7ZIP", false, &["application/x-7z-compressed"], &["7z"]),
// Binary
("JAR", false, &["application/java-archive"], &["jar"]),
("BINARY", false, &["application/octet-stream"], &["bin"]),
("WASM", false, &["application/wasm"], &["wasm"])
];
fn generate_mime(out_dir: &str) {
let dest_path = Path::new(&out_dir).join("mime.rs");
let mut value_enum = format!(
"\
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]\n\
enum MimeValue {{\n"
);
let mut mime_consts = String::new();
let mut from_extension_fn = format!(
"\tfn from_extension(e: &str) -> Option<Self> {{\n\
\t\tmatch e {{\n"
);
let mut from_str_fn = format!(
"\tfn from_str(s: &str) -> Option<Self> {{\n\
\t\tmatch s {{\n"
);
let mut extension_fn = format!(
"\tfn extension(&self) -> &'static str {{\n\
\t\tmatch self {{\n"
);
let mut as_str_fn = format!(
"\tfn as_str(&self) -> &'static str {{\n\
\t\tmatch self {{\n"
);
let mut as_str_with_maybe_charset = format!(
"\tfn as_str_with_maybe_charset(&self) -> &'static str {{\n\
\t\tmatch self {{\n"
);
for (id, utf8, types, extensions) in MIMES {
write!(value_enum,
"\t{id},\n"
).unwrap();
write!(mime_consts,
"\t/// File extensions: {} \n\
\t/// mime types: {}\n\
\tpub const {id}: Self = Self(MimeValue::{id});\n",
extensions.join(", "),
types.join(", ")
).unwrap();
for ext in *extensions {
write!(from_extension_fn,
"\t\t\t\"{ext}\" => Some(Self::{id}),\n"
).unwrap();
}
for ty in *types {
write!(from_str_fn,
"\t\t\t\"{ty}\" => Some(Self::{id}),\n"
).unwrap();
if *utf8 {
write!(from_str_fn,
"\t\t\t\"{ty}; charset=utf-8\" => Some(Self::{id}),\n"
).unwrap();
}
}
let ext = extensions.first().unwrap();
write!(extension_fn,
"\t\t\tSelf::{id} => \"{ext}\",\n"
).unwrap();
let ty = types.first().unwrap();
write!(as_str_fn,
"\t\t\tSelf::{id} => \"{ty}\",\n"
).unwrap();
if *utf8 {
write!(as_str_with_maybe_charset,
"\t\t\tSelf::{id} => \"{ty}; charset=utf-8\",\n"
).unwrap();
} else {
write!(as_str_with_maybe_charset,
"\t\t\tSelf::{id} => \"{ty}\",\n"
).unwrap();
}
}
// now end functions
write!(value_enum,
"}}\n"
).unwrap();
write!(from_extension_fn,
"\t\t\t_ => None\n\
\t\t}}\n\
\t}}\n"
).unwrap();
write!(from_str_fn,
"\t\t\t_ => None\n\
\t\t}}\n\
\t}}\n"
).unwrap();
write!(extension_fn,
"\t\t}}\n\
\t}}\n"
).unwrap();
write!(as_str_fn,
"\t\t}}\n\
\t}}\n"
).unwrap();
write!(as_str_with_maybe_charset,
"\t\t}}\n\
\t}}\n"
).unwrap();
// impl value enum
let content = format!(
"{value_enum}\n\
impl MimeValue {{\n\
{from_extension_fn}\n\
{from_str_fn}\n\
{extension_fn}\n\
{as_str_fn}\n\
{as_str_with_maybe_charset}\
}}\n\n\
impl Mime {{\n\
{mime_consts}\
}}"
);
fs::write(dest_path, content).unwrap();
}