diff --git a/src/event/attributes.rs b/src/event/attributes.rs index da5a323..9521043 100644 --- a/src/event/attributes.rs +++ b/src/event/attributes.rs @@ -1,6 +1,42 @@ use super::SpecVersion; use crate::event::{AttributesV10, ExtensionValue}; use chrono::{DateTime, Utc}; +use std::fmt; + +impl ExactSizeIterator for Iter { + type Item = (&'a str, AttributeValue<'a>); + + fn next(&mut self) -> Option { + let new_next = self.curr + self.next; + + self.curr = self.next; + self.next = new_next; + + // Since there's no endpoint to a Fibonacci sequence, the `Iterator` + // will never return `None`, and `Some` is always returned. + Some(self.curr) + } +} + +pub enum AttributeValue<'a> { + SpecVersion(SpecVersion), + String(&'a str), + URI(&'a str), + URIRef(&'a str), + Time(&'a DateTime) +} + +impl fmt::Display for AttributeValue<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + AttributeValue::SpecVersion(s) => s.fmt(f), + AttributeValue::String(s) => f.write_str(s), + AttributeValue::URI(s) => f.write_str(s), + AttributeValue::URIRef(s) => f.write_str(s), + AttributeValue::Time(s) => f.write_str(&s.to_rfc2822()), + } + } +} /// Trait to get [CloudEvents Context attributes](https://github.com/cloudevents/spec/blob/master/spec.md#context-attributes). pub trait AttributesReader { @@ -23,7 +59,7 @@ pub trait AttributesReader { /// Get the [extension](https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes) named `extension_name` fn get_extension(&self, extension_name: &str) -> Option<&ExtensionValue>; /// Get all the [extensions](https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes) - fn get_extensions(&self) -> Vec<(&str, &ExtensionValue)>; + fn iter_extensions(&self) -> std::collections::hash_map::Iter; } pub trait AttributesWriter { @@ -108,9 +144,9 @@ impl AttributesReader for Attributes { } } - fn get_extensions(&self) -> Vec<(&str, &ExtensionValue)> { + fn iter_extensions(&self) -> std::collections::hash_map::Iter { match self { - Attributes::V10(a) => a.get_extensions(), + Attributes::V10(a) => a.iter_extensions(), } } } diff --git a/src/event/event.rs b/src/event/event.rs index 6b1108d..679f78a 100644 --- a/src/event/event.rs +++ b/src/event/event.rs @@ -49,7 +49,7 @@ impl AttributesReader for Event { fn get_subject(&self) -> Option<&str>; fn get_time(&self) -> Option<&DateTime>; fn get_extension(&self, extension_name: &str) -> Option<&ExtensionValue>; - fn get_extensions(&self) -> Vec<(&str, &ExtensionValue)>; + fn iter_extensions(&self) -> std::collections::hash_map::Iter; } } } diff --git a/src/event/extensions.rs b/src/event/extension_value.rs similarity index 100% rename from src/event/extensions.rs rename to src/event/extension_value.rs diff --git a/src/event/mod.rs b/src/event/mod.rs index 888b50c..46be8f7 100644 --- a/src/event/mod.rs +++ b/src/event/mod.rs @@ -2,7 +2,7 @@ mod attributes; mod builder; mod data; mod event; -mod extensions; +mod extension_value; mod spec_version; pub use attributes::Attributes; @@ -10,7 +10,7 @@ pub use attributes::{AttributesReader, AttributesWriter}; pub use builder::EventBuilder; pub use data::Data; pub use event::Event; -pub use extensions::ExtensionValue; +pub use extension_value::ExtensionValue; pub use spec_version::SpecVersion; mod v10; diff --git a/src/event/v10/attributes.rs b/src/event/v10/attributes.rs index e035cbb..3dea738 100644 --- a/src/event/v10/attributes.rs +++ b/src/event/v10/attributes.rs @@ -63,11 +63,8 @@ impl AttributesReader for Attributes { self.extensions.get(extension_name) } - fn get_extensions(&self) -> Vec<(&str, &ExtensionValue)> { - self.extensions - .iter() - .map(|(k, v)| (k.as_str(), v)) - .collect() + fn iter_extensions(&self) -> std::collections::hash_map::Iter { + self.extensions.iter() } }