From 046fabc55bcfaf953588e6064f248683ec765e5d Mon Sep 17 00:00:00 2001 From: Pranav Bhatt <42911524+pranav-bhatt@users.noreply.github.com> Date: Fri, 5 Jun 2020 20:15:47 +0530 Subject: [PATCH] Expose iter (#55) * exposed iterator via enum Signed-off-by: Pranav Bhatt * exposed iterator Signed-off-by: Pranav Bhatt * exposed iterator(finalise#1) Signed-off-by: Pranav Bhatt * resolving pull request issues #1 Signed-off-by: Pranav Bhatt * resolving pull request issues #2 Signed-off-by: Pranav Bhatt * resolving pull request issues #3 Signed-off-by: Pranav Bhatt * Copy trait issue Signed-off-by: Pranav Bhatt * Attributes Iterator finalise #2 Signed-off-by: Pranav Bhatt * Attributes Iterator finalise #3 Signed-off-by: Pranav Bhatt * Attributes Iterator finalise #4 Signed-off-by: Pranav Bhatt * Attributes Iterator finalise #5 Signed-off-by: Pranav Bhatt --- src/event/attributes.rs | 20 +++++++++++++++++++- src/event/event.rs | 12 ++++++++++-- src/event/mod.rs | 5 ++++- src/event/v03/attributes.rs | 24 +++++++++++++++--------- src/event/v03/mod.rs | 1 + src/event/v10/attributes.rs | 25 ++++++++++++++++--------- src/event/v10/mod.rs | 1 + tests/attributes_iter.rs | 26 ++++++++++++++++++++++++++ 8 files changed, 92 insertions(+), 22 deletions(-) create mode 100644 tests/attributes_iter.rs diff --git a/src/event/attributes.rs b/src/event/attributes.rs index 40d801b..43f55d4 100644 --- a/src/event/attributes.rs +++ b/src/event/attributes.rs @@ -1,4 +1,6 @@ -use super::{AttributesV03, AttributesV10, SpecVersion}; +use super::{ + AttributesIntoIteratorV03, AttributesIntoIteratorV10, AttributesV03, AttributesV10, SpecVersion, +}; use chrono::{DateTime, Utc}; use std::fmt; use url::Url; @@ -68,6 +70,22 @@ pub(crate) trait DataAttributesWriter { fn set_dataschema(&mut self, dataschema: Option>); } +#[derive(PartialEq, Debug, Clone, Copy)] +pub(crate) enum AttributesIter<'a> { + IterV03(AttributesIntoIteratorV03<'a>), + IterV10(AttributesIntoIteratorV10<'a>), +} + +impl<'a> Iterator for AttributesIter<'a> { + type Item = (&'a str, AttributeValue<'a>); + fn next(&mut self) -> Option { + match self { + AttributesIter::IterV03(a) => a.next(), + AttributesIter::IterV10(a) => a.next(), + } + } +} + /// Union type representing one of the possible context attributes structs #[derive(PartialEq, Debug, Clone)] pub enum Attributes { diff --git a/src/event/event.rs b/src/event/event.rs index d7ab118..8fe2dcb 100644 --- a/src/event/event.rs +++ b/src/event/event.rs @@ -1,6 +1,6 @@ use super::{ - Attributes, AttributesReader, AttributesV10, AttributesWriter, Data, ExtensionValue, - SpecVersion, + AttributeValue, Attributes, AttributesIter, AttributesReader, AttributesV10, AttributesWriter, + Data, ExtensionValue, SpecVersion, }; use crate::event::attributes::DataAttributesWriter; use chrono::{DateTime, Utc}; @@ -78,6 +78,14 @@ impl Default for Event { } impl Event { + /// Returns an [`Iterator`] for [`Attributes`] + pub fn attributes_iter<'a>(&'a self) -> impl Iterator)> { + match &self.attributes { + Attributes::V03(a) => AttributesIter::IterV03(a.into_iter()), + Attributes::V10(a) => AttributesIter::IterV10(a.into_iter()), + } + } + /// Remove `data`, `dataschema` and `datacontenttype` from this `Event` pub fn remove_data(&mut self) { self.data = None; diff --git a/src/event/mod.rs b/src/event/mod.rs index 714ed0c..4ec85bf 100644 --- a/src/event/mod.rs +++ b/src/event/mod.rs @@ -10,7 +10,8 @@ mod spec_version; mod types; pub use attributes::Attributes; -pub use attributes::{AttributesReader, AttributesWriter}; +pub(crate) use attributes::AttributesIter; +pub use attributes::{AttributeValue, AttributesReader, AttributesWriter}; pub use builder::Error as EventBuilderError; pub use builder::EventBuilder; pub use data::Data; @@ -23,6 +24,7 @@ pub use types::{TryIntoTime, TryIntoUrl}; mod v03; pub use v03::Attributes as AttributesV03; +pub(crate) use v03::AttributesIntoIterator as AttributesIntoIteratorV03; pub use v03::EventBuilder as EventBuilderV03; pub(crate) use v03::EventFormatDeserializer as EventFormatDeserializerV03; pub(crate) use v03::EventFormatSerializer as EventFormatSerializerV03; @@ -30,6 +32,7 @@ pub(crate) use v03::EventFormatSerializer as EventFormatSerializerV03; mod v10; pub use v10::Attributes as AttributesV10; +pub(crate) use v10::AttributesIntoIterator as AttributesIntoIteratorV10; pub use v10::EventBuilder as EventBuilderV10; pub(crate) use v10::EventFormatDeserializer as EventFormatDeserializerV10; pub(crate) use v10::EventFormatSerializer as EventFormatSerializerV10; diff --git a/src/event/v03/attributes.rs b/src/event/v03/attributes.rs index 256b7af..c0b879b 100644 --- a/src/event/v03/attributes.rs +++ b/src/event/v03/attributes.rs @@ -42,34 +42,36 @@ impl<'a> IntoIterator for &'a Attributes { } } +#[derive(PartialEq, Debug, Clone, Copy)] pub struct AttributesIntoIterator<'a> { - attributes: &'a Attributes, - index: usize, + pub(crate) attributes: &'a Attributes, + pub(crate) index: usize, } impl<'a> Iterator for AttributesIntoIterator<'a> { type Item = (&'a str, AttributeValue<'a>); fn next(&mut self) -> Option { let result = match self.index { - 0 => Some(("id", AttributeValue::String(&self.attributes.id))), - 1 => Some(("type", AttributeValue::String(&self.attributes.ty))), - 2 => Some(("source", AttributeValue::URIRef(&self.attributes.source))), - 3 => self + 0 => Some(("specversion", AttributeValue::SpecVersion(SpecVersion::V03))), + 1 => Some(("id", AttributeValue::String(&self.attributes.id))), + 2 => Some(("type", AttributeValue::String(&self.attributes.ty))), + 3 => Some(("source", AttributeValue::URIRef(&self.attributes.source))), + 4 => self .attributes .datacontenttype .as_ref() .map(|v| ("datacontenttype", AttributeValue::String(v))), - 4 => self + 5 => self .attributes .schemaurl .as_ref() .map(|v| ("schemaurl", AttributeValue::URIRef(v))), - 5 => self + 6 => self .attributes .subject .as_ref() .map(|v| ("subject", AttributeValue::String(v))), - 6 => self + 7 => self .attributes .time .as_ref() @@ -204,6 +206,10 @@ mod tests { let b = &mut a.into_iter(); let time = DateTime::::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc); + assert_eq!( + ("specversion", AttributeValue::SpecVersion(SpecVersion::V03)), + b.next().unwrap() + ); assert_eq!(("id", AttributeValue::String("1")), b.next().unwrap()); assert_eq!( ("type", AttributeValue::String("someType")), diff --git a/src/event/v03/mod.rs b/src/event/v03/mod.rs index 0e791f1..1813134 100644 --- a/src/event/v03/mod.rs +++ b/src/event/v03/mod.rs @@ -4,6 +4,7 @@ mod format; mod message; pub use attributes::Attributes; +pub(crate) use attributes::AttributesIntoIterator; pub(crate) use attributes::ATTRIBUTE_NAMES; pub use builder::EventBuilder; pub(crate) use format::EventFormatDeserializer; diff --git a/src/event/v10/attributes.rs b/src/event/v10/attributes.rs index 47fcfcb..fe9444d 100644 --- a/src/event/v10/attributes.rs +++ b/src/event/v10/attributes.rs @@ -3,6 +3,7 @@ use crate::event::attributes::{ }; use crate::event::{AttributesReader, AttributesV03, AttributesWriter, SpecVersion}; use chrono::{DateTime, Utc}; +use core::fmt::Debug; use url::Url; use uuid::Uuid; @@ -41,34 +42,36 @@ impl<'a> IntoIterator for &'a Attributes { } } +#[derive(PartialEq, Debug, Clone, Copy)] pub struct AttributesIntoIterator<'a> { - attributes: &'a Attributes, - index: usize, + pub(crate) attributes: &'a Attributes, + pub(crate) index: usize, } impl<'a> Iterator for AttributesIntoIterator<'a> { type Item = (&'a str, AttributeValue<'a>); fn next(&mut self) -> Option { let result = match self.index { - 0 => Some(("id", AttributeValue::String(&self.attributes.id))), - 1 => Some(("type", AttributeValue::String(&self.attributes.ty))), - 2 => Some(("source", AttributeValue::URIRef(&self.attributes.source))), - 3 => self + 0 => Some(("specversion", AttributeValue::SpecVersion(SpecVersion::V10))), + 1 => Some(("id", AttributeValue::String(&self.attributes.id))), + 2 => Some(("type", AttributeValue::String(&self.attributes.ty))), + 3 => Some(("source", AttributeValue::URIRef(&self.attributes.source))), + 4 => self .attributes .datacontenttype .as_ref() .map(|v| ("datacontenttype", AttributeValue::String(v))), - 4 => self + 5 => self .attributes .dataschema .as_ref() .map(|v| ("dataschema", AttributeValue::URI(v))), - 5 => self + 6 => self .attributes .subject .as_ref() .map(|v| ("subject", AttributeValue::String(v))), - 6 => self + 7 => self .attributes .time .as_ref() @@ -203,6 +206,10 @@ mod tests { let b = &mut a.into_iter(); let time = DateTime::::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc); + assert_eq!( + ("specversion", AttributeValue::SpecVersion(SpecVersion::V10)), + b.next().unwrap() + ); assert_eq!(("id", AttributeValue::String("1")), b.next().unwrap()); assert_eq!( ("type", AttributeValue::String("someType")), diff --git a/src/event/v10/mod.rs b/src/event/v10/mod.rs index 0e791f1..1813134 100644 --- a/src/event/v10/mod.rs +++ b/src/event/v10/mod.rs @@ -4,6 +4,7 @@ mod format; mod message; pub use attributes::Attributes; +pub(crate) use attributes::AttributesIntoIterator; pub(crate) use attributes::ATTRIBUTE_NAMES; pub use builder::EventBuilder; pub(crate) use format::EventFormatDeserializer; diff --git a/tests/attributes_iter.rs b/tests/attributes_iter.rs new file mode 100644 index 0000000..4722c47 --- /dev/null +++ b/tests/attributes_iter.rs @@ -0,0 +1,26 @@ +mod test_data; +use cloudevents::event::AttributeValue; +use cloudevents::event::SpecVersion; +use test_data::*; + +#[test] +fn iter_v10_test() { + let in_event = v10::full_no_data(); + let mut iter_v10 = in_event.attributes_iter(); + + assert_eq!( + ("specversion", AttributeValue::SpecVersion(SpecVersion::V10)), + iter_v10.next().unwrap() + ); +} + +#[test] +fn iter_v03_test() { + let in_event = v03::full_json_data(); + let mut iter_v03 = in_event.attributes_iter(); + + assert_eq!( + ("specversion", AttributeValue::SpecVersion(SpecVersion::V03)), + iter_v03.next().unwrap() + ); +}