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
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::io;


/// `SpanContext` extraction format and source.
///
/// Each supported extraction format also carries an object trait to
/// the data carrier the `SpanContext` should be extracted from.
///
/// # Examples
///
/// ```
/// extern crate opentracingrust;
///
/// use std::collections::HashMap;
/// use opentracingrust::ExtractFormat;
///
///
/// fn main() {
///     let mut headers: HashMap<String, String> = HashMap::new();
///     headers.insert(String::from("TraceId"), String::from("123"));
///     headers.insert(String::from("SpanId"), String::from("456"));
///
///     let format = ExtractFormat::HttpHeaders(Box::new(&headers)); 
///     // ... snip ...
/// }
/// ```
pub enum ExtractFormat<'a> {
    Binary(Box<&'a mut dyn self::io::Read>),
    HttpHeaders(Box<&'a dyn MapCarrier>),
    TextMap(Box<&'a dyn MapCarrier>)
}


/// `SpanContext` injection format and destination.
///
/// Each supported injection format also carries an object trait to
/// the data carrier the `SpanContext` should be injected into.
///
/// # Examples
///
/// ```
/// extern crate opentracingrust;
///
/// use std::collections::HashMap;
/// use opentracingrust::InjectFormat;
///
///
/// fn main() {
///     let mut headers: HashMap<String, String> = HashMap::new();
///     let format = InjectFormat::HttpHeaders(Box::new(&mut headers)); 
///     // ... snip ...
/// }
/// ```
pub enum InjectFormat<'a> {
    Binary(Box<&'a mut dyn self::io::Write>),
    HttpHeaders(Box<&'a mut dyn MapCarrier>),
    TextMap(Box<&'a mut dyn MapCarrier>)
}


/// Interface for HTTP header and text map carriers.
///
/// A trait used by `InjectFormat` and `ExtractFormat` to store carriers that
/// support the `HttpHeaders` and the `TextMap` formats.
pub trait MapCarrier {
    /// List all items stored in the carrier as `(key, value)` pairs.
    ///
    /// Intended to be used by the `ExtractFormat`s to extract all the
    /// baggage items from the carrier.
    fn items(&self) -> Vec<(&String, &String)>;

    /// Attempt to fetch an exact key from the carrier.
    fn get(&self, key: &str) -> Option<String>;

    /// Set a key/value pair on the carrier.
    fn set(&mut self, key: &str, value: &str);
}

impl MapCarrier for HashMap<String, String> {
    fn items(&self) -> Vec<(&String, &String)> {
        self.iter().collect()
    }

    fn get(&self, key: &str) -> Option<String> {
        self.get(key).cloned()
    }

    fn set(&mut self, key: &str, value: &str) {
        self.insert(String::from(key), String::from(value));
    }
}

impl MapCarrier for BTreeMap<String, String> {
    fn items(&self) -> Vec<(&String, &String)> {
        self.iter().collect()
    }

    fn get(&self, key: &str) -> Option<String> {
        self.get(key).cloned()
    }

    fn set(&mut self, key: &str, value: &str) {
        self.insert(String::from(key), String::from(value));
    }
}


#[cfg(test)]
mod tests {
    mod tree_map {
        use std::collections::BTreeMap;
        use super::super::MapCarrier;

        #[test]
        fn extract_keys() {
            let mut tree: BTreeMap<String, String> = BTreeMap::new();
            tree.insert(String::from("aa"), String::from("d"));
            assert_eq!(tree.get("aa").unwrap(), "d");
        }

        #[test]
        fn find_keys() {
            let mut tree: BTreeMap<String, String> = BTreeMap::new();
            tree.insert(String::from("aa"), String::from("d"));
            tree.insert(String::from("ab"), String::from("e"));
            tree.insert(String::from("bc"), String::from("f"));

            let mut items = vec![];
            for (key, value) in tree.items() {
                if key.starts_with("a") {
                    items.push((key.clone(), value.clone()));
                }
            }
            items.sort();
            assert_eq!(items, [
                (String::from("aa"), String::from("d")),
                (String::from("ab"), String::from("e"))
            ]);
        }

        #[test]
        fn inject_keys() {
            let mut tree: BTreeMap<String, String> = BTreeMap::new();
            tree.set("a", "d");
            tree.set("b", "e");
            tree.set("c", "f");
            assert_eq!("d", tree.get("a").unwrap());
            assert_eq!("e", tree.get("b").unwrap());
            assert_eq!("f", tree.get("c").unwrap());
        }
    }

    mod hash_map {
        use std::collections::HashMap;
        use super::super::MapCarrier;

        #[test]
        fn extract_keys() {
            let mut map: HashMap<String, String> = HashMap::new();
            map.insert(String::from("aa"), String::from("d"));
            assert_eq!(map.get("aa").unwrap(), "d");
        }

        #[test]
        fn find_keys() {
            let mut map: HashMap<String, String> = HashMap::new();
            map.insert(String::from("aa"), String::from("d"));
            map.insert(String::from("ab"), String::from("e"));
            map.insert(String::from("bc"), String::from("f"));

            let mut items = vec![];
            for (key, value) in map.items() {
                if key.starts_with("a") {
                    items.push((key.clone(), value.clone()));
                }
            }
            items.sort();
            assert_eq!(items, [
                (String::from("aa"), String::from("d")),
                (String::from("ab"), String::from("e"))
            ]);
        }

        #[test]
        fn inject_keys() {
            let mut map: HashMap<String, String> = HashMap::new();
            map.set("a", "d");
            map.set("b", "e");
            map.set("c", "f");
            assert_eq!("d", map.get("a").unwrap());
            assert_eq!("e", map.get("b").unwrap());
            assert_eq!("f", map.get("c").unwrap());
        }
    }
}