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
use chrono::{DateTime,UTC};
use std::collections;
use std::collections::btree_map::Entry;
use LogLevel;
use templates;

#[derive(Clone, PartialEq)]
pub enum Value {
    /// Represents null
    Null,

    /// Represents a Boolean
    Bool(bool),

    /// Represents a signed integer
    I64(i64),

    /// Represents an unsigned integer
    U64(u64),

    /// Represents a floating point number
    F64(f64),

    /// Represents a string
    String(String),

    /// Represents a collection
    Vec(Vec<Value>)
}

/// Represents a type that can be converted into a `Value`.
pub trait IntoValue {
    fn into_value(self) -> Value;
}

#[derive(Debug, Clone)]
pub struct Event<'a> {
    timestamp: DateTime<UTC>,
    level: LogLevel,
    message_template: templates::MessageTemplate,
    properties: collections::BTreeMap<&'a str, Value>
}

impl<'a> Event<'a> {
    pub fn new(timestamp: DateTime<UTC>, level: LogLevel, message_template: templates::MessageTemplate, properties: collections::BTreeMap<&'a str, Value>) -> Event<'a> {
        Event {
            timestamp: timestamp,
            level: level,
            message_template: message_template,
            properties: properties
        }
    }
    
    pub fn new_now(level: LogLevel, message_template: templates::MessageTemplate, properties: collections::BTreeMap<&'a str, Value>) -> Event<'a> {
        Self::new(UTC::now(), level, message_template, properties)
    }
    
    pub fn timestamp(&self) -> DateTime<UTC> {
        self.timestamp
    }
    
    pub fn level(&self) -> LogLevel {
        self.level
    }
    
    pub fn message_template(&self) -> &templates::MessageTemplate {
        &self.message_template
    }

    pub fn message(&self) -> String {
        let repl = self.message_template.parse();
        repl.replace(self.properties())
    }
    
    pub fn properties(&self) -> &collections::BTreeMap<&'a str, Value> {
        &self.properties
    }
    
    pub fn add_or_update_property<I: IntoValue>(&mut self, name: &'a str, value: I) {
        match self.properties.entry(name) {
            Entry::Vacant(v) => {v.insert(value.into_value());},
            Entry::Occupied(mut o) => {o.insert(value.into_value());}
        }
    }
    
    pub fn add_property_if_absent<I: IntoValue>(&mut self, name: &'a str, value: I) {
        if !self.properties.contains_key(name) {
            self.properties.insert(name, value.into_value());
        }
    }
}