Skip to content

Commit 046fabc

Browse files
authored
Expose iter (cloudevents#55)
* exposed iterator via enum Signed-off-by: Pranav Bhatt <[email protected]> * exposed iterator Signed-off-by: Pranav Bhatt <[email protected]> * exposed iterator(finalise#1) Signed-off-by: Pranav Bhatt <[email protected]> * resolving pull request issues cloudevents#1 Signed-off-by: Pranav Bhatt <[email protected]> * resolving pull request issues cloudevents#2 Signed-off-by: Pranav Bhatt <[email protected]> * resolving pull request issues cloudevents#3 Signed-off-by: Pranav Bhatt <[email protected]> * Copy trait issue Signed-off-by: Pranav Bhatt <[email protected]> * Attributes Iterator finalise cloudevents#2 Signed-off-by: Pranav Bhatt <[email protected]> * Attributes Iterator finalise cloudevents#3 Signed-off-by: Pranav Bhatt <[email protected]> * Attributes Iterator finalise cloudevents#4 Signed-off-by: Pranav Bhatt <[email protected]> * Attributes Iterator finalise cloudevents#5 Signed-off-by: Pranav Bhatt <[email protected]>
1 parent f294cec commit 046fabc

File tree

8 files changed

+92
-22
lines changed

8 files changed

+92
-22
lines changed

src/event/attributes.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use super::{AttributesV03, AttributesV10, SpecVersion};
1+
use super::{
2+
AttributesIntoIteratorV03, AttributesIntoIteratorV10, AttributesV03, AttributesV10, SpecVersion,
3+
};
24
use chrono::{DateTime, Utc};
35
use std::fmt;
46
use url::Url;
@@ -68,6 +70,22 @@ pub(crate) trait DataAttributesWriter {
6870
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>);
6971
}
7072

73+
#[derive(PartialEq, Debug, Clone, Copy)]
74+
pub(crate) enum AttributesIter<'a> {
75+
IterV03(AttributesIntoIteratorV03<'a>),
76+
IterV10(AttributesIntoIteratorV10<'a>),
77+
}
78+
79+
impl<'a> Iterator for AttributesIter<'a> {
80+
type Item = (&'a str, AttributeValue<'a>);
81+
fn next(&mut self) -> Option<Self::Item> {
82+
match self {
83+
AttributesIter::IterV03(a) => a.next(),
84+
AttributesIter::IterV10(a) => a.next(),
85+
}
86+
}
87+
}
88+
7189
/// Union type representing one of the possible context attributes structs
7290
#[derive(PartialEq, Debug, Clone)]
7391
pub enum Attributes {

src/event/event.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{
2-
Attributes, AttributesReader, AttributesV10, AttributesWriter, Data, ExtensionValue,
3-
SpecVersion,
2+
AttributeValue, Attributes, AttributesIter, AttributesReader, AttributesV10, AttributesWriter,
3+
Data, ExtensionValue, SpecVersion,
44
};
55
use crate::event::attributes::DataAttributesWriter;
66
use chrono::{DateTime, Utc};
@@ -78,6 +78,14 @@ impl Default for Event {
7878
}
7979

8080
impl Event {
81+
/// Returns an [`Iterator`] for [`Attributes`]
82+
pub fn attributes_iter<'a>(&'a self) -> impl Iterator<Item = (&'a str, AttributeValue<'a>)> {
83+
match &self.attributes {
84+
Attributes::V03(a) => AttributesIter::IterV03(a.into_iter()),
85+
Attributes::V10(a) => AttributesIter::IterV10(a.into_iter()),
86+
}
87+
}
88+
8189
/// Remove `data`, `dataschema` and `datacontenttype` from this `Event`
8290
pub fn remove_data(&mut self) {
8391
self.data = None;

src/event/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ mod spec_version;
1010
mod types;
1111

1212
pub use attributes::Attributes;
13-
pub use attributes::{AttributesReader, AttributesWriter};
13+
pub(crate) use attributes::AttributesIter;
14+
pub use attributes::{AttributeValue, AttributesReader, AttributesWriter};
1415
pub use builder::Error as EventBuilderError;
1516
pub use builder::EventBuilder;
1617
pub use data::Data;
@@ -23,13 +24,15 @@ pub use types::{TryIntoTime, TryIntoUrl};
2324
mod v03;
2425

2526
pub use v03::Attributes as AttributesV03;
27+
pub(crate) use v03::AttributesIntoIterator as AttributesIntoIteratorV03;
2628
pub use v03::EventBuilder as EventBuilderV03;
2729
pub(crate) use v03::EventFormatDeserializer as EventFormatDeserializerV03;
2830
pub(crate) use v03::EventFormatSerializer as EventFormatSerializerV03;
2931

3032
mod v10;
3133

3234
pub use v10::Attributes as AttributesV10;
35+
pub(crate) use v10::AttributesIntoIterator as AttributesIntoIteratorV10;
3336
pub use v10::EventBuilder as EventBuilderV10;
3437
pub(crate) use v10::EventFormatDeserializer as EventFormatDeserializerV10;
3538
pub(crate) use v10::EventFormatSerializer as EventFormatSerializerV10;

src/event/v03/attributes.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,36 @@ impl<'a> IntoIterator for &'a Attributes {
4242
}
4343
}
4444

45+
#[derive(PartialEq, Debug, Clone, Copy)]
4546
pub struct AttributesIntoIterator<'a> {
46-
attributes: &'a Attributes,
47-
index: usize,
47+
pub(crate) attributes: &'a Attributes,
48+
pub(crate) index: usize,
4849
}
4950

5051
impl<'a> Iterator for AttributesIntoIterator<'a> {
5152
type Item = (&'a str, AttributeValue<'a>);
5253
fn next(&mut self) -> Option<Self::Item> {
5354
let result = match self.index {
54-
0 => Some(("id", AttributeValue::String(&self.attributes.id))),
55-
1 => Some(("type", AttributeValue::String(&self.attributes.ty))),
56-
2 => Some(("source", AttributeValue::URIRef(&self.attributes.source))),
57-
3 => self
55+
0 => Some(("specversion", AttributeValue::SpecVersion(SpecVersion::V03))),
56+
1 => Some(("id", AttributeValue::String(&self.attributes.id))),
57+
2 => Some(("type", AttributeValue::String(&self.attributes.ty))),
58+
3 => Some(("source", AttributeValue::URIRef(&self.attributes.source))),
59+
4 => self
5860
.attributes
5961
.datacontenttype
6062
.as_ref()
6163
.map(|v| ("datacontenttype", AttributeValue::String(v))),
62-
4 => self
64+
5 => self
6365
.attributes
6466
.schemaurl
6567
.as_ref()
6668
.map(|v| ("schemaurl", AttributeValue::URIRef(v))),
67-
5 => self
69+
6 => self
6870
.attributes
6971
.subject
7072
.as_ref()
7173
.map(|v| ("subject", AttributeValue::String(v))),
72-
6 => self
74+
7 => self
7375
.attributes
7476
.time
7577
.as_ref()
@@ -204,6 +206,10 @@ mod tests {
204206
let b = &mut a.into_iter();
205207
let time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);
206208

209+
assert_eq!(
210+
("specversion", AttributeValue::SpecVersion(SpecVersion::V03)),
211+
b.next().unwrap()
212+
);
207213
assert_eq!(("id", AttributeValue::String("1")), b.next().unwrap());
208214
assert_eq!(
209215
("type", AttributeValue::String("someType")),

src/event/v03/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod format;
44
mod message;
55

66
pub use attributes::Attributes;
7+
pub(crate) use attributes::AttributesIntoIterator;
78
pub(crate) use attributes::ATTRIBUTE_NAMES;
89
pub use builder::EventBuilder;
910
pub(crate) use format::EventFormatDeserializer;

src/event/v10/attributes.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::event::attributes::{
33
};
44
use crate::event::{AttributesReader, AttributesV03, AttributesWriter, SpecVersion};
55
use chrono::{DateTime, Utc};
6+
use core::fmt::Debug;
67
use url::Url;
78
use uuid::Uuid;
89

@@ -41,34 +42,36 @@ impl<'a> IntoIterator for &'a Attributes {
4142
}
4243
}
4344

45+
#[derive(PartialEq, Debug, Clone, Copy)]
4446
pub struct AttributesIntoIterator<'a> {
45-
attributes: &'a Attributes,
46-
index: usize,
47+
pub(crate) attributes: &'a Attributes,
48+
pub(crate) index: usize,
4749
}
4850

4951
impl<'a> Iterator for AttributesIntoIterator<'a> {
5052
type Item = (&'a str, AttributeValue<'a>);
5153
fn next(&mut self) -> Option<Self::Item> {
5254
let result = match self.index {
53-
0 => Some(("id", AttributeValue::String(&self.attributes.id))),
54-
1 => Some(("type", AttributeValue::String(&self.attributes.ty))),
55-
2 => Some(("source", AttributeValue::URIRef(&self.attributes.source))),
56-
3 => self
55+
0 => Some(("specversion", AttributeValue::SpecVersion(SpecVersion::V10))),
56+
1 => Some(("id", AttributeValue::String(&self.attributes.id))),
57+
2 => Some(("type", AttributeValue::String(&self.attributes.ty))),
58+
3 => Some(("source", AttributeValue::URIRef(&self.attributes.source))),
59+
4 => self
5760
.attributes
5861
.datacontenttype
5962
.as_ref()
6063
.map(|v| ("datacontenttype", AttributeValue::String(v))),
61-
4 => self
64+
5 => self
6265
.attributes
6366
.dataschema
6467
.as_ref()
6568
.map(|v| ("dataschema", AttributeValue::URI(v))),
66-
5 => self
69+
6 => self
6770
.attributes
6871
.subject
6972
.as_ref()
7073
.map(|v| ("subject", AttributeValue::String(v))),
71-
6 => self
74+
7 => self
7275
.attributes
7376
.time
7477
.as_ref()
@@ -203,6 +206,10 @@ mod tests {
203206
let b = &mut a.into_iter();
204207
let time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);
205208

209+
assert_eq!(
210+
("specversion", AttributeValue::SpecVersion(SpecVersion::V10)),
211+
b.next().unwrap()
212+
);
206213
assert_eq!(("id", AttributeValue::String("1")), b.next().unwrap());
207214
assert_eq!(
208215
("type", AttributeValue::String("someType")),

src/event/v10/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod format;
44
mod message;
55

66
pub use attributes::Attributes;
7+
pub(crate) use attributes::AttributesIntoIterator;
78
pub(crate) use attributes::ATTRIBUTE_NAMES;
89
pub use builder::EventBuilder;
910
pub(crate) use format::EventFormatDeserializer;

tests/attributes_iter.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
mod test_data;
2+
use cloudevents::event::AttributeValue;
3+
use cloudevents::event::SpecVersion;
4+
use test_data::*;
5+
6+
#[test]
7+
fn iter_v10_test() {
8+
let in_event = v10::full_no_data();
9+
let mut iter_v10 = in_event.attributes_iter();
10+
11+
assert_eq!(
12+
("specversion", AttributeValue::SpecVersion(SpecVersion::V10)),
13+
iter_v10.next().unwrap()
14+
);
15+
}
16+
17+
#[test]
18+
fn iter_v03_test() {
19+
let in_event = v03::full_json_data();
20+
let mut iter_v03 = in_event.attributes_iter();
21+
22+
assert_eq!(
23+
("specversion", AttributeValue::SpecVersion(SpecVersion::V03)),
24+
iter_v03.next().unwrap()
25+
);
26+
}

0 commit comments

Comments
 (0)