Skip to main content

coil_core/manifest/
route.rs

1use super::*;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum RouteSurfaceKind {
5    FrontendPage,
6    FrontendAction,
7    AdminPage,
8    AdminAction,
9    Api,
10    Fragment,
11    Asset,
12    Webhook,
13}
14
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct RouteSurface {
17    pub name: String,
18    pub kind: RouteSurfaceKind,
19    pub path: String,
20    pub localized: bool,
21    pub capability: Option<Capability>,
22}
23
24impl RouteSurface {
25    pub fn new(name: impl Into<String>, kind: RouteSurfaceKind, path: impl Into<String>) -> Self {
26        Self {
27            name: name.into(),
28            kind,
29            path: path.into(),
30            localized: false,
31            capability: None,
32        }
33    }
34
35    pub fn localized(mut self) -> Self {
36        self.localized = true;
37        self
38    }
39
40    pub fn gated_by(mut self, capability: Capability) -> Self {
41        self.capability = Some(capability);
42        self
43    }
44}
45
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub enum HttpSurfaceArea {
48    Public,
49    Account,
50    Admin,
51    Api,
52    Fragment,
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56pub enum HttpSurfaceMethod {
57    Get,
58    Head,
59    Post,
60    Put,
61    Patch,
62    Delete,
63}
64
65#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66pub enum HttpFileDeliveryMode {
67    PublicCdn,
68    SignedUrl,
69    AppProxy,
70    LocalOnly,
71}
72
73#[derive(Debug, Clone, PartialEq, Eq)]
74pub enum HttpResponseContract {
75    Page {
76        template: String,
77        status: u16,
78    },
79    Fragment {
80        template: String,
81        fragment_id: String,
82    },
83    Redirect {
84        location: String,
85        status: u16,
86    },
87    Json {
88        status: u16,
89        payload: BTreeMap<String, String>,
90    },
91    File {
92        logical_path: String,
93        content_type: String,
94        delivery_mode: HttpFileDeliveryMode,
95    },
96}
97
98#[derive(Debug, Clone, PartialEq, Eq)]
99pub struct HttpSurfaceContribution {
100    pub name: String,
101    pub method: HttpSurfaceMethod,
102    pub path: String,
103    pub area: HttpSurfaceArea,
104    pub localized: bool,
105    pub capability: Option<Capability>,
106    pub response: HttpResponseContract,
107}
108
109impl HttpSurfaceContribution {
110    pub fn page(
111        name: impl Into<String>,
112        area: HttpSurfaceArea,
113        path: impl Into<String>,
114        template: impl Into<String>,
115    ) -> Self {
116        Self {
117            name: name.into(),
118            method: HttpSurfaceMethod::Get,
119            path: path.into(),
120            area,
121            localized: false,
122            capability: None,
123            response: HttpResponseContract::Page {
124                template: template.into(),
125                status: 200,
126            },
127        }
128    }
129
130    pub fn fragment(
131        name: impl Into<String>,
132        path: impl Into<String>,
133        template: impl Into<String>,
134        fragment_id: impl Into<String>,
135    ) -> Self {
136        Self {
137            name: name.into(),
138            method: HttpSurfaceMethod::Get,
139            path: path.into(),
140            area: HttpSurfaceArea::Fragment,
141            localized: false,
142            capability: None,
143            response: HttpResponseContract::Fragment {
144                template: template.into(),
145                fragment_id: fragment_id.into(),
146            },
147        }
148    }
149
150    pub fn json(
151        name: impl Into<String>,
152        method: HttpSurfaceMethod,
153        area: HttpSurfaceArea,
154        path: impl Into<String>,
155        status: u16,
156        payload: BTreeMap<String, String>,
157    ) -> Self {
158        Self {
159            name: name.into(),
160            method,
161            path: path.into(),
162            area,
163            localized: false,
164            capability: None,
165            response: HttpResponseContract::Json { status, payload },
166        }
167    }
168
169    pub fn redirect(
170        name: impl Into<String>,
171        method: HttpSurfaceMethod,
172        area: HttpSurfaceArea,
173        path: impl Into<String>,
174        location: impl Into<String>,
175        status: u16,
176    ) -> Self {
177        Self {
178            name: name.into(),
179            method,
180            path: path.into(),
181            area,
182            localized: false,
183            capability: None,
184            response: HttpResponseContract::Redirect {
185                location: location.into(),
186                status,
187            },
188        }
189    }
190
191    pub fn file(
192        name: impl Into<String>,
193        area: HttpSurfaceArea,
194        path: impl Into<String>,
195        logical_path: impl Into<String>,
196        content_type: impl Into<String>,
197        delivery_mode: HttpFileDeliveryMode,
198    ) -> Self {
199        Self {
200            name: name.into(),
201            method: HttpSurfaceMethod::Get,
202            path: path.into(),
203            area,
204            localized: false,
205            capability: None,
206            response: HttpResponseContract::File {
207                logical_path: logical_path.into(),
208                content_type: content_type.into(),
209                delivery_mode,
210            },
211        }
212    }
213
214    pub fn localized(mut self) -> Self {
215        self.localized = true;
216        self
217    }
218
219    pub fn gated_by(mut self, capability: Capability) -> Self {
220        self.capability = Some(capability);
221        self
222    }
223}