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
//! Instances of libhoney

use std::cell::RefCell;
use std::sync::Arc;
use std::collections::HashMap;

use transmission::Transmission;
use builder_trait::BuilderT;
use builder::Builder;
use event::Event;
use serde_json::value::Value;

pub struct Libhoney {
    builder: RefCell<Builder>,
}

impl Libhoney {
    pub fn new() -> Libhoney {
        Config::default().build()
    }

    fn configured(config: Config) -> Libhoney {
        Libhoney {
            builder: RefCell::new(Builder::new_root(Arc::new(config.transmission))),
        }
    }

    pub fn configure() -> Config {
        Config::default()
    }
}

pub struct Config {
    transmission: Transmission,
    api_host: String,
    write_key: String,
    dataset: String,
}

impl Default for Config {
    fn default() -> Config {
        Config {
            transmission: Transmission::new(),
            api_host: "https://api.honeycomb.io".to_string(),
            write_key: "".to_string(),
            dataset: "".to_string(),
        }
    }
}

impl Config {
    pub fn transmission(self, val: Transmission) -> Config {
        Config {
            transmission: val,
            api_host: self.api_host,
            write_key: self.write_key,
            dataset: self.dataset,
        }
    }

    pub fn api_host<S>(self, val: S) -> Config where S : Into<String> {
        Config {
            transmission: self.transmission,
            api_host: val.into(),
            write_key: self.write_key,
            dataset: self.dataset,
        }
    }

    pub fn write_key<S>(self, val: S) -> Config where S : Into<String> {
        Config {
            transmission: self.transmission,
            api_host: self.api_host,
            write_key: val.into(),
            dataset: self.dataset,
        }
    }

    pub fn dataset<S>(self, val: S) -> Config where S : Into<String> {
        Config {
            transmission: self.transmission,
            api_host: self.api_host,
            write_key: self.write_key,
            dataset: val.into(),
        }
    }

    pub fn build(self) -> Libhoney {
        Libhoney::configured(self)
    }
}
// an implementation of the BuilderT trait that proxies to Libhoney's builder.
impl BuilderT for Libhoney {
    #[inline]
    fn add_field<S>(&mut self, key: S, value: Value) -> &mut Libhoney where S : Into<String> {
        self.builder.borrow_mut().add_field(key, value);
        self
    }
        
    #[inline]
    fn add_dynamic_field<S>(&mut self, key: S, value: fn() -> Value) -> &mut Libhoney where S : Into<String>  {
        self.builder.borrow_mut().add_dynamic_field(key, value);
        self
    }

    #[inline]
    fn add(&mut self, data: HashMap<String, Value>) -> &mut Libhoney {
        self.builder.borrow_mut().add(data);
        self
    }

    #[inline]
    fn new_event(&self/* ... */) -> Event {
        self.builder.borrow_mut().new_event()
    }

    #[inline]
    fn new_builder(&self/* ... */) -> Builder {
        self.builder.borrow_mut().new_builder()
    }

    #[inline]
    fn get_fields(&self) -> HashMap<String, Value> {
        self.builder.borrow_mut().get_fields()
    }

    #[inline]
    fn get_dynamic_fields(&self) -> HashMap<String, fn() -> Value> {
        self.builder.borrow_mut().get_dynamic_fields()
    }
}