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
extern crate chrono;
extern crate serde;
extern crate serde_json;
extern crate uuid;

use self::serde::de::DeserializeOwned;
use self::serde::ser::Serialize;
use self::uuid::Uuid;
use std::error;
use std::fmt;
use std::io;
use date_time_tz::DateTimeTz;


/// Errors for the database
#[derive(Debug)]
pub enum Error {
    /// Indicates that the UUID specified is invalid and cannot be parsed
    UUIDParseError(uuid::ParseError),

    /// Indicates an error in the JSON serialization
    JSONStringError(serde_json::error::Error),

    /// Indicates an error in the JSON deserialization
    JSONParseError(serde_json::error::Error),

    /// Indicates a general IO error
    IOError(io::Error),
}


impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::UUIDParseError(err) => write!(f, "UUID failed to parse: {}", err),
            Error::JSONStringError(err) => write!(f, "Error generating a JSON string: {}", err),
            Error::JSONParseError(err) => write!(f, "Error parsing JSON: {}", err),
            Error::IOError(err) => write!(f, "IO Error: {}", err),
        }
    }
}


impl error::Error for Error {
    fn description(&self) -> &str {
        match self {
            Error::UUIDParseError(ref err) => err.description(),
            Error::JSONStringError(ref err) => err.description(),
            Error::JSONParseError(ref err) => err.description(),
            Error::IOError(ref err) => err.description(),
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match self {
            Error::UUIDParseError(ref err) => Some(err),
            Error::JSONStringError(ref err) => Some(err),
            Error::JSONParseError(ref err) => Some(err),
            Error::IOError(ref err) => Some(err),
        }
    }
}


/// Any element to be put into the database needs to be Recordable. This is the common API that
/// will aid in searching and later in indexing records.
pub trait Recordable {
    /// The timestamp for the record.
    fn timestamp(&self) -> DateTimeTz;

    /// A list of string tags that can be used for indexing. This list defined per-type.
    fn tags(&self) -> Vec<String>;
}


/// Uniquely identifies a record.
///
/// This is a wrapper around a basic uuid with some extra convenience methods.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
pub struct UniqueId(Uuid);

impl UniqueId {
    /// Create a new V4 UUID (this is the most common type in use these days).
    pub fn new() -> UniqueId {
        let id = Uuid::new_v4();
        UniqueId(id)
    }

    /// Parse a UUID from a string. Raise UUIDParseError if the parsing fails.
    pub fn from_str(val: &str) -> Result<UniqueId, Error> {
        Uuid::parse_str(val).map(UniqueId).map_err(|err| {
            Error::UUIDParseError(err)
        })
    }

    /// Convert to a hyphenated string
    pub fn to_string(&self) -> String {
        self.0.hyphenated().to_string()
    }
}

/// Every record contains a unique ID and then the primary data, which itself must implementd the
/// Recordable trait.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Record<T: Clone + Recordable> {
    pub id: UniqueId,
    pub data: T,
}

impl<T> Record<T>
where
    T: Clone + Recordable,
{
    pub fn new(data: T) -> Record<T> {
        let id = UniqueId::new();
        Record { id, data }
    }
}

impl<T> Recordable for Record<T>
where
    T: Clone + Recordable,
{
    fn timestamp(&self) -> DateTimeTz {
        self.data.timestamp()
    }
    fn tags(&self) -> Vec<String> {
        self.data.tags()
    }
}


#[derive(Clone, Deserialize, Serialize)]
pub struct DeletableRecord<T: Clone + Recordable> {
    pub id: UniqueId,
    pub data: Option<T>,
}

pub fn parse_line<T>(line: &str) -> Result<DeletableRecord<T>, Error>
where
    T: Clone + Recordable + DeserializeOwned + Serialize,
{
    serde_json::from_str(&line).map_err(|err| {
        println!("deserialization error: {}", err);
        Error::JSONParseError(err)
    })
}


#[cfg(test)]
mod test {
    extern crate dimensioned;
    extern crate serde_json;

    use self::dimensioned::si::{Kilogram, KG};
    use super::{DeletableRecord, Recordable, UniqueId, parse_line};
    use date_time_tz::DateTimeTz;
    use chrono::TimeZone;
    use chrono_tz::Etc::UTC;
    use chrono_tz::US::Central;

    #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
    pub struct Weight(Kilogram<f64>);

    #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
    pub struct WeightRecord {
        pub date: DateTimeTz,
        pub weight: Weight,
    }

    impl Recordable for WeightRecord {
        fn timestamp(&self) -> DateTimeTz {
            self.date.clone()
        }

        fn tags(&self) -> Vec<String> {
            Vec::new()
        }
    }

    const WEIGHT_ENTRY: &str = "{\"data\":{\"weight\":77.79109,\"date\":\"2003-11-10T06:00:00.000000000000Z\"},\"id\":\"3330c5b0-783f-4919-b2c4-8169c38f65ff\"}";

    #[test]
    pub fn legacy_deserialization() {
        let rec: DeletableRecord<WeightRecord> =
            parse_line(WEIGHT_ENTRY).expect("should successfully parse the record");
        assert_eq!(
            rec.id,
            UniqueId::from_str("3330c5b0-783f-4919-b2c4-8169c38f65ff").unwrap()
        );
        assert_eq!(
            rec.data,
            Some(WeightRecord {
                date: DateTimeTz(UTC.ymd(2003, 11, 10).and_hms(6, 0, 0)),
                weight: Weight(77.79109 * KG),
            })
        );
    }

    #[test]
    pub fn serialization_output() {
        let rec = WeightRecord {
            date: DateTimeTz(UTC.ymd(2003, 11, 10).and_hms(6, 0, 0)),
            weight: Weight(77.0 * KG),
        };
        assert_eq!(
            serde_json::to_string(&rec).unwrap(),
            "{\"date\":\"2003-11-10T06:00:00Z\",\"weight\":77.0}"
        );

        let rec2 = WeightRecord {
            date: DateTimeTz(Central.ymd(2003, 11, 10).and_hms(0, 0, 0)),
            weight: Weight(77.0 * KG),
        };
        assert_eq!(
            serde_json::to_string(&rec2).unwrap(),
            "{\"date\":\"2003-11-10T06:00:00Z US/Central\",\"weight\":77.0}"
        );
    }
}