From 3c95e55df2e66e5c0d902dfabf9bed9ab0bd0777 Mon Sep 17 00:00:00 2001 From: Simeon Romanov Date: Sun, 14 Jan 2024 03:45:12 +0300 Subject: [PATCH] tweak flags --- backend/src/lib.rs | 1 + backend/src/models/shared.rs | 7 ++-- backend/src/router.rs | 78 +++++++++++++++++++++++++++--------- 3 files changed, 63 insertions(+), 23 deletions(-) diff --git a/backend/src/lib.rs b/backend/src/lib.rs index c009b995..a861a7db 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -3,6 +3,7 @@ #[cfg(all(feature = "swagger", debug_assertions))] use axum::{routing::get, Router}; +#[allow(unused_imports)] use prelude::*; pub mod config; diff --git a/backend/src/models/shared.rs b/backend/src/models/shared.rs index 50f2f763..24d3f007 100644 --- a/backend/src/models/shared.rs +++ b/backend/src/models/shared.rs @@ -64,9 +64,10 @@ impl ToString for Hostname { } /// Data needed to connect to a BOB cluster -#[derive(IntoParams, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] -#[schema(example = json!({"hostname": "0.0.0.0:7000", "credentials": {"login": "archeoss", "password": "12345"}}))] -#[cfg_attr(all(feature = "swagger", debug_assertions), derive(ToSchema))] +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[cfg_attr(all(feature = "swagger", debug_assertions), + schema(example = json!({"hostname": "0.0.0.0:7000", "credentials": {"login": "archeoss", "password": "12345"}})))] +#[cfg_attr(all(feature = "swagger", debug_assertions), derive(IntoParams, ToSchema))] pub struct BobConnectionData { /// Address to connect to pub hostname: Hostname, diff --git a/backend/src/router.rs b/backend/src/router.rs index 2d52cbf9..de9d964e 100644 --- a/backend/src/router.rs +++ b/backend/src/router.rs @@ -92,40 +92,82 @@ where } /// Add API Route to the `Router` - /// #[must_use] - pub fn api_route(mut self, path: &str, method: &Method, handler: H) -> Self + #[cfg(not(all(feature = "swagger", debug_assertions)))] + pub fn api_route(self, path: &str, method: &Method, handler: H) -> Self where H: Handler, T: 'static, S: Clone + Send + Sync + 'static, B: HttpBody + Send + 'static, - Doc: OpenApi, { - #[cfg(all(feature = "swagger", debug_assertions))] - match try_convert_path_item_type_from_method(method) - .map(|path_item_type| check_api::<_, _, _, H, Version, Doc>(path, &path_item_type)) - { - Ok(Ok(())) => (), - Ok(Err(err)) | Err(err) => { + self.apply_handler::(path, method, handler) + } + + fn apply_handler(mut self, path: &str, method: &Method, handler: H) -> Self + where + H: Handler, + T: 'static, + S: Clone + Send + Sync + 'static, + B: HttpBody + Send + 'static, + { + match try_convert_method_filter_from_method(method) { + Ok(method) => self.inner = self.inner.route(path, on(method, handler)), + Err(err) => { if let Some(errors) = &mut self.api_errors { errors.extend_one(err); } else { self.api_errors = Some(err); } } - } + }; - match try_convert_method_filter_from_method(method) { - Ok(method) => self.inner = self.inner.route(path, on(method, handler)), - Err(err) => { + self + } +} + +#[cfg(all(feature = "swagger", debug_assertions))] +impl<'a, Version, Doc, S, B> ContextRouter +where + Version: ApiVersion<'a>, + Doc: OpenApi, + B: HttpBody + Send + 'static, + S: Clone + Send + Sync + 'static, +{ + /// Add API Route to the `Router` + #[must_use] + pub fn api_route(self, path: &str, method: &Method, handler: H) -> Self + where + H: Handler, + T: 'static, + S: Clone + Send + Sync + 'static, + B: HttpBody + Send + 'static, + { + // #[cfg(all(feature = "swagger", debug_assertions))] + self.check_api::(path, method) + .apply_handler::(path, method, handler) + } + + fn check_api(mut self, path: &str, method: &Method) -> Self + where + H: Handler, + T: 'static, + S: Clone + Send + Sync + 'static, + B: HttpBody + Send + 'static, + Doc: OpenApi, + { + match try_convert_path_item_type_from_method(method) + .map(|path_item_type| check_api::<_, _, _, H, Version, Doc>(path, &path_item_type)) + { + Ok(Ok(())) => (), + Ok(Err(err)) | Err(err) => { if let Some(errors) = &mut self.api_errors { errors.extend_one(err); } else { self.api_errors = Some(err); } } - }; + } self } @@ -143,9 +185,7 @@ impl Deref for ContextRouter { pub trait RouterApiExt { /// Wraps `Router` with `ApiVersion` and `OpenApi` instances into the new context to call /// `api_route` with said context - fn with_context<'a, Version: ApiVersion<'a>, Doc: OpenApi>( - self, - ) -> ContextRouter; + fn with_context<'a, Version: ApiVersion<'a>, Doc>(self) -> ContextRouter; } #[cfg(not(all(feature = "swagger", debug_assertions)))] @@ -161,9 +201,7 @@ where B: HttpBody + Send + 'static, S: Clone + Send + Sync + 'static, { - fn with_context<'a, Version: ApiVersion<'a>, Doc: OpenApi>( - self, - ) -> ContextRouter { + fn with_context<'a, Version: ApiVersion<'a>, Doc>(self) -> ContextRouter { ContextRouter::new(self) } }