momento_functions/
encode_response_bridge.rs

1use crate::IntoWebResponse;
2use momento_functions_host::encoding::{Encode, Json};
3use momento_functions_wit::function_web::exports::momento::functions::guest_function_web::Response;
4use serde::Serialize;
5
6macro_rules! content_type {
7    ($content_type:expr) => {
8        vec![("content-type".to_string(), $content_type.to_string())]
9            .into_iter()
10            .map(Into::into)
11            .collect()
12    };
13}
14
15impl IntoWebResponse for Vec<u8> {
16    fn response(self) -> Response {
17        Response {
18            status: 200,
19            headers: content_type!("application/octet-stream"),
20            body: self,
21        }
22    }
23}
24
25impl IntoWebResponse for &[u8] {
26    fn response(self) -> Response {
27        Response {
28            status: 200,
29            headers: content_type!("application/octet-stream"),
30            body: self.to_vec(),
31        }
32    }
33}
34
35impl IntoWebResponse for String {
36    fn response(self) -> Response {
37        Response {
38            status: 200,
39            headers: content_type!("text/plain; charset=utf-8"),
40            body: self.into_bytes(),
41        }
42    }
43}
44
45impl IntoWebResponse for &str {
46    fn response(self) -> Response {
47        Response {
48            status: 200,
49            headers: content_type!("text/plain; charset=utf-8"),
50            body: self.to_string().into_bytes(),
51        }
52    }
53}
54
55impl IntoWebResponse for () {
56    fn response(self) -> Response {
57        Response {
58            status: 204,
59            headers: vec![],
60            body: vec![],
61        }
62    }
63}
64
65impl IntoWebResponse for Option<Vec<u8>> {
66    fn response(self) -> Response {
67        Response {
68            status: 200,
69            headers: content_type!("application/octet-stream"),
70            body: self.unwrap_or_default(),
71        }
72    }
73}
74
75impl IntoWebResponse for Option<String> {
76    fn response(self) -> Response {
77        Response {
78            status: 200,
79            headers: content_type!("text/plain; charset=utf-8"),
80            body: self.unwrap_or_default().into_bytes(),
81        }
82    }
83}
84
85impl IntoWebResponse for serde_json::Value {
86    fn response(self) -> Response {
87        match serde_json::to_vec(&self) {
88            Ok(body) => Response {
89                status: 200,
90                headers: content_type!("application/json; charset=utf-8"),
91                body,
92            },
93            Err(e) => Response {
94                status: 500,
95                headers: vec![],
96                body: format!("Failed to encode response: {e}").into(),
97            },
98        }
99    }
100}
101
102impl<T: Serialize> IntoWebResponse for Json<T> {
103    fn response(self) -> Response {
104        match self.try_serialize() {
105            Ok(body) => Response {
106                status: 200,
107                headers: content_type!("application/json; charset=utf-8"),
108                body: body.into(),
109            },
110            Err(e) => Response {
111                status: 500,
112                headers: vec![],
113                body: format!("Failed to encode response: {e}").into(),
114            },
115        }
116    }
117}