@@ -2,7 +2,6 @@ use super::{
22 AttributeValue , Attributes , AttributesReader , AttributesV10 , AttributesWriter , Data ,
33 ExtensionValue , SpecVersion ,
44} ;
5- use crate :: event:: attributes:: DataAttributesWriter ;
65use chrono:: { DateTime , Utc } ;
76use delegate_attr:: delegate;
87use 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
7072impl 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"
0 commit comments