use super::preclude::*; pub const VERSION: [u8; 4] = [0, 0, 1, 0]; pub struct VersionContext { ctx: Arc, } impl VersionContext { pub fn new(ctx: Arc) -> Self { Self { ctx } } } #[async_trait] impl ResponseJsonFor for VersionContext { async fn response_json( &self, req: Request, ) -> Result, PixivDownloaderError> { filter_http_methods!( req, json::object! {}, true, self.ctx, allow_headers = [CONTENT_TYPE], GET, OPTIONS, POST, ); Ok(builder.body(json::object! {"version": VERSION.to_vec()})?) } } pub struct VersionRoute { regex: Regex, } impl VersionRoute { pub fn new() -> Self { Self { regex: Regex::new(r"^(/+api)?/+version(/.*)?$").unwrap(), } } } impl MatchRoute for VersionRoute { fn match_route( &self, ctx: &Arc, req: &http::Request, ) -> Option> { if self.regex.is_match(req.uri().path()) { Some(Box::new(VersionContext::new(Arc::clone(ctx)))) } else { None } } }