Skip to content

Commit 534cf01

Browse files
Changed again interface now to work similar to a builder (receives self -> return self)
Signed-off-by: Francesco Guardiani <[email protected]>
1 parent 2dfdd78 commit 534cf01

File tree

9 files changed

+145
-133
lines changed

9 files changed

+145
-133
lines changed

src/event/message.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use super::Event;
33
use super::{Attributes, AttributesReader};
44
use crate::event::SpecVersion;
55
use crate::message::{
6-
BinaryDeserializer, BinarySerializer, DeserializationResult, Error, MessageAttributeValue,
7-
SerializationResult, StructuredDeserializer, StructuredSerializer,
6+
BinaryDeserializer, BinarySerializer, Result, MessageAttributeValue,
7+
StructuredDeserializer, StructuredSerializer,
88
};
99

1010
impl StructuredDeserializer for Event {
11-
fn deserialize_structured<R, V: StructuredSerializer<R>>(self, visitor: V) -> Result<R, Error> {
11+
fn deserialize_structured<R, V: StructuredSerializer<R>>(self, visitor: V) -> Result<R> {
1212
let vec: Vec<u8> = serde_json::to_vec(&self)?;
1313
visitor.set_structured_event(vec)
1414
}
@@ -18,11 +18,11 @@ impl BinaryDeserializer for Event {
1818
fn deserialize_binary<R: Sized, V: BinarySerializer<R>>(
1919
self,
2020
mut visitor: V,
21-
) -> Result<R, Error> {
22-
visitor.set_spec_version(self.get_specversion())?;
23-
self.attributes.deserialize_attributes(&mut visitor)?;
21+
) -> Result<R> {
22+
visitor = visitor.set_spec_version(self.get_specversion())?;
23+
visitor = self.attributes.deserialize_attributes(visitor)?;
2424
for (k, v) in self.extensions.into_iter() {
25-
visitor.set_extension(&k, v.into())?;
25+
visitor = visitor.set_extension(&k, v.into())?;
2626
}
2727
match self.data {
2828
Some(Data::String(s)) => visitor.end_with_data(s.into_bytes()),
@@ -39,23 +39,23 @@ impl BinaryDeserializer for Event {
3939
pub(crate) trait AttributesDeserializer {
4040
fn deserialize_attributes<R: Sized, V: BinarySerializer<R>>(
4141
self,
42-
visitor: &mut V,
43-
) -> DeserializationResult;
42+
visitor: V,
43+
) -> Result<V>;
4444
}
4545

4646
pub(crate) trait AttributesSerializer {
4747
fn serialize_attribute(
4848
&mut self,
4949
name: &str,
5050
value: MessageAttributeValue,
51-
) -> SerializationResult;
51+
) -> Result<()>;
5252
}
5353

5454
impl AttributesDeserializer for Attributes {
5555
fn deserialize_attributes<R: Sized, V: BinarySerializer<R>>(
5656
self,
57-
visitor: &mut V,
58-
) -> DeserializationResult {
57+
visitor: V,
58+
) -> Result<V> {
5959
match self {
6060
Attributes::V03(v03) => v03.deserialize_attributes(visitor),
6161
Attributes::V10(v10) => v10.deserialize_attributes(visitor),
@@ -68,7 +68,7 @@ impl AttributesSerializer for Attributes {
6868
&mut self,
6969
name: &str,
7070
value: MessageAttributeValue,
71-
) -> SerializationResult {
71+
) -> Result<()> {
7272
match self {
7373
Attributes::V03(v03) => v03.serialize_attribute(name, value),
7474
Attributes::V10(v10) => v10.serialize_attribute(name, value),
@@ -77,7 +77,7 @@ impl AttributesSerializer for Attributes {
7777
}
7878

7979
impl StructuredSerializer<Event> for Event {
80-
fn set_structured_event(mut self, bytes: Vec<u8>) -> Result<Event, Error> {
80+
fn set_structured_event(mut self, bytes: Vec<u8>) -> Result<Event> {
8181
let new_event: Event = serde_json::from_slice(&bytes)?;
8282
self.attributes = new_event.attributes;
8383
self.data = new_event.data;
@@ -87,29 +87,30 @@ impl StructuredSerializer<Event> for Event {
8787
}
8888

8989
impl BinarySerializer<Event> for Event {
90-
fn set_spec_version(&mut self, spec_version: SpecVersion) -> SerializationResult {
90+
fn set_spec_version(mut self, spec_version: SpecVersion) -> Result<Self> {
9191
match spec_version {
9292
SpecVersion::V03 => self.attributes = self.attributes.clone().into_v03(),
9393
SpecVersion::V10 => self.attributes = self.attributes.clone().into_v10(),
9494
}
95-
Ok(())
95+
Ok(self)
9696
}
9797

98-
fn set_attribute(&mut self, name: &str, value: MessageAttributeValue) -> SerializationResult {
99-
self.attributes.serialize_attribute(name, value)
98+
fn set_attribute(mut self, name: &str, value: MessageAttributeValue) -> Result<Self> {
99+
self.attributes.serialize_attribute(name, value)?;
100+
Ok(self)
100101
}
101102

102-
fn set_extension(&mut self, name: &str, value: MessageAttributeValue) -> SerializationResult {
103+
fn set_extension(mut self, name: &str, value: MessageAttributeValue) -> Result<Self> {
103104
self.extensions.insert(name.to_string(), value.into());
104-
Ok(())
105+
Ok(self)
105106
}
106107

107-
fn end_with_data(mut self, bytes: Vec<u8>) -> Result<Event, Error> {
108+
fn end_with_data(mut self, bytes: Vec<u8>) -> Result<Event> {
108109
self.data = Some(Data::from_binary(self.get_datacontenttype(), bytes)?);
109110
Ok(self)
110111
}
111112

112-
fn end(self) -> Result<Event, Error> {
113+
fn end(self) -> Result<Event> {
113114
Ok(self)
114115
}
115116
}

src/event/v03/attributes.rs

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::event::attributes::{AttributeValue, AttributesConverter, DataAttributesWriter};
22
use crate::event::AttributesV10;
33
use crate::event::{AttributesReader, AttributesWriter, SpecVersion};
4-
use chrono::{DateTime, NaiveDateTime, Utc};
4+
use chrono::{DateTime, Utc};
55
use hostname::get_hostname;
66
use url::Url;
77
use uuid::Uuid;
@@ -187,34 +187,40 @@ impl AttributesConverter for Attributes {
187187
}
188188
}
189189

190-
#[test]
191-
fn iterator_test_V03() {
192-
let a = Attributes {
193-
id: String::from("1"),
194-
ty: String::from("someType"),
195-
source: Url::parse("https://example.net").unwrap(),
196-
datacontenttype: None,
197-
schemaurl: None,
198-
subject: None,
199-
time: Some(DateTime::<Utc>::from_utc(
200-
NaiveDateTime::from_timestamp(61, 0),
201-
Utc,
202-
)),
203-
};
204-
let b = &mut a.into_iter();
205-
let time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);
206-
207-
assert_eq!(("id", AttributeValue::String("1")), b.next().unwrap());
208-
assert_eq!(
209-
("type", AttributeValue::String("someType")),
210-
b.next().unwrap()
211-
);
212-
assert_eq!(
213-
(
214-
"source",
215-
AttributeValue::URIRef(&Url::parse("https://example.net").unwrap())
216-
),
217-
b.next().unwrap()
218-
);
219-
assert_eq!(("time", AttributeValue::Time(&time)), b.next().unwrap());
190+
#[cfg(test)]
191+
mod tests {
192+
use super::*;
193+
use chrono::NaiveDateTime;
194+
195+
#[test]
196+
fn iterator_test_V03() {
197+
let a = Attributes {
198+
id: String::from("1"),
199+
ty: String::from("someType"),
200+
source: Url::parse("https://example.net").unwrap(),
201+
datacontenttype: None,
202+
schemaurl: None,
203+
subject: None,
204+
time: Some(DateTime::<Utc>::from_utc(
205+
NaiveDateTime::from_timestamp(61, 0),
206+
Utc,
207+
)),
208+
};
209+
let b = &mut a.into_iter();
210+
let time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);
211+
212+
assert_eq!(("id", AttributeValue::String("1")), b.next().unwrap());
213+
assert_eq!(
214+
("type", AttributeValue::String("someType")),
215+
b.next().unwrap()
216+
);
217+
assert_eq!(
218+
(
219+
"source",
220+
AttributeValue::URIRef(&Url::parse("https://example.net").unwrap())
221+
),
222+
b.next().unwrap()
223+
);
224+
assert_eq!(("time", AttributeValue::Time(&time)), b.next().unwrap());
225+
}
220226
}

src/event/v03/message.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
use crate::message::{
2-
BinarySerializer, DeserializationResult, Error, MessageAttributeValue, SerializationResult,
2+
BinarySerializer, Result, Error, MessageAttributeValue,
33
};
44
use std::convert::TryInto;
55

66
impl crate::event::message::AttributesDeserializer for super::Attributes {
77
fn deserialize_attributes<R: Sized, V: BinarySerializer<R>>(
88
self,
9-
visitor: &mut V,
10-
) -> DeserializationResult {
11-
visitor.set_attribute("id", MessageAttributeValue::String(self.id))?;
12-
visitor.set_attribute("type", MessageAttributeValue::String(self.ty))?;
13-
visitor.set_attribute("source", MessageAttributeValue::UriRef(self.source))?;
9+
mut visitor: V,
10+
) -> Result<V> {
11+
visitor = visitor.set_attribute("id", MessageAttributeValue::String(self.id))?;
12+
visitor = visitor.set_attribute("type", MessageAttributeValue::String(self.ty))?;
13+
visitor = visitor.set_attribute("source", MessageAttributeValue::UriRef(self.source))?;
1414
if self.datacontenttype.is_some() {
15-
visitor.set_attribute(
15+
visitor = visitor.set_attribute(
1616
"datacontenttype",
1717
MessageAttributeValue::String(self.datacontenttype.unwrap()),
1818
)?;
1919
}
2020
if self.schemaurl.is_some() {
21-
visitor.set_attribute(
21+
visitor = visitor.set_attribute(
2222
"schemaurl",
2323
MessageAttributeValue::Uri(self.schemaurl.unwrap()),
2424
)?;
2525
}
2626
if self.subject.is_some() {
27-
visitor.set_attribute(
27+
visitor = visitor.set_attribute(
2828
"subject",
2929
MessageAttributeValue::String(self.subject.unwrap()),
3030
)?;
3131
}
3232
if self.time.is_some() {
33-
visitor.set_attribute("time", MessageAttributeValue::DateTime(self.time.unwrap()))?;
33+
visitor = visitor.set_attribute("time", MessageAttributeValue::DateTime(self.time.unwrap()))?;
3434
}
35-
Ok(())
35+
Ok(visitor)
3636
}
3737
}
3838

@@ -41,7 +41,7 @@ impl crate::event::message::AttributesSerializer for super::Attributes {
4141
&mut self,
4242
name: &str,
4343
value: MessageAttributeValue,
44-
) -> SerializationResult {
44+
) -> Result<()> {
4545
match name {
4646
"id" => self.id = value.to_string(),
4747
"type" => self.ty = value.to_string(),

src/event/v10/attributes.rs

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::event::attributes::{AttributeValue, AttributesConverter, DataAttributesWriter};
22
use crate::event::{AttributesReader, AttributesV03, AttributesWriter, SpecVersion};
3-
use chrono::{DateTime, NaiveDateTime, Utc};
3+
use chrono::{DateTime, Utc};
44
use hostname::get_hostname;
55
use url::Url;
66
use uuid::Uuid;
@@ -186,34 +186,40 @@ impl AttributesConverter for Attributes {
186186
}
187187
}
188188

189-
#[test]
190-
fn iterator_test_V10() {
191-
let a = Attributes {
192-
id: String::from("1"),
193-
ty: String::from("someType"),
194-
source: Url::parse("https://example.net").unwrap(),
195-
datacontenttype: None,
196-
dataschema: None,
197-
subject: None,
198-
time: Some(DateTime::<Utc>::from_utc(
199-
NaiveDateTime::from_timestamp(61, 0),
200-
Utc,
201-
)),
202-
};
203-
let b = &mut a.into_iter();
204-
let time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);
205-
206-
assert_eq!(("id", AttributeValue::String("1")), b.next().unwrap());
207-
assert_eq!(
208-
("type", AttributeValue::String("someType")),
209-
b.next().unwrap()
210-
);
211-
assert_eq!(
212-
(
213-
"source",
214-
AttributeValue::URIRef(&Url::parse("https://example.net").unwrap())
215-
),
216-
b.next().unwrap()
217-
);
218-
assert_eq!(("time", AttributeValue::Time(&time)), b.next().unwrap());
189+
#[cfg(test)]
190+
mod tests {
191+
use super::*;
192+
use chrono::NaiveDateTime;
193+
194+
#[test]
195+
fn iterator_test_V10() {
196+
let a = Attributes {
197+
id: String::from("1"),
198+
ty: String::from("someType"),
199+
source: Url::parse("https://example.net").unwrap(),
200+
datacontenttype: None,
201+
dataschema: None,
202+
subject: None,
203+
time: Some(DateTime::<Utc>::from_utc(
204+
NaiveDateTime::from_timestamp(61, 0),
205+
Utc,
206+
)),
207+
};
208+
let b = &mut a.into_iter();
209+
let time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);
210+
211+
assert_eq!(("id", AttributeValue::String("1")), b.next().unwrap());
212+
assert_eq!(
213+
("type", AttributeValue::String("someType")),
214+
b.next().unwrap()
215+
);
216+
assert_eq!(
217+
(
218+
"source",
219+
AttributeValue::URIRef(&Url::parse("https://example.net").unwrap())
220+
),
221+
b.next().unwrap()
222+
);
223+
assert_eq!(("time", AttributeValue::Time(&time)), b.next().unwrap());
224+
}
219225
}

src/event/v10/message.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
use crate::message::{
2-
BinarySerializer, DeserializationResult, Error, MessageAttributeValue, SerializationResult,
2+
BinarySerializer, Error, MessageAttributeValue, Result,
33
};
44
use std::convert::TryInto;
55

66
impl crate::event::message::AttributesDeserializer for super::Attributes {
77
fn deserialize_attributes<R: Sized, V: BinarySerializer<R>>(
88
self,
9-
visitor: &mut V,
10-
) -> DeserializationResult {
11-
visitor.set_attribute("id", MessageAttributeValue::String(self.id))?;
12-
visitor.set_attribute("type", MessageAttributeValue::String(self.ty))?;
13-
visitor.set_attribute("source", MessageAttributeValue::UriRef(self.source))?;
9+
mut visitor: V,
10+
) -> Result<V> {
11+
visitor = visitor.set_attribute("id", MessageAttributeValue::String(self.id))?;
12+
visitor = visitor.set_attribute("type", MessageAttributeValue::String(self.ty))?;
13+
visitor = visitor.set_attribute("source", MessageAttributeValue::UriRef(self.source))?;
1414
if self.datacontenttype.is_some() {
15-
visitor.set_attribute(
15+
visitor = visitor.set_attribute(
1616
"datacontenttype",
1717
MessageAttributeValue::String(self.datacontenttype.unwrap()),
1818
)?;
1919
}
2020
if self.dataschema.is_some() {
21-
visitor.set_attribute(
21+
visitor = visitor.set_attribute(
2222
"dataschema",
2323
MessageAttributeValue::Uri(self.dataschema.unwrap()),
2424
)?;
2525
}
2626
if self.subject.is_some() {
27-
visitor.set_attribute(
27+
visitor = visitor.set_attribute(
2828
"subject",
2929
MessageAttributeValue::String(self.subject.unwrap()),
3030
)?;
3131
}
3232
if self.time.is_some() {
33-
visitor.set_attribute("time", MessageAttributeValue::DateTime(self.time.unwrap()))?;
33+
visitor = visitor.set_attribute("time", MessageAttributeValue::DateTime(self.time.unwrap()))?;
3434
}
35-
Ok(())
35+
Ok(visitor)
3636
}
3737
}
3838

@@ -41,7 +41,7 @@ impl crate::event::message::AttributesSerializer for super::Attributes {
4141
&mut self,
4242
name: &str,
4343
value: MessageAttributeValue,
44-
) -> SerializationResult {
44+
) -> Result<()> {
4545
match name {
4646
"id" => self.id = value.to_string(),
4747
"type" => self.ty = value.to_string(),

0 commit comments

Comments
 (0)