Skip to content

Commit efb0f64

Browse files
committed
am i fixing or what
1 parent 2bfd883 commit efb0f64

14 files changed

Lines changed: 208 additions & 204 deletions

File tree

cot-core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::error::error_impl::Error;
1+
pub use crate::error::error_impl::Error;
22

33
pub mod body;
44
/// Error handling types and utilities for Cot applications.

cot-core/src/openapi.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,58 @@ pub trait AsApiOperation<T = ()> {
243243
) -> Option<Operation>;
244244
}
245245

246+
#[macro_export]
247+
macro_rules! impl_as_openapi_operation {
248+
($($ty:ident),*) => {
249+
impl<T, $($ty,)* R, Response> AsApiOperation<($($ty,)*)> for T
250+
where
251+
T: Fn($($ty,)*) -> R + Clone + Send + Sync + 'static,
252+
$($ty: ApiOperationPart,)*
253+
R: for<'a> Future<Output = Response> + Send,
254+
Response: ApiOperationResponse,
255+
{
256+
#[allow(
257+
clippy::allow_attributes,
258+
non_snake_case,
259+
reason = "for the case where there are no FromRequestHead params"
260+
)]
261+
fn as_api_operation(
262+
&self,
263+
route_context: &RouteContext<'_>,
264+
schema_generator: &mut SchemaGenerator,
265+
) -> Option<Operation> {
266+
let mut operation = Operation::default();
267+
268+
$(
269+
$ty::modify_api_operation(
270+
&mut operation,
271+
&route_context,
272+
schema_generator
273+
);
274+
)*
275+
let responses = Response::api_operation_responses(
276+
&mut operation,
277+
&route_context,
278+
schema_generator
279+
);
280+
let operation_responses = operation.responses.get_or_insert_default();
281+
for (response_code, response) in responses {
282+
if let Some(response_code) = response_code {
283+
operation_responses.responses.insert(
284+
response_code,
285+
ReferenceOr::Item(response),
286+
);
287+
} else {
288+
operation_responses.default = Some(ReferenceOr::Item(response));
289+
}
290+
}
291+
292+
Some(operation)
293+
}
294+
}
295+
};
296+
}
297+
246298
pub(crate) trait BoxApiRequestHandler: BoxRequestHandler + AsApiOperation {}
247299

248300
pub(crate) fn into_box_api_request_handler<HandlerParams, ApiParams, H>(

cot-core/src/router.rs

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -508,20 +508,6 @@ impl tower::Service<Request> for RouterService {
508508
}
509509
}
510510

511-
// used in the reverse! macro; not part of public API
512-
#[doc(hidden)]
513-
#[must_use]
514-
pub fn split_view_name(view_name: &str) -> (Option<&str>, &str) {
515-
let colon_pos = view_name.find(':');
516-
if let Some(colon_pos) = colon_pos {
517-
let app_name = &view_name[..colon_pos];
518-
let view_name = &view_name[colon_pos + 1..];
519-
(Some(app_name), view_name)
520-
} else {
521-
(None, view_name)
522-
}
523-
}
524-
525511
/// A route that can be used to route requests to their respective views.
526512
///
527513
/// # Examples
@@ -783,78 +769,6 @@ enum RouteInner {
783769
ApiHandler(Arc<dyn crate::openapi::BoxApiEndpointRequestHandler + Send + Sync>),
784770
}
785771

