Skip to content

Commit c4305e0

Browse files
Modify data writer APIs on Event (cloudevents#92)
* Modify setters apis Signed-off-by: Francesco Guardiani <[email protected]> * Cargo fmt Signed-off-by: Francesco Guardiani <[email protected]> * Switched into impls in from Signed-off-by: Francesco Guardiani <[email protected]> * fmt Signed-off-by: Francesco Guardiani <[email protected]>
1 parent 39af2d7 commit c4305e0

File tree

7 files changed

+51
-62
lines changed

7 files changed

+51
-62
lines changed

src/event/attributes.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,20 @@ pub trait AttributesWriter {
8080
/// Set the [time](https://github.com/cloudevents/spec/blob/master/spec.md#time).
8181
/// Returns the previous value.
8282
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>) -> Option<DateTime<Utc>>;
83+
/// Set the [datacontenttype](https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype).
84+
/// Returns the previous value.
85+
fn set_datacontenttype(&mut self, datacontenttype: Option<impl Into<String>>)
86+
-> Option<String>;
87+
/// Set the [dataschema](https://github.com/cloudevents/spec/blob/master/spec.md#dataschema).
88+
/// Returns the previous value.
89+
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) -> Option<Url>;
8390
}
8491

8592
pub(crate) trait AttributesConverter {
8693
fn into_v03(self) -> AttributesV03;
8794
fn into_v10(self) -> AttributesV10;
8895
}
8996

90-
pub(crate) trait DataAttributesWriter {
91-
fn set_datacontenttype(&mut self, datacontenttype: Option<impl Into<String>>)
92-
-> Option<String>;
93-
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) -> Option<Url>;
94-
}
95-
9697
#[derive(PartialEq, Debug, Clone, Copy)]
9798
pub(crate) enum AttributesIter<'a> {
9899
IterV03(AttributesIntoIteratorV03<'a>),
@@ -209,9 +210,7 @@ impl AttributesWriter for Attributes {
209210
Attributes::V10(a) => a.set_time(time),
210211
}
211212
}
212-
}
213213

