diff --git a/opentracing-api/src/context.rs b/opentracing-api/src/context.rs index 5220a77..290df4e 100644 --- a/opentracing-api/src/context.rs +++ b/opentracing-api/src/context.rs @@ -38,7 +38,7 @@ mod tests { fn get_items() { let mut items = HashMap::new(); items.insert("key".into(), "value".into()); - let context = TestContext { items: items }; + let context = TestContext { items }; let items: Vec<(&String, &String)> = context.baggage_items().collect(); assert_eq!(items, [(&"key".into(), &"value".into())]) } diff --git a/opentracing-mock/Cargo.toml b/opentracing-mock/Cargo.toml index 1761dad..fcab0c1 100644 --- a/opentracing-mock/Cargo.toml +++ b/opentracing-mock/Cargo.toml @@ -10,4 +10,7 @@ documentation = "https://docs.rs/opentracing-mock" keywords = ["opentracing", "tracing"] [badges] -maintenance = { status = "actively-developed" } \ No newline at end of file +maintenance = { status = "actively-developed" } + +[dependencies] +opentracing-api = { path = "../opentracing-api" } \ No newline at end of file diff --git a/opentracing-mock/src/lib.rs b/opentracing-mock/src/lib.rs index f4f7659..d912641 100644 --- a/opentracing-mock/src/lib.rs +++ b/opentracing-mock/src/lib.rs @@ -1 +1,51 @@ +//! This crate provides a mocking implementation of the tracing +//! API, especially useful for testing. #![doc(html_root_url = "https://docs.rs/opentracing-mock/0.1.0")] + +extern crate opentracing_api; + +use opentracing_api::SpanContext; +use std::collections::HashMap; +use std::collections::hash_map::Iter as HashMapIter; + +pub struct MockSpanContext { + baggage: HashMap, +} + +impl MockSpanContext { + /// Create a new `MockSpanContext` with the given baggage. + pub fn new(baggage: HashMap) -> Self { + MockSpanContext { baggage } + } +} + +impl<'a> SpanContext<'a> for MockSpanContext { + type Iter = HashMapIter<'a, String, String>; + + fn baggage_items(&'a self) -> Self::Iter { + self.baggage.iter() + } +} + +#[cfg(test)] +mod tests { + + use super::MockSpanContext; + use opentracing_api::SpanContext; + use std::collections::HashMap; + + #[test] + fn test_map_baggage() { + let mut items = HashMap::new(); + items.insert("key".into(), "value".into()); + + let ctx = MockSpanContext::new(items); + let mut iter = ctx.baggage_items(); + assert_eq!( + Some((&String::from("key"), &String::from("value"))), + iter.next() + ); + assert_eq!(None, iter.next()); + } + +} diff --git a/opentracing-noop/Cargo.toml b/opentracing-noop/Cargo.toml index b4d3f46..e50bbf3 100644 --- a/opentracing-noop/Cargo.toml +++ b/opentracing-noop/Cargo.toml @@ -10,4 +10,7 @@ documentation = "https://docs.rs/opentracing-noop" keywords = ["opentracing", "tracing"] [badges] -maintenance = { status = "actively-developed" } \ No newline at end of file +maintenance = { status = "actively-developed" } + +[dependencies] +opentracing-api = { path = "../opentracing-api" } \ No newline at end of file diff --git a/opentracing-noop/src/lib.rs b/opentracing-noop/src/lib.rs index d3ee850..abf365c 100644 --- a/opentracing-noop/src/lib.rs +++ b/opentracing-noop/src/lib.rs @@ -1 +1,40 @@ +//! This crate provides a simple and cheap "noop" implementation of the +//! tracing API and all of its sub-components. #![doc(html_root_url = "https://docs.rs/opentracing-noop/0.1.0")] + +extern crate opentracing_api; + +use opentracing_api::SpanContext; +use std::iter::{empty, Empty}; + +/// The `NoopSpanContext` just returns an empty iterator on +/// baggage items and all of its associated content. +pub struct NoopSpanContext {} + +impl Default for NoopSpanContext { + fn default() -> Self { + NoopSpanContext {} + } +} + +impl<'a> SpanContext<'a> for NoopSpanContext { + type Iter = Empty<(&'a String, &'a String)>; + + fn baggage_items(&'a self) -> Self::Iter { + empty() + } +} + +#[cfg(test)] +mod tests { + + use super::NoopSpanContext; + use opentracing_api::SpanContext; + + #[test] + fn test_empty_baggage() { + let ctx = NoopSpanContext::default(); + assert_eq!(None, ctx.baggage_items().next()); + } + +}