Skip to content

Commit 2e66f6a

Browse files
Now set_attribute returns the previous attribute value (#86)
remove_data renamed to take_data and now returns the previous data value Signed-off-by: Francesco Guardiani <[email protected]>
1 parent 5e0067b commit 2e66f6a

File tree

4 files changed

+91
-54
lines changed

4 files changed

+91
-54
lines changed

src/event/attributes.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,20 @@ pub trait AttributesReader {
6666
/// Trait to set [CloudEvents Context attributes](https://github.com/cloudevents/spec/blob/master/spec.md#context-attributes).
6767
pub trait AttributesWriter {
6868
/// Set the [id](https://github.com/cloudevents/spec/blob/master/spec.md#id).
69-
fn set_id(&mut self, id: impl Into<String>);
69+
/// Returns the previous value.
70+
fn set_id(&mut self, id: impl Into<String>) -> String;
7071
/// Set the [source](https://github.com/cloudevents/spec/blob/master/spec.md#source-1).
71-
fn set_source(&mut self, source: impl Into<Url>);
72+
/// Returns the previous value.
73+
fn set_source(&mut self, source: impl Into<Url>) -> Url;
7274
/// Set the [type](https://github.com/cloudevents/spec/blob/master/spec.md#type).
73-
fn set_type(&mut self, ty: impl Into<String>);
75+
/// Returns the previous value.
76+
fn set_type(&mut self, ty: impl Into<String>) -> String;
7477
/// Set the [subject](https://github.com/cloudevents/spec/blob/master/spec.md#subject).
75-
fn set_subject(&mut self, subject: Option<impl Into<String>>);
78+
/// Returns the previous value.
79+
fn set_subject(&mut self, subject: Option<impl Into<String>>) -> Option<String>;
7680
/// Set the [time](https://github.com/cloudevents/spec/blob/master/spec.md#time).
77-
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>);
81+
/// Returns the previous value.
82+
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>) -> Option<DateTime<Utc>>;
7883
}
7984

8085
pub(crate) trait AttributesConverter {
@@ -83,8 +88,9 @@ pub(crate) trait AttributesConverter {
8388
}
8489

8590
pub(crate) trait DataAttributesWriter {
86-
fn set_datacontenttype(&mut self, datacontenttype: Option<impl Into<String>>);
87-
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>);
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>;
8894
}
8995

9096
#[derive(PartialEq, Debug, Clone, Copy)]
@@ -169,35 +175,35 @@ impl AttributesReader for Attributes {
169175
}
170176

