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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use crate::data::{CsmlFlow, Position};
use crate::error_format::*;
use crate::Interval;
use serde::{Deserialize, Serialize};

////////////////////////////////////////////////////////////////////////////////
// DATA STRUCTURE
////////////////////////////////////////////////////////////////////////////////

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CsmlBot {
    pub id: String,
    pub name: String,
    #[serde(alias = "fn_endpoint")]
    pub apps_endpoint: Option<String>,
    pub flows: Vec<CsmlFlow>,
    pub modules: Option<Vec<Module>>,
    pub multibot: Option<Vec<MultiBot>>,
    pub native_components: Option<serde_json::Map<String, serde_json::Value>>,
    pub custom_components: Option<serde_json::Value>,
    pub default_flow: String,
    pub bot_ast: Option<String>,
    pub no_interruption_delay: Option<i32>,
    pub env: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Module {
    pub name: String,
    pub url: Option<String>,
    pub auth: Option<String>,
    #[serde(default = "default_version")]
    pub version: String,
    pub flow: Option<CsmlFlow>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MultiBot {
    pub id: String,
    pub name: Option<String>,
    pub version_id: Option<String>,
}

fn default_version() -> String {
    "latest".to_string()
}

////////////////////////////////////////////////////////////////////////////////
// STATIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////

impl CsmlBot {
    pub fn new(
        id: &str,
        name: &str,
        apps_endpoint: Option<String>,
        flows: Vec<CsmlFlow>,
        native_components: Option<serde_json::Map<String, serde_json::Value>>,
        custom_components: Option<serde_json::Value>,
        default_flow: &str,
        bot_ast: Option<String>,
        no_interruption_delay: Option<i32>,
        env: Option<serde_json::Value>,
        modules: Option<Vec<Module>>,
        multibot: Option<Vec<MultiBot>>,
    ) -> Self {
        Self {
            id: id.to_owned(),
            name: name.to_owned(),
            apps_endpoint,
            flows,
            modules,
            multibot,
            native_components,
            custom_components,
            default_flow: default_flow.to_owned(),
            bot_ast,
            no_interruption_delay,
            env,
        }
    }

    pub fn get_default_flow_name(&self) -> String {
        match self.flows.iter().find(|&val| {
            val.id.to_ascii_lowercase() == self.default_flow.to_ascii_lowercase()
                || val.name.to_ascii_lowercase() == self.default_flow.to_ascii_lowercase()
        }) {
            Some(ref f) => f.name.to_owned(),
            None => unreachable!("default_flow should always be found"),
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
// METHOD FUNCTIONS
////////////////////////////////////////////////////////////////////////////////

impl CsmlBot {
    pub fn get_flow(&self, name: &str) -> Result<String, Vec<ErrorInfo>> {
        for flow in self.flows.iter() {
            if flow.name == name {
                return Ok(flow.content.to_owned());
            }
        }

        Err(vec![gen_error_info(
            Position::new(Interval::new_as_u32(0, 0, 0, None, None), name),
            format!("{} {}", ERROR_INVALID_FLOW, name),
        )])
    }

    pub fn to_json(&self) -> serde_json::Value {
        let mut map: serde_json::Map<String, serde_json::Value> = serde_json::Map::new();

        map.insert("id".to_owned(), serde_json::json!(self.id));
        map.insert("name".to_owned(), serde_json::json!(self.name));
        map.insert(
            "apps_endpoint".to_owned(),
            serde_json::json!(self.apps_endpoint),
        );
        map.insert("flows".to_owned(), serde_json::json!(self.flows));
        map.insert(
            "default_flow".to_owned(),
            serde_json::json!(self.default_flow),
        );
        map.insert(
            "no_interruption_delay".to_owned(),
            serde_json::json!(self.no_interruption_delay),
        );
        map.insert("env".to_owned(), serde_json::json!(self.env));

        serde_json::json!(map)
    }
}