diff options
| author | Kit La Touche <kit@transneptune.net> | 2024-10-23 21:56:31 -0400 |
|---|---|---|
| committer | Kit La Touche <kit@transneptune.net> | 2024-10-23 21:56:31 -0400 |
| commit | 1f769855df2d9cf2bca883a0475670f227e3678b (patch) | |
| tree | 6c94d9c868eb022588a07245df978478034ac5dd /src/ui/mime.rs | |
| parent | 8f360dd9cc45bb14431238ccc5e3d137c020fa7b (diff) | |
| parent | 461814e5174cef1be3e07b4e4069314e9bcbedd6 (diff) | |
Merge branch 'main' into wip/mobile
Diffstat (limited to 'src/ui/mime.rs')
| -rw-r--r-- | src/ui/mime.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/ui/mime.rs b/src/ui/mime.rs new file mode 100644 index 0000000..9c724f0 --- /dev/null +++ b/src/ui/mime.rs @@ -0,0 +1,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) +} |