171177
impl AttributesWriter for Attributes {
172-
fn set_id(&mut self, id: impl Into<String>) {
178+
fn set_id(&mut self, id: impl Into<String>) -> String {
173179
match self {
174180
Attributes::V03(a) => a.set_id(id),
175181
Attributes::V10(a) => a.set_id(id),
176182
}
177183
}
178184

179-
fn set_source(&mut self, source: impl Into<Url>) {
185+
fn set_source(&mut self, source: impl Into<Url>) -> Url {
180186
match self {
181187
Attributes::V03(a) => a.set_source(source),
182188
Attributes::V10(a) => a.set_source(source),
183189
}
184190
}
185191

186-
fn set_type(&mut self, ty: impl Into<String>) {
192+
fn set_type(&mut self, ty: impl Into<String>) -> String {
187193
match self {
188194
Attributes::V03(a) => a.set_type(ty),
189195
Attributes::V10(a) => a.set_type(ty),
190196
}
191197
}
192198

193-
fn set_subject(&mut self, subject: Option<impl Into<String>>) {
199+
fn set_subject(&mut self, subject: Option<impl Into<String>>) -> Option<String> {
194200
match self {
195201
Attributes::V03(a) => a.set_subject(subject),
196202
Attributes::V10(a) => a.set_subject(subject),
197203
}
198204
}
199205

200-
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>) {
206+
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>) -> Option<DateTime<Utc>> {
201207
match self {
202208
Attributes::V03(a) => a.set_time(time),
203209
Attributes::V10(a) => a.set_time(time),
@@ -206,14 +212,17 @@ impl AttributesWriter for Attributes {
206212
}
207213

208214
impl DataAttributesWriter for Attributes {
209-
fn set_datacontenttype(&mut self, datacontenttype: Option<impl Into<String>>) {
215+
fn set_datacontenttype(
216+
&mut self,
217+
datacontenttype: Option<impl Into<String>>,
218+
) -> Option<String> {
210219
match self {
211220
Attributes::V03(a) => a.set_datacontenttype(datacontenttype),
212221
Attributes::V10(a) => a.set_datacontenttype(datacontenttype),
213222
}
214223
}
215224

216-
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) {
225+
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) -> Option<Url> {
217226
match self {
218227
Attributes::V03(a) => a.set_dataschema(dataschema),
219228
Attributes::V10(a) => a.set_dataschema(dataschema),

src/event/event.rs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ impl AttributesReader for Event {
5454

5555
#[delegate(self.attributes)]
5656
impl AttributesWriter for Event {
57-
fn set_id(&mut self, id: impl Into<String>);
58-
fn set_source(&mut self, source: impl Into<Url>);
59-
fn set_type(&mut self, ty: impl Into<String>);
60-
fn set_subject(&mut self, subject: Option<impl Into<String>>);
61-
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>);
57+
fn set_id(&mut self, id: impl Into<String>) -> String;
58+
fn set_source(&mut self, source: impl Into<Url>) -> Url;
59+
fn set_type(&mut self, ty: impl Into<String>) -> String;
60+
fn set_subject(&mut self, subject: Option<impl Into<String>>) -> Option<String>;
61+
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>) -> Option<DateTime<Utc>>;
6262
}
6363

6464
impl Default for Event {
@@ -93,11 +93,24 @@ impl Event {
9393
self.extensions.iter().map(|(k, v)| (k.as_str(), v))
9494
}
9595

96-
/// Remove `data`, `dataschema` and `datacontenttype` from this `Event`
97-
pub fn remove_data(&mut self) {
98-
self.data = None;
99-
self.attributes.set_dataschema(None as Option<Url>);
100-
self.attributes.set_datacontenttype(None as Option<String>);
96+
/// Take (`datacontenttype`, `dataschema`, `data`) from this event, leaving these fields empty
97+
///
98+
/// ```
99+
/// use cloudevents::Event;
100+
/// use serde_json::json;
101+
/// use std::convert::Into;
102+
///
103+
/// let mut e = Event::default();
104+
/// e.write_data("application/json", json!({}));
105+
///
106+
/// let (datacontenttype, dataschema, data) = e.take_data();
107+
/// ```
108+
pub fn take_data(&mut self) -> (Option<String>, Option<Url>, Option<Data>) {
109+
(
110+
self.attributes.set_datacontenttype(None as Option<String>),
111+
self.attributes.set_dataschema(None as Option<Url>),
112+
self.data.take(),
113+
)
101114
}
102115

103116
/// Write `data` into this `Event` with the specified `datacontenttype`.
@@ -219,7 +232,7 @@ mod tests {
219232
}
220233

221234
#[test]
222-
fn remove_data() {
235+
fn take_data() {
223236
let mut e = Event::default();
224237
e.write_data(
225238
"application/json",
@@ -228,13 +241,22 @@ mod tests {
228241
}),
229242
);
230243

231-
e.remove_data();
244+
let _d = e.take_data();
232245

233246
assert!(e.try_get_data::<serde_json::Value>().unwrap().is_none());
234247
assert!(e.get_dataschema().is_none());
235248
assert!(e.get_datacontenttype().is_none());
236249
}
237250

251+
#[test]
252+
fn set_id() {
253+
let mut e = Event::default();
254+
e.set_id("001");
255+
256+
assert_eq!(e.set_id("002"), String::from("001"));
257+
assert_eq!(e.get_id(), "002")
258+
}
259+
238260
#[test]
239261
fn iter() {
240262
let mut e = Event::default();

src/event/v03/attributes.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,34 +122,37 @@ impl AttributesReader for Attributes {
122122
}
123123

124124
impl AttributesWriter for Attributes {
125-
fn set_id(&mut self, id: impl Into<String>) {
126-
self.id = id.into()
125+
fn set_id(&mut self, id: impl Into<String>) -> String {
126+
std::mem::replace(&mut self.id, id.into())
127127
}
128128

129-
fn set_source(&mut self, source: impl Into<Url>) {
130-
self.source = source.into()
129+
fn set_source(&mut self, source: impl Into<Url>) -> Url {
130+
std::mem::replace(&mut self.source, source.into())
131131
}
132132

133-
fn set_type(&mut self, ty: impl Into<String>) {
134-
self.ty = ty.into()
133+
fn set_type(&mut self, ty: impl Into<String>) -> String {
134+
std::mem::replace(&mut self.ty, ty.into())
135135
}
136136

137-
fn set_subject(&mut self, subject: Option<impl Into<String>>) {
138-
self.subject = subject.map(Into::into)
137+
fn set_subject(&mut self, subject: Option<impl Into<String>>) -> Option<String> {
138+
std::mem::replace(&mut self.subject, subject.map(Into::into))
139139
}
140140

141-
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>) {
142-
self.time = time.map(Into::into)
141+
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>) -> Option<DateTime<Utc>> {
142+
std::mem::replace(&mut self.time, time.map(Into::into))
143143
}
144144
}
145145

146146
impl DataAttributesWriter for Attributes {
147-
fn set_datacontenttype(&mut self, datacontenttype: Option<impl Into<String>>) {
148-
self.datacontenttype = datacontenttype.map(Into::into)
147+
fn set_datacontenttype(
148+
&mut self,
149+
datacontenttype: Option<impl Into<String>>,
150+
) -> Option<String> {
151+
std::mem::replace(&mut self.datacontenttype, datacontenttype.map(Into::into))
149152
}
150153

151-
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) {
152-
self.schemaurl = dataschema.map(Into::into)
154+
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) -> Option<Url> {
155+
std::mem::replace(&mut self.schemaurl, dataschema.map(Into::into))
153156
}
154157
}
155158

src/event/v10/attributes.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,34 +122,37 @@ impl AttributesReader for Attributes {
122122
}
123123

124124
impl AttributesWriter for Attributes {
125-
fn set_id(&mut self, id: impl Into<String>) {
126-
self.id = id.into()
125+
fn set_id(&mut self, id: impl Into<String>) -> String {
126+
std::mem::replace(&mut self.id, id.into())
127127
}
128128

129-
fn set_source(&mut self, source: impl Into<Url>) {
130-
self.source = source.into()
129+
fn set_source(&mut self, source: impl Into<Url>) -> Url {
130+
std::mem::replace(&mut self.source, source.into())
131131
}
132132

133-
fn set_type(&mut self, ty: impl Into<String>) {
134-
self.ty = ty.into()
133+
fn set_type(&mut self, ty: impl Into<String>) -> String {
134+
std::mem::replace(&mut self.ty, ty.into())
135135
}
136136

137-
fn set_subject(&mut self, subject: Option<impl Into<String>>) {
138-
self.subject = subject.map(Into::into)
137+
fn set_subject(&mut self, subject: Option<impl Into<String>>) -> Option<String> {
138+
std::mem::replace(&mut self.subject, subject.map(Into::into))
139139
}
140140

141-
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>) {
142-
self.time = time.map(Into::into)
141+
fn set_time(&mut self, time: Option<impl Into<DateTime<Utc>>>) -> Option<DateTime<Utc>> {
142+
std::mem::replace(&mut self.time, time.map(Into::into))
143143
}
144144
}
145145

146146
impl DataAttributesWriter for Attributes {
147-
fn set_datacontenttype(&mut self, datacontenttype: Option<impl Into<String>>) {
148-
self.datacontenttype = datacontenttype.map(Into::into)
147+
fn set_datacontenttype(
148+
&mut self,
149+
datacontenttype: Option<impl Into<String>>,
150+
) -> Option<String> {
151+
std::mem::replace(&mut self.datacontenttype, datacontenttype.map(Into::into))
149152
}
150153

151-
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) {
152-
self.dataschema = dataschema.map(Into::into)
154+
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>) -> Option<Url> {
155+
std::mem::replace(&mut self.dataschema, dataschema.map(Into::into))
153156
}
154157
}
155158

0 commit comments

Comments
 (0)