rocket_include_static_resources/
macros.rs

1/// Used for generating a fairing for static resources.
2#[macro_export]
3macro_rules! static_resources_initializer {
4    ( $($name:expr => $path:expr), * $(,)* ) => {
5        {
6            $crate::StaticResponse::fairing(|resources| {
7                $crate::static_resources_initialize!(
8                    resources
9                    $(, $name => $path)*
10                );
11            })
12        }
13    };
14}
15
16/// Used for quickly creating **GET** route handlers to retrieve static resources.
17#[macro_export]
18macro_rules! static_response_handler {
19    ( $($route:expr => $handler_name:ident => $name:expr), * $(,)* ) => {
20        $(
21            #[get($route)]
22            fn $handler_name(
23                static_resources: &$crate::rocket::State<$crate::StaticContextManager>,
24                etag_if_none_match: $crate::EtagIfNoneMatch,
25            ) -> $crate::StaticResponse {
26                static_resources.build(&etag_if_none_match, $name)
27            }
28        )*
29    };
30}
31
32#[cfg(feature = "cache")]
33/// Used for quickly creating **GET** route handlers to retrieve static resources with cache control.
34#[macro_export]
35macro_rules! cached_static_response_handler {
36    ( $max_age:expr, $must_revalidate:expr ; $($route:expr => $handler_name:ident => $name:expr), * $(,)* ) => {
37        $(
38            #[get($route)]
39            fn $handler_name(
40                static_resources: &$crate::rocket::State<$crate::StaticContextManager>,
41                etag_if_none_match: $crate::EtagIfNoneMatch,
42            ) -> $crate::CacheResponse<$crate::StaticResponse> {
43                let responder =  static_resources.build(&etag_if_none_match, $name);
44
45                $crate::CacheResponse::public_only_release(responder, $max_age, $must_revalidate)
46            }
47        )*
48    };
49    ( $max_age:expr ; $($route:expr => $handler_name:ident => $name:expr), * $(,)* ) => {
50        $crate::cached_static_response_handler! {
51            $max_age, false;
52            $(
53                $route => $handler_name => $name,
54            )*
55        }
56    };
57}