Skip to main content

shared_framework/doc/
assets.rs

1//! Bundled Swagger UI assets served by the documentation controller.
2//!
3//! The files under `doc/static/` are compiled into the binary via
4//! `include_bytes!`, so the documentation UI resolves regardless of the
5//! process working directory. Filesystem lookup under [`FS_DIR`] is kept as a
6//! fallback so locally edited assets are picked up without a rebuild.
7//!
8//! Use [`lookup`] to resolve an asset name to its content type and bytes, or
9//! [`FS_DIR`] when reading the same files from disk.
10
11/// Filesystem directory (relative to the crate root) holding the same assets.
12/// Used as a fallback when the process runs with the crate root as cwd.
13pub const FS_DIR: &str = "doc/static";
14
15/// Returns `(content_type, bytes)` for a bundled asset, or `None` if unknown.
16///
17/// An empty name resolves to `index.html`. The returned content type is the
18/// MIME type to send (for example `"text/html"` or `"text/css"`).
19/// Returns `None` for names with no bundled file.
20pub fn lookup(name: &str) -> Option<(&'static str, &'static [u8])> {
21    let name = name.trim_start_matches('/');
22    let key = if name.is_empty() { "index.html" } else { name };
23    match key {
24        "index.html" => Some(("text/html", include_bytes!("static/index.html"))),
25        "index.css" => Some(("text/css", include_bytes!("static/index.css"))),
26        "swagger-ui.css" => Some(("text/css", include_bytes!("static/swagger-ui.css"))),
27        "swagger-ui.js" => Some(("application/javascript", include_bytes!("static/swagger-ui.js"))),
28        "swagger-ui-bundle.js" => {
29            Some(("application/javascript", include_bytes!("static/swagger-ui-bundle.js")))
30        }
31        "swagger-ui-standalone-preset.js" => Some((
32            "application/javascript",
33            include_bytes!("static/swagger-ui-standalone-preset.js"),
34        )),
35        "swagger-ui-es-bundle.js" => Some((
36            "application/javascript",
37            include_bytes!("static/swagger-ui-es-bundle.js"),
38        )),
39        "swagger-ui-es-bundle-core.js" => Some((
40            "application/javascript",
41            include_bytes!("static/swagger-ui-es-bundle-core.js"),
42        )),
43        "swagger-initializer.js" => Some((
44            "application/javascript",
45            include_bytes!("static/swagger-initializer.js"),
46        )),
47        "oauth2-redirect.html" => {
48            Some(("text/html", include_bytes!("static/oauth2-redirect.html")))
49        }
50        "oauth2-redirect.js" => Some((
51            "application/javascript",
52            include_bytes!("static/oauth2-redirect.js"),
53        )),
54        "favicon-16x16.png" => {
55            Some(("image/png", include_bytes!("static/favicon-16x16.png")))
56        }
57        "favicon-32x32.png" => {
58            Some(("image/png", include_bytes!("static/favicon-32x32.png")))
59        }
60        _ => None,
61    }
62}