use crate::net::http::Response;
use crate::net::httpd::{self, Reply};
const FONTS: &[(&str, &[u8])] = &[
("sixtyfour.woff2", include_bytes!("fonts/sixtyfour.woff2")),
("michroma.woff2", include_bytes!("fonts/michroma.woff2")),
("plex-sans.woff2", include_bytes!("fonts/plex-sans.woff2")),
(
"plex-mono-400.woff2",
include_bytes!("fonts/plex-mono-400.woff2"),
),
(
"plex-mono-500.woff2",
include_bytes!("fonts/plex-mono-500.woff2"),
),
];
pub const UI_CSS: &str = include_str!("ui.css");
pub fn font(name: &str) -> Option<&'static [u8]> {
FONTS
.iter()
.find(|(n, _)| *n == name)
.map(|(_, data)| *data)
}
pub fn get(path: &str) -> Option<&'static [u8]> {
font(path.strip_prefix("/fonts/")?)
}
pub fn reply(path: &str) -> Reply {
Reply::Response(font_response(get(path)))
}
pub fn font_response(font: Option<&'static [u8]>) -> Response {
match font {
Some(data) => {
let mut r = httpd::response(200, "OK", "font/woff2", data.to_vec());
r.headers.set("Cache-Control", "max-age=86400");
r
}
None => httpd::error_response(404, "Not Found"),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serves_every_font_and_nothing_else() {
for (name, data) in FONTS {
assert_eq!(&data[..4], b"wOF2", "{name}");
assert_eq!(get(&format!("/fonts/{name}")), Some(*data));
}
assert_eq!(get("/fonts/"), None);
assert_eq!(get("/fonts/../share.html"), None);
assert_eq!(get("/sixtyfour.woff2"), None);
assert_eq!(font("../fonts.rs"), None);
}
#[test]
fn the_stylesheet_names_only_fonts_there_are() {
let mut n = 0;
for part in UI_CSS.split("url(").skip(1) {
let url = part.split(')').next().unwrap();
let name = url.strip_prefix("fonts/").expect(url);
assert!(font(name).is_some(), "{url}");
n += 1;
}
assert_eq!(n, FONTS.len());
}
}