786-
/// Get a URL for a view by its registered name and given params.
787-
///
788-
/// If the view name has two parts separated by a colon, the first part is
789-
/// considered the app name. If the app name is not provided, the app name of
790-
/// the request is used. This means that if you don't specify the `app_name`,
791-
/// this macro will only return URLs for views in the same app as the current
792-
/// request handler.
793-
///
794-
/// # Return value
795-
///
796-
/// Returns a [`crate::Result<String>`] that contains the URL for the view. You
797-
/// will typically want to append `?` to the macro call to get the URL.
798-
///
799-
/// # Examples
800-
///
801-
/// ```
802-
/// use cot::html::Html;
803-
/// use cot::project::RegisterAppsContext;
804-
/// use cot::{App, AppBuilder, Project, StatusCode, reverse};
805-
/// use cot_core::request::Request;
806-
/// use cot_core::router::{Route, Router};
807-
///
808-
/// async fn home(request: Request) -> cot::Result<Html> {
809-
/// // any of below two lines returns the same:
810-
/// let url = reverse!(request, "home")?;
811-
/// let url = reverse!(request, "my_custom_app:home")?;
812-
///
813-
/// Ok(Html::new(format!(
814-
/// "Hello! The URL for this view is: {}",
815-
/// url
816-
/// )))
817-
/// }
818-
///
819-
/// let router = Router::with_urls([Route::with_handler_and_name("/", home, "home")]);
820-
///
821-
/// struct MyApp;
822-
///
823-
/// impl App for MyApp {
824-
/// fn name(&self) -> &'static str {
825-
/// "my_custom_app"
826-
/// }
827-
///
828-
/// fn router(&self) -> Router {
829-
/// Router::with_urls([Route::with_handler_and_name("/", home, "home")])
830-
/// }
831-
/// }
832-
///
833-
/// struct MyProject;
834-
///
835-
/// impl Project for MyProject {
836-
/// fn register_apps(&self, apps: &mut AppBuilder, context: &RegisterAppsContext) {
837-
/// apps.register_with_views(MyApp, "");
838-
/// }
839-
/// }
840-
/// ```
841-
#[macro_export]
842-
macro_rules! reverse {
843-
($request:expr, $view_name:literal $(, $($key:ident = $value:expr),*)?) => {{
844-
#[allow(
845-
clippy::allow_attributes,
846-
unused_imports,
847-
reason = "allow using either `Request` or `Urls` objects"
848-
)]
849-
use $crate::request::RequestExt;
850-
let (app_name, view_name) = $crate::router::split_view_name($view_name);
851-
let app_name = app_name.or_else(|| $request.app_name());
852-
$request
853-
.router()
854-
.reverse(app_name, view_name, &$crate::reverse_param_map!($( $($key = $value),* )?))
855-
}};
856-
}
857-
858772
impl Debug for RouteInner {
859773
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
860774
match &self {
@@ -868,43 +782,6 @@ impl Debug for RouteInner {
868782
}
869783
}
870784

871-
/// Get a URL for a view by its registered name and given params and return a
872-
/// response with a redirect.
873-
///
874-
/// This macro is a shorthand for creating a response with a redirect to a URL
875-
/// generated by the [`reverse!`] macro.
876-
///
877-
/// # Return value
878-
///
879-
/// Returns a [`crate::Result<Response>`] that contains the URL for
880-
/// the view. You will typically want to append `?` to the macro call to get the
881-
/// [`Response`] object.
882-
///
883-
/// # Examples
884-
///
885-
/// ```
886-
/// use cot::reverse_redirect;
887-
/// use cot_core::request::Request;
888-
/// use cot_core::response::Response;
889-
/// use cot_core::router::{Route, Router};
890-
///
891-
/// async fn infinite_loop(request: Request) -> cot::Result<Response> {
892-
/// Ok(reverse_redirect!(request, "home")?)
893-
/// }
894-
///
895-
/// let router = Router::with_urls([Route::with_handler_and_name("/", infinite_loop, "home")]);
896-
/// ```
897-
#[macro_export]
898-
macro_rules! reverse_redirect {
899-
($request:expr, $view_name:literal $(, $($key:ident = $value:expr),*)?) => {
900-
$crate::reverse!(
901-
$request,
902-
$view_name,
903-
$( $($key = $value),* )?
904-
).map(|url| <$crate::response::Response as $crate::response::ResponseExt>::new_redirect(url))
905-
};
906-
}
907-
908785
#[cfg(test)]
909786
mod tests {
910787
use super::*;

cot/src/admin.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ use std::marker::PhantomData;
99
use askama::Template;
1010
use async_trait::async_trait;
1111
use bytes::Bytes;
12-
use cot_core::error::NotFound;
13-
use cot_core::html::Html;
14-
use cot_core::request::extractors::{FromRequestHead, Path, StaticFiles, UrlQuery};
15-
use cot_core::request::{Request, RequestExt, RequestHead};
16-
use cot_core::response::{IntoResponse, Response};
17-
use cot_core::router::{Router, Urls};
12+
use crate::error::NotFound;
13+
use crate::html::Html;
14+
use crate::request::extractors::{FromRequestHead, Path, StaticFiles, UrlQuery};
15+
use crate::request::{Request, RequestExt, RequestHead};
16+
use crate::response::{IntoResponse, Response};
17+
use crate::reverse_redirect;
18+
use crate::router::{Router, Urls};
1819
/// Implements the [`AdminModel`] trait for a struct.
1920
///
2021
/// This is a simple method for adding a database model to the admin panel.
@@ -32,7 +33,7 @@ use crate::form::{
3233
Form, FormContext, FormErrorTarget, FormField, FormFieldValidationError, FormResult,
3334
};
3435
use crate::static_files::StaticFile;
35-
use crate::{App, Error, Method, RequestHandler, reverse_redirect};
36+
use crate::{App, Error, Method, RequestHandler};
3637

3738
struct AdminAuthenticated<T, H: Send + Sync>(H, PhantomData<fn() -> T>);
3839

cot/src/auth.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use std::sync::{Arc, Mutex, MutexGuard};
1616
/// backwards compatible shim for form Password type.
1717
use async_trait::async_trait;
1818
use chrono::{DateTime, FixedOffset};
19-
use cot_core::error::error_impl::impl_into_cot_error;
20-
use cot_core::request::{Request, RequestExt};
19+
use crate::error::error_impl::impl_into_cot_error;
20+
use crate::request::{Request, RequestExt};
2121
use derive_more::with_trait::Debug;
2222
#[cfg(test)]
2323
use mockall::automock;

cot/src/form.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ use async_trait::async_trait;
3131
use bytes::Bytes;
3232
use chrono::NaiveDateTime;
3333
use chrono_tz::Tz;
34-
use cot_core::error::error_impl::impl_into_cot_error;
35-
use cot_core::headers::{MULTIPART_FORM_CONTENT_TYPE, URLENCODED_FORM_CONTENT_TYPE};
36-
use cot_core::request::{Request, RequestExt};
34+
use crate::error::error_impl::impl_into_cot_error;
35+
use crate::headers::{MULTIPART_FORM_CONTENT_TYPE, URLENCODED_FORM_CONTENT_TYPE};
36+
use crate::request::{Request, RequestExt};
3737
/// Derive the [`Form`] trait for a struct and create a [`FormContext`] for it.
3838
///
3939
/// This macro will generate an implementation of the [`Form`] trait for the

cot/src/openapi.rs

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ use aide::openapi::{
114114
use cot::router::Urls;
115115
use cot_core::handle_all_parameters;
116116
use cot_core::handler::BoxRequestHandler;
117+
use cot_core::impl_as_openapi_operation;
117118
#[doc(inline)]
118119
pub use cot_core::openapi::{RouteContext, AsApiRoute, BoxApiEndpointRequestHandler, AsApiOperation, into_box_api_endpoint_request_handler};
119120
use cot_core::request::extractors::{Path, UrlQuery};
@@ -312,57 +313,6 @@ impl<T> AsApiOperation for NoApi<T> {
312313
}
313314
}
314315

315-
macro_rules! impl_as_openapi_operation {
316-
($($ty:ident),*) => {
317-
impl<T, $($ty,)* R, Response> AsApiOperation<($($ty,)*)> for T
318-
where
319-
T: Fn($($ty,)*) -> R + Clone + Send + Sync + 'static,
320-
$($ty: ApiOperationPart,)*
321-
R: for<'a> Future<Output = Response> + Send,
322-
Response: ApiOperationResponse,
323-
{
324-
#[allow(
325-
clippy::allow_attributes,
326-
non_snake_case,
327-
reason = "for the case where there are no FromRequestHead params"
328-
)]
329-
fn as_api_operation(
330-
&self,
331-
route_context: &RouteContext<'_>,
332-
schema_generator: &mut SchemaGenerator,
333-
) -> Option<Operation> {
334-
let mut operation = Operation::default();
335-
336-
$(
337-
$ty::modify_api_operation(
338-
&mut operation,
339-
&route_context,
340-
schema_generator
341-
);
342-
)*
343-
let responses = Response::api_operation_responses(
344-
&mut operation,
345-
&route_context,
346-
schema_generator
347-
);
348-
let operation_responses = operation.responses.get_or_insert_default();
349-
for (response_code, response) in responses {
350-
if let Some(response_code) = response_code {
351-
operation_responses.responses.insert(
352-
response_code,
353-
ReferenceOr::Item(response),
354-
);
355-
} else {
356-
operation_responses.default = Some(ReferenceOr::Item(response));
357-
}
358-
}
359-
360-
Some(operation)
361-
}
362-
}
363-
};
364-
}
365-
366316
handle_all_parameters!(impl_as_openapi_operation);
367317

