mikros 0.3.0

An optionated crate to help building multi-purpose applications.
Documentation
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
pub(crate) mod errors;
mod name;
pub mod service;
mod validation;

use std::cmp::PartialEq;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use std::sync::Arc;

use serde::de::DeserializeOwned;
use validator::ValidateArgs;

use crate::definition::name::ServiceName;

// ServiceInfo represents the service information loaded from the 'service.toml'
// file.
#[derive(serde_derive::Deserialize, validator::Validate, Debug)]
#[validate(context = CustomServiceInfo)]
#[validate(schema(
    function = "validation::validate_service_info",
    skip_on_field_errors = false,
    use_context
))]
pub struct Definitions {
    pub name: ServiceName,
    pub version: String,
    pub language: String,
    pub product: String,
    pub envs: Option<Vec<String>>,
    log: Option<Log>,

    features: Option<HashMap<String, serde_json::Value>>,
    services: Option<HashMap<String, serde_json::Value>>,
    clients: Option<HashMap<String, Client>>,
    service: Option<serde_json::Value>,

    #[serde(deserialize_with = "service::deserialize_services")]
    pub types: Vec<service::Service>,
}

#[derive(serde_derive::Deserialize, Debug, Clone)]
pub struct Log {
    pub level: Option<String>,
    pub local_timestamp: Option<bool>,
    pub display_errors: Option<bool>,
}

impl Default for Log {
    fn default() -> Self {
        Log {
            level: Some("info".to_string()),
            local_timestamp: Some(true),
            display_errors: Some(true),
        }
    }
}

impl Log {
    fn merge(&mut self, other: Log) {
        if self.level.is_none() {
            self.level = other.level;
        }

        if self.local_timestamp.is_none() {
            self.local_timestamp = other.local_timestamp;
        }

        if self.display_errors.is_none() {
            self.display_errors = other.display_errors;
        }
    }
}

#[derive(serde_derive::Deserialize, Debug, Clone)]
pub struct Client {
    pub host: String,
    pub port: i32,
}

#[derive(serde_derive::Deserialize, Clone, Debug, PartialEq)]
pub enum ServiceKind {
    Grpc,
    Http,
    Script,
    Native,
    Custom(String),
}

impl FromStr for ServiceKind {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "grpc" => Ok(ServiceKind::Grpc),
            "http" => Ok(ServiceKind::Http),
            "native" => Ok(ServiceKind::Native),
            "script" => Ok(ServiceKind::Script),
            _ => Ok(ServiceKind::Custom(s.to_string())),
        }
    }
}

impl Display for ServiceKind {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ServiceKind::Grpc => write!(f, "grpc"),
            ServiceKind::Http => write!(f, "http"),
            ServiceKind::Native => write!(f, "native"),
            ServiceKind::Script => write!(f, "script"),
            ServiceKind::Custom(s) => write!(f, "{s}"),
        }
    }
}

#[derive(Default)]
pub struct CustomServiceInfo {
    pub types: Option<Vec<String>>,
}

impl Definitions {
    pub(crate) fn new(
        filename: Option<&str>,
        custom_info: Option<CustomServiceInfo>,
    ) -> Result<Arc<Self>, errors::Error> {
        let info: Definitions = match toml::from_str(&Self::load_service_file(filename)?) {
            Ok(content) => content,
            Err(e) => return Err(errors::Error::MalformedToml(e.to_string())),
        };

        info.validate(custom_info)?;
        Ok(Arc::new(info))
    }

    fn load_service_file(filename: Option<&str>) -> Result<String, errors::Error> {
        let path = Self::get_service_file_path(filename)?;

        match std::fs::read_to_string(path.as_path()) {
            Ok(content) => Ok(content),
            Err(e) => Err(errors::Error::CouldNotLoadFile(e.to_string())),
        }
    }

