@@ -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-
858772impl 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) ]
909786mod tests {
910787 use super :: * ;
0 commit comments