summaryrefslogtreecommitdiff
path: root/src/ui/mime.rs
blob: 9c724f0726351c01206aab026ca9053a4a6341e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use mime::Mime;
use unix_path::Path;

// Extremely manual; using `std::path` here would result in platform-dependent behaviour when it's not appropriate (the URLs passed here always use `/` and are parsed like URLs). Using `unix_path` might be an option, but it's not clearly
pub fn from_path<P>(path: P) -> Result<Mime, mime::FromStrError>
where
    P: AsRef<Path>,
{
    let path = path.as_ref();
    let extension = path.extension().and_then(|ext| ext.to_str());
    let mime = match extension {
        Some("css") => "text/css; charset=utf-8",
        Some("js") => "text/javascript; charset=utf-8",
        Some("json") => "application/json",
        Some("html") => "text/html; charset=utf-8",
        Some("png") => "image/png",
        _ => "application/octet-stream",
    };
    let mime = mime.parse()?;

    Ok(mime)
}