1pub type Result = std::result::Result<ft_sdk::chr::CHR<Output>, ft_sdk::Error>;
2
3#[derive(Debug)]
4pub enum Output {
5 Redirect(u16, String),
6 Json(serde_json::Value),
7}
8
9impl Output {
10 pub fn map_json<F>(self, f: F) -> Output
11 where
12 F: FnOnce(serde_json::Value) -> serde_json::Value,
13 {
14 match self {
15 Output::Redirect(code, url) => Output::Redirect(code, url),
16 Output::Json(j) => Output::Json(f(j)),
17 }
18 }
19}
20
21impl From<ft_sdk::chr::CHR<Output>>
22 for std::result::Result<http::Response<bytes::Bytes>, ft_sdk::Error>
23{
24 fn from(
25 ft_sdk::chr::CHR {
26 cookies,
27 headers,
28 response,
29 }: ft_sdk::chr::CHR<Output>,
30 ) -> Self {
31 let response = match response {
32 Output::Redirect(code, url) => Ok(::http::Response::builder()
33 .status(code)
34 .header("Location", url)
35 .body("".into())?),
36 Output::Json(j) => ft_sdk::json(j),
37 }?;
38 ft_sdk::chr::chr(cookies, headers, response)
39 }
40}
41
42pub fn json<T: serde::Serialize>(j: T) -> Result {
43 Ok(ft_sdk::chr::CHR::new(Output::Json(serde_json::to_value(
44 j,
45 )?)))
46}
47pub fn permanent_redirect<S: AsRef<str>>(url: S) -> Result {
48 Ok(ft_sdk::chr::CHR::new(Output::Redirect(
49 308,
50 url.as_ref().to_string(),
51 )))
52}
53
54pub fn temporary_redirect<S: AsRef<str>>(url: S) -> Result {
55 Ok(ft_sdk::chr::CHR::new(Output::Redirect(
56 307,
57 url.as_ref().to_string(),
58 )))
59}