1use super::*;
2use std::collections::BTreeMap;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum FileDeliveryMode {
6 PublicCdn,
7 SignedUrl,
8 AppProxy,
9 LocalOnly,
10}
11
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct PageResponse {
14 pub template: String,
15 pub status: u16,
16}
17
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct FragmentResponse {
20 pub template: String,
21 pub fragment_id: String,
22}
23
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct RedirectResponse {
26 pub location: String,
27 pub status: u16,
28}
29
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct JsonResponse {
32 pub status: u16,
33 pub payload: BTreeMap<String, String>,
34}
35
36#[derive(Debug, Clone, PartialEq, Eq)]
37pub struct FileResponse {
38 pub logical_path: String,
39 pub content_type: String,
40 pub delivery_mode: FileDeliveryMode,
41}
42
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub enum HandlerResponse {
45 Page(PageResponse),
46 Fragment(FragmentResponse),
47 Redirect(RedirectResponse),
48 Json(JsonResponse),
49 File(FileResponse),
50}
51
52#[derive(Debug, Clone, PartialEq, Eq)]
53pub struct HandlerDefinition {
54 pub route_name: String,
55 pub response: HandlerResponse,
56}
57
58impl HandlerDefinition {
59 pub fn page(
60 route_name: impl Into<String>,
61 template: impl Into<String>,
62 ) -> Result<Self, RouteBuildError> {
63 Ok(Self {
64 route_name: validate_route_name(route_name.into())?,
65 response: HandlerResponse::Page(PageResponse {
66 template: validate_template_name(template.into())?,
67 status: 200,
68 }),
69 })
70 }
71
72 pub fn fragment(
73 route_name: impl Into<String>,
74 template: impl Into<String>,
75 fragment_id: impl Into<String>,
76 ) -> Result<Self, RouteBuildError> {
77 Ok(Self {
78 route_name: validate_route_name(route_name.into())?,
79 response: HandlerResponse::Fragment(FragmentResponse {
80 template: validate_template_name(template.into())?,
81 fragment_id: validate_fragment_id(fragment_id.into())?,
82 }),
83 })
84 }
85
86 pub fn redirect(
87 route_name: impl Into<String>,
88 location: impl Into<String>,
89 ) -> Result<Self, RouteBuildError> {
90 Ok(Self {
91 route_name: validate_route_name(route_name.into())?,
92 response: HandlerResponse::Redirect(RedirectResponse {
93 location: validate_route_path(location.into())?,
94 status: 303,
95 }),
96 })
97 }
98
99 pub fn json(
100 route_name: impl Into<String>,
101 payload: BTreeMap<String, String>,
102 ) -> Result<Self, RouteBuildError> {
103 Ok(Self {
104 route_name: validate_route_name(route_name.into())?,
105 response: HandlerResponse::Json(JsonResponse {
106 status: 200,
107 payload,
108 }),
109 })
110 }
111
112 pub fn file(
113 route_name: impl Into<String>,
114 logical_path: impl Into<String>,
115 content_type: impl Into<String>,
116 delivery_mode: FileDeliveryMode,
117 ) -> Result<Self, RouteBuildError> {
118 Ok(Self {
119 route_name: validate_route_name(route_name.into())?,
120 response: HandlerResponse::File(FileResponse {
121 logical_path: validate_template_name(logical_path.into())?,
122 content_type: validate_template_name(content_type.into())?,
123 delivery_mode,
124 }),
125 })
126 }
127}