use compmd_pack::{FilePath, Place};
use http::Method;
use crate::refusal::Refusal;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Route {
Frame(FilePath),
Pack(Place),
Refused(Refusal),
}
#[must_use]
pub fn route(method: &Method, path: &str, hash: &str) -> Route {
if method != Method::GET {
return Route::Refused(Refusal::ONLY_GET);
}
file_of(path, hash).unwrap_or(Route::Refused(Refusal::NOT_SERVED))
}
fn file_of(path: &str, hash: &str) -> Option<Route> {
let parts: Vec<&str> = path.strip_prefix('/')?.split('/').collect();
match parts.as_slice() {
["frames", given, file @ ..] if *given == hash => {
FilePath::new(file.join("/")).ok().map(Route::Frame)
}
["packs", place @ ..] => Place::from_parts(place).map(Route::Pack),
_ => None,
}
}