368318
/// A trait that can be implemented for types that should be taken into

cot/src/openapi/method.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ use std::fmt::{Debug, Formatter};
88

99
use aide::openapi::Operation;
1010
use cot::openapi::{
11-
AsApiOperation, AsApiRoute, BoxApiRequestHandler, RouteContext, into_box_api_request_handler,
11+
AsApiOperation, AsApiRoute, RouteContext
1212
};
1313
use cot::request::Request;
14-
use cot_core::router::method::{InnerHandler, InnerMethodRouter};
1514
use schemars::SchemaGenerator;
1615

1716
use crate::handler::RequestHandler;

cot/src/project.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ use std::sync::Arc;
2828
use askama::Template;
2929
use async_trait::async_trait;
3030
use axum::handler::HandlerWithoutStateExt;
31-
use cot_core::error::UncaughtPanic;
32-
use cot_core::error::error_impl::impl_into_cot_error;
33-
use cot_core::error::handler::{DynErrorPageHandler, RequestOuterError};
34-
use cot_core::handler::BoxedHandler;
35-
use cot_core::html::Html;
36-
use cot_core::middleware::{
31+
use crate::error::UncaughtPanic;
32+
use crate::error::error_impl::impl_into_cot_error;
33+
use crate::error::handler::{DynErrorPageHandler, RequestOuterError};
34+
use crate::handler::BoxedHandler;
35+
use crate::html::Html;
36+
use crate::middleware::{
3737
IntoCotError, IntoCotErrorLayer, IntoCotResponse, IntoCotResponseLayer,
3838
};
39-
use cot_core::request::{AppName, Request, RequestExt, RequestHead};
40-
use cot_core::response::{IntoResponse, Response};
41-
use cot_core::router::{Route, Router, RouterService};
39+
use crate::request::{AppName, Request, RequestExt, RequestHead};
40+
use crate::response::{IntoResponse, Response};
41+
use crate::router::{Route, Router, RouterService};
4242
use derive_more::with_trait::Debug;
4343
use futures_util::FutureExt;
4444
use thiserror::Error;

cot/src/request/extractors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use cot::json::Json;
77
use cot::router::Urls;
88
use cot::session::Session;
99
#[doc(inline)]
10-
pub use cot_core::request::extractors::{FromRequest, FromRequestHead};
10+
pub use cot_core::request::extractors::{FromRequest, FromRequestHead, Path, UrlQuery};
1111
use cot_core::request::{Request, RequestHead};
1212
use cot_core::{Body, impl_into_cot_error};
1313
use serde::de::DeserializeOwned;

0 commit comments

Comments
 (0)