Skip to content

Commit c0018b4

Browse files
committed
define mime constants with a macro
1 parent 7e06db5 commit c0018b4

File tree

1 file changed

+57
-254
lines changed

1 file changed

+57
-254
lines changed

src/mime/constants.rs

Lines changed: 57 additions & 254 deletions
Original file line numberDiff line numberDiff line change
@@ -1,260 +1,63 @@
11
use super::ParamKind;
22
use crate::Mime;
33

4-
/// Content-Type that matches anything.
5-
///
6-
/// # Mime Type
7-
///
8-
/// ```txt
9-
/// */*
10-
/// ```
11-
pub const ANY: Mime = Mime {
12-
essence: String::new(),
13-
basetype: String::new(),
14-
subtype: String::new(),
15-
params: None,
16-
static_essence: Some("*/*"),
17-
static_basetype: Some("*"),
18-
static_subtype: Some("*"),
19-
};
20-
21-
/// Content-Type for JavaScript.
22-
///
23-
/// # Mime Type
24-
///
25-
/// ```txt
26-
/// application/javascript; charset=utf-8
27-
/// ```
28-
pub const JAVASCRIPT: Mime = Mime {
29-
static_essence: Some("application/javascript"),
30-
essence: String::new(),
31-
basetype: String::new(),
32-
subtype: String::new(),
33-
params: Some(ParamKind::Utf8),
34-
static_basetype: Some("application"),
35-
static_subtype: Some("javascript"),
36-
};
37-
38-
/// Content-Type for JSON.
39-
///
40-
/// # Mime Type
41-
///
42-
/// ```txt
43-
/// application/json
44-
/// ```
45-
pub const JSON: Mime = Mime {
46-
static_essence: Some("application/json"),
47-
essence: String::new(),
48-
basetype: String::new(),
49-
subtype: String::new(),
50-
params: None,
51-
static_basetype: Some("application"),
52-
static_subtype: Some("json"),
53-
};
54-
55-
/// Content-Type for CSS.
56-
///
57-
/// # Mime Type
58-
///
59-
/// ```txt
60-
/// text/css; charset=utf-8
61-
/// ```
62-
pub const CSS: Mime = Mime {
63-
static_essence: Some("text/css"),
64-
essence: String::new(),
65-
basetype: String::new(),
66-
subtype: String::new(),
67-
params: Some(ParamKind::Utf8),
68-
static_basetype: Some("text"),
69-
static_subtype: Some("css"),
70-
};
71-
72-
/// Content-Type for HTML.
73-
///
74-
/// # Mime Type
75-
///
76-
/// ```txt
77-
/// text/html; charset=utf-8
78-
/// ```
79-
pub const HTML: Mime = Mime {
80-
static_essence: Some("text/html"),
81-
essence: String::new(),
82-
basetype: String::new(),
83-
subtype: String::new(),
84-
params: Some(ParamKind::Utf8),
85-
static_basetype: Some("text"),
86-
static_subtype: Some("html"),
87-
};
88-
89-
/// Content-Type for SVG.
90-
///
91-
/// # Mime Type
92-
///
93-
/// ```txt
94-
/// image/svg+xml
95-
/// ```
96-
pub const SVG: Mime = Mime {
97-
static_essence: Some("image/svg+xml"),
98-
essence: String::new(),
99-
basetype: String::new(),
100-
subtype: String::new(),
101-
params: None,
102-
static_basetype: Some("image"),
103-
static_subtype: Some("svg+xml"),
104-
};
105-
106-
/// Content-Type for ICO icons.
107-
///
108-
/// # Mime Type
109-
///
110-
/// ```txt
111-
/// image/x-icon
112-
/// ```
4+
macro_rules! utf8_mime_const {
5+
($name:ident, $desc:expr, $base:expr, $sub:expr) => {
6+
mime_const!(
7+
with_params,
8+
$name,
9+
$desc,
10+
$base,
11+
$sub,
12+
Some(ParamKind::Utf8),
13+
";charset=utf-8"
14+
);
15+
};
16+
}
17+
macro_rules! mime_const {
18+
($name:ident, $desc:expr, $base:expr, $sub:expr) => {
19+
mime_const!(with_params, $name, $desc, $base, $sub, None, "");
20+
};
21+
22+
(with_params, $name:ident, $desc:expr, $base:expr, $sub:expr, $params:expr, $doccomment:expr) => {
23+
mime_const!(doc_expanded, $name, $desc, $base, $sub, $params,
24+
concat!(
25+
"Content-Type for ",
26+
$desc,
27+
".\n\n# Mime Type\n\n```text\n",
28+
$base, "/", $sub, $doccomment, "\n```")
29+
);
30+
};
31+
32+
(doc_expanded, $name:ident, $desc:expr, $base:expr, $sub:expr, $params:expr, $doccomment:expr) => {
33+
#[doc = $doccomment]
34+
pub const $name: Mime = Mime {
35+
essence: String::new(),
36+
basetype: String::new(),
37+
subtype: String::new(),
38+
params: $params,
39+
static_essence: Some(concat!($base, "/", $sub)),
40+
static_basetype: Some($base),
41+
static_subtype: Some($sub),
42+
};
43+
};
44+
}
45+
46+
utf8_mime_const!(JAVASCRIPT, "JavaScript", "application", "javascript");
47+
utf8_mime_const!(CSS, "CSS", "text", "css");
48+
utf8_mime_const!(HTML, "HTML", "text", "html");
49+
utf8_mime_const!(PLAIN, "Plain text", "text", "plain");
50+
mime_const!(ANY, "matching anything", "*", "*");
51+
mime_const!(JSON, "JSON", "application", "json");
52+
mime_const!(SVG, "SVG", "image", "svg+xml");
53+
mime_const!(PNG, "PNG images", "image", "png");
54+
mime_const!(JPEG, "JPEG images", "image", "jpeg");
55+
mime_const!(SSE, "Server Sent Events", "text", "event-stream");
56+
mime_const!(BYTE_STREAM, "byte streams", "application", "octet-stream");
57+
mime_const!(FORM, "forms", "application", "x-www-form-urlencoded");
58+
mime_const!(MULTIPART_FORM, "multipart forms", "multipart", "form-data");
59+
mime_const!(WASM, "webassembly", "application", "wasm");
11360
// There are multiple `.ico` mime types known, but `image/x-icon`
11461
// is what most browser use. See:
11562
// https://en.wikipedia.org/wiki/ICO_%28file_format%29#MIME_type
116-
pub const ICO: Mime = Mime {
117-
static_essence: Some("image/x-icon"),
118-
essence: String::new(),
119-
basetype: String::new(),
120-
subtype: String::new(),
121-
params: None,
122-
static_basetype: Some("image"),
123-
static_subtype: Some("x-icon"),
124-
};
125-
126-
/// Content-Type for PNG images.
127-
///
128-
/// # Mime Type
129-
///
130-
/// ```txt
131-
/// image/png
132-
/// ```
133-
pub const PNG: Mime = Mime {
134-
static_essence: Some("image/png"),
135-
essence: String::new(),
136-
basetype: String::new(),
137-
subtype: String::new(),
138-
params: None,
139-
static_basetype: Some("image"),
140-
static_subtype: Some("png"),
141-
};
142-
143-
/// Content-Type for JPEG images.
144-
///
145-
/// # Mime Type
146-
///
147-
/// ```txt
148-
/// image/jpeg
149-
/// ```
150-
pub const JPEG: Mime = Mime {
151-
static_essence: Some("image/jpeg"),
152-
essence: String::new(),
153-
basetype: String::new(),
154-
subtype: String::new(),
155-
params: None,
156-
static_basetype: Some("image"),
157-
static_subtype: Some("jpeg"),
158-
};
159-
160-
/// Content-Type for Server Sent Events
161-
///
162-
/// # Mime Type
163-
///
164-
/// ```txt
165-
/// text/event-stream
166-
/// ```
167-
pub const SSE: Mime = Mime {
168-
static_essence: Some("text/event-stream"),
169-
essence: String::new(),
170-
basetype: String::new(),
171-
subtype: String::new(),
172-
static_basetype: Some("text"),
173-
static_subtype: Some("event-stream"),
174-
params: None,
175-
};
176-
177-
/// Content-Type for plain text.
178-
///
179-
/// # Mime Type
180-
///
181-
/// ```txt
182-
/// text/plain; charset=utf-8
183-
/// ```
184-
pub const PLAIN: Mime = Mime {
185-
static_essence: Some("text/plain"),
186-
essence: String::new(),
187-
basetype: String::new(),
188-
subtype: String::new(),
189-
params: Some(ParamKind::Utf8),
190-
static_basetype: Some("text"),
191-
static_subtype: Some("plain"),
192-
};
193-
194-
/// Content-Type for byte streams.
195-
///
196-
/// # Mime Type
197-
///
198-
/// ```txt
199-
/// application/octet-stream
200-
/// ```
201-
pub const BYTE_STREAM: Mime = Mime {
202-
static_essence: Some("application/octet-stream"),
203-
essence: String::new(),
204-
basetype: String::new(),
205-
subtype: String::new(),
206-
static_basetype: Some("application"),
207-
static_subtype: Some("octet-stream"),
208-
params: None,
209-
};
210-
211-
/// Content-Type for form.
212-
///
213-
/// # Mime Type
214-
///
215-
/// ```txt
216-
/// application/x-www-form-urlencoded
217-
/// ```
218-
pub const FORM: Mime = Mime {
219-
static_essence: Some("application/x-www-form-urlencoded"),
220-
essence: String::new(),
221-
basetype: String::new(),
222-
subtype: String::new(),
223-
static_basetype: Some("application"),
224-
static_subtype: Some("x-www-form-urlencoded"),
225-
params: None,
226-
};
227-
228-
/// Content-Type for a multipart form.
229-
///
230-
/// # Mime Type
231-
///
232-
/// ```txt
233-
/// multipart/form-data
234-
/// ```
235-
pub const MULTIPART_FORM: Mime = Mime {
236-
static_essence: Some("multipart/form-data"),
237-
essence: String::new(),
238-
basetype: String::new(),
239-
subtype: String::new(),
240-
static_basetype: Some("multipart"),
241-
static_subtype: Some("form-data"),
242-
params: None,
243-
};
244-
245-
/// Content-Type for webassembly.
246-
///
247-
/// # Mime Type
248-
///
249-
/// ```txt
250-
/// application/wasm
251-
/// ```
252-
pub const WASM: Mime = Mime {
253-
static_essence: Some("application/wasm"),
254-
essence: String::new(),
255-
basetype: String::new(),
256-
subtype: String::new(),
257-
static_basetype: Some("application"),
258-
static_subtype: Some("wasm"),
259-
params: None,
260-
};
63+
mime_const!(ICO, "ICO icons", "image", "x-icon");

0 commit comments

Comments
 (0)