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
//! The response for Slack Slash Command.

use sfr_types as st;

use crate::response::{CT_APPLICATION_JSON, CT_TEXT_PLAIN};
use axum::body::Body;
use axum::http::status::StatusCode;
use axum::http::Response;
use axum::response::IntoResponse;

/// The response for Slack Slash Command.
#[derive(Debug)]
pub struct SlashCommandResponse(st::SlashCommandResponse);

impl From<st::SlashCommandResponse> for SlashCommandResponse {
    fn from(r: st::SlashCommandResponse) -> Self {
        Self(r)
    }
}

impl IntoResponse for SlashCommandResponse {
    fn into_response(self) -> Response<Body> {
        match self.0 {
            st::SlashCommandResponse::Empty => return ([CT_TEXT_PLAIN], "").into_response(),
            st::SlashCommandResponse::String(s) => return ([CT_TEXT_PLAIN], s).into_response(),
            st::SlashCommandResponse::Layouts(_)
            | st::SlashCommandResponse::LayoutsInChannel(_) => {}
        }

        let body = match serde_json::to_string(&self.0) {
            Ok(body) => body,
            Err(e) => {
                tracing::warn!("failed to serialise to build layouts: {e:?}");
                return (StatusCode::INTERNAL_SERVER_ERROR, "").into_response();
            }
        };

        tracing::trace!("SlashCommandResponse::into_response: body = {body}");
        ([CT_APPLICATION_JSON], body).into_response()
    }
}