Skip to main content

rama_http/service/web/endpoint/response/
datastar.rs

1use super::{IntoResponse, Script};
2use rama_http_types::{
3    Response,
4    header::{CONTENT_TYPE, HeaderValue},
5};
6
7#[derive(Debug, Clone, Copy)]
8/// Datastar script ready to be served.
9///
10/// Embedded version of a recent enough datastar frontend script
11/// compatible with the datastar support embedded in rama.
12///
13/// Source: <https://cdn.jsdelivr.net/gh/starfederation/datastar@1.0.2/bundles/datastar.js>
14///
15/// Learn more about datastar at <https://data-star.dev/>
16/// or in the book: <https://ramaproxy.org/book/web_servers.html#datastar>.
17pub struct DatastarScript(Script<&'static str>);
18
19impl Default for DatastarScript {
20    fn default() -> Self {
21        Self(Script(include_str!("./datastar.js")))
22    }
23}
24
25impl DatastarScript {
26    #[inline]
27    /// Create a new [`DatastarScript`] ready to be served.
28    #[must_use]
29    pub fn new() -> Self {
30        Default::default()
31    }
32}
33
34impl IntoResponse for DatastarScript {
35    #[inline]
36    fn into_response(self) -> Response {
37        self.0.into_response()
38    }
39}
40
41#[derive(Debug, Clone, Copy)]
42/// Datastar source map ready to be served.
43///
44/// Embedded version of the datastar source map file
45/// for improved debugging experience.
46///
47/// Source: <https://cdn.jsdelivr.net/gh/starfederation/datastar@1.0.2/bundles/datastar.js.map>
48///
49/// Learn more about datastar at <https://data-star.dev/>
50/// or in the book: <https://ramaproxy.org/book/web_servers.html#datastar>.
51pub struct DatastarSourceMap(&'static str);
52
53impl Default for DatastarSourceMap {
54    fn default() -> Self {
55        Self(include_str!("./datastar.js.map"))
56    }
57}
58
59impl DatastarSourceMap {
60    #[inline]
61    /// Create a new [`DatastarSourceMap`] ready to be served.
62    #[must_use]
63    pub fn new() -> Self {
64        Default::default()
65    }
66}
67
68impl IntoResponse for DatastarSourceMap {
69    #[inline]
70    fn into_response(self) -> Response {
71        let mut response = Response::new(self.0.into());
72        response
73            .headers_mut()
74            .insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
75        response
76    }
77}