    fn get_service_file_path(filename: Option<&str>) -> Result<std::path::PathBuf, errors::Error> {
        if let Some(filename) = filename {
            return Ok(std::path::Path::new(filename).to_path_buf());
        }

        match std::env::current_dir() {
            Ok(mut p) => {
                p.push("service.toml");
                Ok(p)
            }
            Err(r) => Err(errors::Error::DefinitionFileNotFound(r.to_string())),
        }
    }

    fn validate(&self, custom_info: Option<CustomServiceInfo>) -> Result<(), errors::Error> {
        let context = custom_info.unwrap_or_else(|| {
            let c: CustomServiceInfo = CustomServiceInfo::default();
            c
        });

        match self.validate_with_args(&context) {
            Err(e) => Err(errors::Error::InvalidDefinitions(e.to_string())),
            Ok(()) => {
                if self.types.is_empty() {
                    Err(errors::Error::EmptyServiceType)
                } else {
                    Ok(())
                }
            }
        }
    }

    #[allow(clippy::needless_pass_by_value)]
    pub(crate) fn get_service_type(
        &self,
        kind: ServiceKind,
    ) -> Result<&service::Service, errors::Error> {
        match self.types.iter().find(|t| t.0 == kind) {
            Some(t) => Ok(t),
            None => Err(errors::Error::ServiceNotFound(kind.to_string())),
        }
    }

    pub(crate) fn log(&self) -> Log {
        match &self.log {
            None => Log::default(),
            Some(log) => {
                let mut log = log.clone();
                log.merge(Log::default());
                log
            }
        }
    }

    /// Loads definitions from a feature.
    pub fn load_feature<T>(&self, feature: &str) -> Option<T>
    where
        T: DeserializeOwned,
    {
        Self::decode(self.feature(feature))
    }

    fn feature(&self, feature: &str) -> Option<serde_json::Value> {
        match &self.features {
            None => None,
            Some(features) => features.get(feature).cloned(),
        }
    }

    pub fn load_service<T>(&self, service_kind: ServiceKind) -> Option<T>
    where
        T: DeserializeOwned,
    {
        Self::decode(self.service(&service_kind))
    }

    fn service(&self, service_kind: &ServiceKind) -> Option<serde_json::Value> {
        match &self.services {
            None => None,
            Some(services) => services.get(&service_kind.to_string()).cloned(),
        }
    }

    fn decode<T>(data: Option<serde_json::Value>) -> Option<T>
    where
        T: DeserializeOwned,
    {
        if let Some(d) = data {
            return serde_json::from_value::<T>(d.clone()).ok()
        }

        None
    }

    pub fn client(&self, name: &str) -> Option<Client> {
        self.clients.clone()?.get(name).cloned()
    }