214-
impl DataAttributesWriter for Attributes {
215214
fn set_datacontenttype(
216215
&mut self,
217216
datacontenttype: Option<impl Into<String>>,

src/event/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ where
2525
/// Create a new empty builder
2626
fn new() -> Self;
2727

28-
/// Build [`super::Event`]
29-
fn build(self) -> Result<super::Event, Error>;
28+
/// Build [`Event`]
29+
fn build(self) -> Result<Event, Error>;
3030
}
3131

3232
/// Represents an error during build process

src/event/data.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use serde::export::Formatter;
2+
use serde_json::Value;
23
use std::convert::{Into, TryFrom};
34
use std::fmt;
45

@@ -51,21 +52,21 @@ pub(crate) fn is_json_content_type(ct: &str) -> bool {
5152
ct.starts_with("application/json") || ct.starts_with("text/json") || ct.ends_with("+json")
5253
}
5354

54-
impl Into<Data> for serde_json::Value {
55-
fn into(self) -> Data {
56-
Data::Json(self)
55+
impl From<serde_json::Value> for Data {
56+
fn from(value: Value) -> Self {
57+
Data::Json(value)
5758
}
5859
}
5960

60-
impl Into<Data> for Vec<u8> {
61-
fn into(self) -> Data {
62-
Data::Binary(self)
61+
impl From<Vec<u8>> for Data {
62+
fn from(value: Vec<u8>) -> Self {
63+
Data::Binary(value)
6364
}
6465
}
6566

66-
impl Into<Data> for String {
67-
fn into(self) -> Data {
68-
Data::String(self)
67+
impl From<String> for Data {
68+
fn from(value: String) -> Self {
69+
Data::String(value)
6970
}
7071
}
7172

src/event/event.rs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::{
22
AttributeValue, Attributes, AttributesReader, AttributesV10, AttributesWriter, Data,
33
ExtensionValue, SpecVersion,
44
};
5-
use crate::event::attributes::DataAttributesWriter;
65
use chrono::{DateTime, Utc};
76
use delegate_attr::delegate;
87
use std::collections::HashMap;
@@ -22,7 +21,7 @@ use url::Url;
2221
/// # fn main() -> Result<(), Box<dyn Error>> {
2322
/// // Create an event using the Default trait
2423
/// let mut e = Event::default();
25-
/// e.write_data(
24+
/// e.set_data(
2625
/// "application/json",
2726
/// serde_json::json!({"hello": "world"})
2827
/// );
@@ -65,6 +64,9 @@ impl AttributesWriter for Event {
6564
fn set_type(&mut self, ty: impl Into<String>) -> String;
6665
fn set_subject(&mut self, subject: Option<impl Into<String>>) -> Option<String>;
6766
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>) -> Option<DateTime<Utc>>;
67+
fn set_datacontenttype(&mut self, datacontenttype: Option<impl Into<String>>)
68+
-> Option<String>;
69+
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) -> Option<Url>;
6870
}
6971

7072
impl Default for Event {
@@ -96,6 +98,11 @@ impl Event {
9698
self.extensions.iter().map(|(k, v)| (k.as_str(), v))
9799
}
98100

101+
/// Get `data` from this `Event`
102+
pub fn data(&self) -> Option<&Data> {
103+
self.data.as_ref()
104+
}
105+
99106
/// Take (`datacontenttype`, `dataschema`, `data`) from this event, leaving these fields empty
100107
///
101108
/// ```
@@ -104,7 +111,7 @@ impl Event {
104111
/// use std::convert::Into;
105112
///
106113
/// let mut e = Event::default();
107-
/// e.write_data("application/json", json!({}));
114+
/// e.set_data("application/json", json!({}));
108115
///
109116
/// let (datacontenttype, dataschema, data) = e.take_data();
110117
/// ```
@@ -116,51 +123,41 @@ impl Event {
116123
)
117124
}
118125

119-
/// Write `data` into this `Event` with the specified `datacontenttype`.
126+
/// Set `data` into this `Event` with the specified `datacontenttype`.
127+
/// Returns the previous value of `datacontenttype` and `data`.
120128
///
121129
/// ```
122130
/// use cloudevents::Event;
123131
/// use serde_json::json;
124132
/// use std::convert::Into;
125133
///
126134
/// let mut e = Event::default();
127-
/// e.write_data("application/json", json!({}))
135+
/// let (old_datacontenttype, old_data) = e.set_data("application/json", json!({}));
128136
/// ```
129-
pub fn write_data(&mut self, datacontenttype: impl Into<String>, data: impl Into<Data>) {
130-
self.attributes.set_datacontenttype(Some(datacontenttype));
131-
self.attributes.set_dataschema(None as Option<Url>);
132-
self.data = Some(data.into());
133-
}
134-
135-
/// Get `data` from this `Event`
136-
pub fn data(&self) -> Option<&Data> {
137-
self.data.as_ref()
137+
pub fn set_data(
138+
&mut self,
139+
datacontenttype: impl Into<String>,
140+
data: impl Into<Data>,
141+
) -> (Option<String>, Option<Data>) {
142+
(
143+
self.attributes.set_datacontenttype(Some(datacontenttype)),
144+
std::mem::replace(&mut self.data, Some(data.into())),
145+
)
138146
}
139147

140-
/// Write `data` into this `Event` with the specified `datacontenttype` and `dataschema`.
148+
/// Set `data` into this `Event`, without checking if there is a `datacontenttype`.
149+
/// Returns the previous value of `data`.
141150
///
142151
/// ```
143152
/// use cloudevents::Event;
144153
/// use serde_json::json;
145154
/// use std::convert::Into;
146-
/// use url::Url;
147155
///
148156
/// let mut e = Event::default();
149-
/// e.write_data_with_schema(
150-
/// "application/json",
151-
/// Url::parse("http://myapplication.com/schema").unwrap(),
152-
/// json!({})
153-
/// )
157+
/// let old_data = e.set_data_unchecked(json!({}));
154158
/// ```
155-
pub fn write_data_with_schema(
156-
&mut self,
157-
datacontenttype: impl Into<String>,
158-
dataschema: impl Into<Url>,
159-
data: impl Into<Data>,
160-
) {
161-
self.attributes.set_datacontenttype(Some(datacontenttype));
162-
self.attributes.set_dataschema(Some(dataschema));
163-
self.data = Some(data.into());
159+
pub fn set_data_unchecked(&mut self, data: impl Into<Data>) -> Option<Data> {
160+
std::mem::replace(&mut self.data, Some(data.into()))
164161
}
165162

166163
/// Get the [extension](https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes) named `extension_name`
@@ -194,7 +191,7 @@ mod tests {
194191
#[test]
195192
fn take_data() {
196193
let mut e = Event::default();
197-
e.write_data(
194+
e.set_data(
198195
"application/json",
199196
serde_json::json!({
200197
"hello": "world"
@@ -225,7 +222,7 @@ mod tests {
225222
fn iter() {
226223
let mut e = Event::default();
227224
e.set_extension("aaa", "bbb");
228-
e.write_data(
225+
e.set_data(
229226
"application/json",
230227
serde_json::json!({
231228
"hello": "world"

src/event/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl TryIntoUrl for String {
2424
}
2525
}
2626

27+
/// Trait to define conversion to [`DateTime`]
2728
pub trait TryIntoTime {
2829
fn into_time(self) -> Result<DateTime<Utc>, chrono::ParseError>;
2930
}
@@ -34,7 +35,6 @@ impl TryIntoTime for DateTime<Utc> {
3435
}
3536
}
3637

37-
/// Trait to define conversion to [`DateTime`]
3838
impl TryIntoTime for &str {
3939
fn into_time(self) -> Result<DateTime<Utc>, chrono::ParseError> {
4040
Ok(DateTime::<Utc>::from(DateTime::parse_from_rfc3339(self)?))

src/event/v03/attributes.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::event::attributes::{
2-
default_hostname, AttributeValue, AttributesConverter, DataAttributesWriter,
3-
};
1+
use crate::event::attributes::{default_hostname, AttributeValue, AttributesConverter};
42
use crate::event::AttributesV10;
53
use crate::event::{AttributesReader, AttributesWriter, SpecVersion};
64
use crate::message::{BinarySerializer, MessageAttributeValue};
@@ -141,9 +139,7 @@ impl AttributesWriter for Attributes {
141139
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>) -> Option<DateTime<Utc>> {
142140
std::mem::replace(&mut self.time, time.map(Into::into))
143141
}
144-
}
145142

146-
impl DataAttributesWriter for Attributes {
147143
fn set_datacontenttype(
148144
&mut self,
149145
datacontenttype: Option<impl Into<String>>,

src/event/v10/attributes.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::event::attributes::{
2-
default_hostname, AttributeValue, AttributesConverter, DataAttributesWriter,
3-
};
1+
use crate::event::attributes::{default_hostname, AttributeValue, AttributesConverter};
42
use crate::event::{AttributesReader, AttributesV03, AttributesWriter, SpecVersion};
53
use crate::message::{BinarySerializer, MessageAttributeValue};
64
use chrono::{DateTime, Utc};
@@ -141,9 +139,7 @@ impl AttributesWriter for Attributes {
141139
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>) -> Option<DateTime<Utc>> {
142140
std::mem::replace(&mut self.time, time.map(Into::into))
143141
}
144-
}
145142

146-
impl DataAttributesWriter for Attributes {
147143
fn set_datacontenttype(
148144
&mut self,
149145
datacontenttype: Option<impl Into<String>>,

0 commit comments

Comments
 (0)