1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#![forbid(unsafe_code)]

extern crate self as ft_sdk;

mod cookie;
mod crypto;
mod in_;
mod json_body;
mod layout;
mod query;

pub use cookie::CookieExt;
pub use crypto::{DecryptionError, EncryptedString, PlainText};
pub use ft_sys::{env, println, PgConnection};
pub use ft_sys::{http, UserData};
pub use in_::In;
pub use json_body::{JsonBody, JsonBodyExt};
pub use layout::{Action, ActionOutput, Layout, Page, RequestType};
pub use query::{Query, QueryExt};

pub fn default_pg() -> Result<PgConnection, Error> {
    use diesel::Connection;
    Ok(PgConnection::establish("default")?)
}

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("serde_json error {0}")]
    Serde(#[from] serde_json::Error),

    #[error("diesel error {0}")]
    Diesel(#[from] diesel::result::Error),

    #[error("diesel connection error {0}")]
    DieselConnection(#[from] diesel::result::ConnectionError),
}

#[macro_export]
macro_rules! not_found {
    ($($t:tt)*) => {{
        let msg = format!($($t)*);
        ft_sdk::println!("not-found: {msg}");
        ::http::Response::builder()
            .status(::http::StatusCode::NOT_FOUND)
            .body(bytes::Bytes::from(msg + "\n"))
            .unwrap()
    }};
}

#[macro_export]
macro_rules! server_error {
    ($($t:tt)*) => {{
        let msg = format!($($t)*);
        ft_sdk::println!("server-error: {msg}");
        ::http::Response::builder()
            .status(::http::StatusCode::INTERNAL_SERVER_ERROR)
            .body(bytes::Bytes::from(msg + "\n"))
            .unwrap()
    }};
}

pub fn json_response<T: serde::Serialize>(t: T) -> ::http::Response<bytes::Bytes> {
    ::http::Response::builder()
        .status(::http::StatusCode::OK)
        .header("Content-Type", "application/json")
        .body(bytes::Bytes::from(serde_json::to_vec(&t).unwrap()))
        .unwrap()
}