    pub fn custom_settings<T>(&self) -> Option<T>
    where
        T: DeserializeOwned,
    {
        match &self.service {
            None => None,
            Some(settings) => serde_json::from_value::<T>(settings.clone()).ok()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use mikros_tests::common::assets_path;
    use serde_derive::Deserialize;

    #[test]
    fn test_load_service_file_with_invalid_settings() {
        let filename = assets_path().join("definitions/service.toml.err_unsupported_type");
        let defs = Definitions::new(filename.to_str(), None);
        assert!(defs.is_err());
    }

    #[test]
    fn test_load_service_file_with_invalid_toml() {
        let filename = assets_path().join("definitions/service.toml.err_invalid_toml");
        let defs = Definitions::new(filename.to_str(), None);
        assert!(defs.is_err());
    }

    #[test]
    fn test_load_service_file_ok() {
        let filename = assets_path().join("definitions/service.toml.ok");
        let defs = Definitions::new(filename.to_str(), None);
        assert!(defs.is_ok());
        assert_eq!(defs.unwrap().types.len(), 1);
    }

    #[test]
    fn test_load_service_file_ok_hybrid() {
        let filename = assets_path().join("definitions/service.toml.ok_hybrid");
        let defs = Definitions::new(filename.to_str(), None);
        assert!(defs.is_ok());

        match defs {
            Ok(info) => {
                assert_eq!(info.types.len(), 2);
                assert_eq!(info.envs.clone().unwrap().len(), 2);
            }
            Err(_) => assert!(false),
        }
    }

    #[test]
    fn test_load_service_file_with_custom_supported_type() {
        let custom_info = CustomServiceInfo {
            types: Some(vec!["websocket".to_string()]),
        };

        let filename = assets_path().join("definitions/service.toml.err_unsupported_type");
        let defs = Definitions::new(filename.to_str(), Some(custom_info));
        assert!(defs.is_ok());
        assert_eq!(defs.unwrap().types.len(), 1);
    }

    #[test]
    fn test_load_features_settings() {
        let filename = assets_path().join("definitions/service.toml.ok");
        let defs = Definitions::new(filename.to_str(), None);
        assert!(defs.is_ok());

        let defs = defs.unwrap();

        #[derive(Deserialize)]
        struct SimpleApi {
            enabled: bool,
            collections: Vec<String>,
        }

        let s: Option<SimpleApi> = defs.clone().load_feature("simple_api");
        assert!(s.is_some());

        let simple_api = s.unwrap();
        assert_eq!(simple_api.collections.len(), 2);
        assert_eq!(simple_api.enabled, true);

        #[derive(Deserialize)]
        struct AnotherApi {
            enabled: bool,
            use_tls: bool,
            host: String,
        }

        let s: Option<AnotherApi> = defs.clone().load_feature("another_api");
        assert!(s.is_some());

        let another_api = s.unwrap();
        assert_eq!(another_api.enabled, true);
        assert_eq!(another_api.use_tls, true);
        assert_eq!(another_api.host, "localhost");
    }

    #[test]
    fn test_load_service_settings() {
        let custom_info = CustomServiceInfo {
            types: Some(vec!["cronjob".to_string()]),
        };

        let filename = assets_path().join("definitions/service.toml.ok_cronjob");
        let defs = Definitions::new(filename.to_str(), Some(custom_info));
        assert!(defs.is_ok());

        let defs = defs.unwrap();

        #[derive(Deserialize)]
        struct Cronjob {
            frequency: String,
            scheduled_times: Vec<String>,
            days: Vec<String>,
        }

        let s: Option<Cronjob> = defs
            .clone()
            .load_service(ServiceKind::Custom("cronjob".to_string()));

        assert!(s.is_some());

        let cronjob = s.unwrap();
        assert_eq!(cronjob.days.len(), 1);
        assert_eq!(cronjob.frequency, "weekly");
        assert_eq!(cronjob.scheduled_times.len(), 2);
    }

    #[test]
    fn test_load_clients() {
        let filename = assets_path().join("definitions/service.toml.ok_clients");
        let defs = Definitions::new(filename.to_str(), None);
        assert!(defs.is_ok());

        let defs = defs.unwrap();
        let user = defs.client("user");
        assert!(user.is_some());
        assert_eq!(user.clone().unwrap().host, "localhost");
        assert_eq!(user.unwrap().port, 7070);

        let auth = defs.client("auth");
        assert!(auth.is_none());

        let address = defs.client("address");
        assert!(address.is_some());
        assert_eq!(address.clone().unwrap().host, "127.0.0.1");
        assert_eq!(address.unwrap().port, 7071);
    }

    #[test]
    fn test_load_service_custom_settings() {
        let filename = assets_path().join("definitions/service.toml.ok_custom_settings");
        let defs = Definitions::new(filename.to_str(), None);
        assert!(defs.is_ok());

        let defs = defs.unwrap();

        #[derive(Deserialize)]
        struct Service {
            direction: String,
            ipc_port: i32,
        }

        let s: Option<Service> = defs.custom_settings();
        assert!(s.is_some());

        let settings = s.unwrap();
        assert_eq!(settings.direction, "forward");
        assert_eq!(settings.ipc_port, 9991);
    }
}