micro_kit/
lib.rs

1//! #Micro-Kit
2//!
3//! The Micro-Kit module is meant to be a curated collection of crates (Re-Exported) and helper
4//! functionality to build `RESTful` micro-services with standardized logging, healthchecking,
5//! metrics, and configuration
6//!
7//! The motivation for the module is from Dropwizard, a collection of java libs that help quickly
8//! build standardized `RESTful` applications. This kit is nowhere close to as useful (yet) but we
9//! can aspire!
10
11extern crate log4rs;
12extern crate metrics as metrics_lib;
13
14pub extern crate yaml_rust as yaml;
15pub extern crate iron;
16pub extern crate router;
17
18pub extern crate serde;
19pub extern crate serde_bytes as bytes;
20pub extern crate serde_json as json;
21
22pub extern crate chrono;
23
24/// Configuration based on YAML files for apps.
25pub mod config;
26
27/// Healthchecks for apps.
28pub mod healthcheck;
29
30/// `RESTful` API helpers.
31pub mod http;
32
33/// Logging configuration for apps.
34pub mod logging;
35
36/// A framework for adding metrics to an app.
37pub mod metrics;
38
39use std::ops::Deref;
40use chrono::prelude::{TimeZone, Utc, DateTime};
41use serde::{Deserialize, Serialize, Deserializer, Serializer};
42
43/// A `TimeStamp` newtype around the i64 so we can implement
44/// serialization to and from Epoch timestamps in a type safe manner.
45#[derive(Debug, PartialOrd, PartialEq, Clone, Copy, Hash)]
46pub struct TimeStamp(i64);
47
48impl TimeStamp {
49
50    /// Create a new `TimeStamp`. Here we use an i64 because several modern time packages have moved
51    /// to an i64 representation of epoch.
52    pub fn new(ts: i64) -> TimeStamp {
53        TimeStamp(ts)
54    }
55}
56
57impl Deref for TimeStamp {
58    type Target = i64;
59
60    fn deref(&self) -> &i64 {
61        let &TimeStamp(ref value) = self;
62        value
63    }
64}
65
66impl Serialize for TimeStamp {
67    fn serialize<S>(&self, serializer: S) -> std::result::Result<<S as Serializer>::Ok, <S as Serializer>::Error>
68        where S: Serializer
69    {
70        serializer.serialize_i64(**self)
71    }
72}
73
74impl From<DateTime<Utc>> for TimeStamp {
75    fn from(dt: DateTime<Utc>) -> Self {
76        TimeStamp(dt.timestamp())
77    }
78}
79
80impl Into<DateTime<Utc>> for TimeStamp {
81    fn into(self) -> DateTime<Utc> {
82        Utc.timestamp(*self, 0)
83    }
84}
85
86impl<'de> Deserialize<'de> for TimeStamp {
87    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
88        where D: Deserializer<'de>
89    {
90        i64::deserialize(deserializer).map(TimeStamp::new)
91    }
92}