Skip to content

Commit

Permalink
tweak flags
Browse files Browse the repository at this point in the history
  • Loading branch information
archeoss committed Jan 14, 2024
1 parent 13004b4 commit 3c95e55
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 23 deletions.
1 change: 1 addition & 0 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#[cfg(all(feature = "swagger", debug_assertions))]
use axum::{routing::get, Router};

#[allow(unused_imports)]
use prelude::*;

pub mod config;
Expand Down
7 changes: 4 additions & 3 deletions backend/src/models/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"}})))]

Check warning on line 69 in backend/src/models/shared.rs

View workflow job for this annotation

GitHub Actions / gen-openapi

derive helper attribute is used before it is introduced
#[cfg_attr(all(feature = "swagger", debug_assertions), derive(IntoParams, ToSchema))]
pub struct BobConnectionData {
/// Address to connect to
pub hostname: Hostname,
Expand Down
78 changes: 58 additions & 20 deletions backend/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,40 +92,82 @@ where
}

/// Add API Route to the `Router`
///
#[must_use]
pub fn api_route<H, T>(mut self, path: &str, method: &Method, handler: H) -> Self
#[cfg(not(all(feature = "swagger", debug_assertions)))]
pub fn api_route<H, T>(self, path: &str, method: &Method, handler: H) -> Self
where
H: Handler<T, S, B>,
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::<H, T>(path, method, handler)
}

fn apply_handler<H, T>(mut self, path: &str, method: &Method, handler: H) -> Self
where
H: Handler<T, S, B>,
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<Version, Doc, S, B>
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<H, T>(self, path: &str, method: &Method, handler: H) -> Self
where
H: Handler<T, S, B>,
T: 'static,
S: Clone + Send + Sync + 'static,
B: HttpBody + Send + 'static,
{
// #[cfg(all(feature = "swagger", debug_assertions))]
self.check_api::<H, T>(path, method)
.apply_handler::<H, T>(path, method, handler)
}

fn check_api<H, T>(mut self, path: &str, method: &Method) -> Self
where
H: Handler<T, S, B>,
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
}
Expand All @@ -143,9 +185,7 @@ impl<V, D, S, B> Deref for ContextRouter<V, D, S, B> {
pub trait RouterApiExt<S = (), B = Body, E = Infallible> {
/// 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<Version, Doc, S, B>;
fn with_context<'a, Version: ApiVersion<'a>, Doc>(self) -> ContextRouter<Version, Doc, S, B>;
}

#[cfg(not(all(feature = "swagger", debug_assertions)))]
Expand All @@ -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<Version, Doc, S, B> {
fn with_context<'a, Version: ApiVersion<'a>, Doc>(self) -> ContextRouter<Version, Doc, S, B> {
ContextRouter::new(self)
}
}
Expand Down

0 comments on commit 3c95e55

Please sign in to comment.