1- use crate :: { Context , Id , Subject , UriReference } ;
1+ use crate :: { Context , ContextEnum , Id , Subject , UriReference } ;
22use serde:: {
33 de:: { self , Deserializer , MapAccess , Visitor } ,
44 Deserialize , Serialize ,
@@ -8,7 +8,7 @@ use std::fmt;
88#[ derive( Debug , Clone , Serialize , PartialEq , Eq ) ]
99#[ serde( deny_unknown_fields) ]
1010pub struct CDEvent {
11- context : Context ,
11+ context : ContextEnum ,
1212 subject : Subject ,
1313 #[ serde( rename = "customData" , skip_serializing_if = "Option::is_none" ) ]
1414 custom_data : Option < serde_json:: Value > ,
@@ -21,10 +21,8 @@ pub struct CDEvent {
2121
2222impl From < Subject > for CDEvent {
2323 fn from ( subject : Subject ) -> Self {
24- let context = Context {
25- ty : subject. content ( ) . ty ( ) . into ( ) ,
26- ..Default :: default ( )
27- } ;
24+ // TODO select context from subject version
25+ let context = crate :: new_context ( subject. content ( ) . ty ( ) ) ;
2826 Self {
2927 context,
3028 subject,
@@ -37,41 +35,41 @@ impl From<Subject> for CDEvent {
3735impl CDEvent {
3836 /// see <https://github.com/cdevents/spec/blob/main/spec.md#version>
3937 pub fn version ( & self ) -> & str {
40- self . context . version . as_str ( )
38+ self . context . version ( )
4139 }
4240
43- pub fn with_version < T > ( mut self , v : T ) -> Self where T : Into < String > {
44- self . context . version = v . into ( ) ;
45- self
46- }
41+ // pub fn with_version<T>(mut self, v: T) -> Self where T: Into<String> {
42+ // self.context.with_version(v );
43+ // self
44+ // }
4745
4846 /// see <https://github.com/cdevents/spec/blob/main/spec.md#id-context>
4947 pub fn id ( & self ) -> & Id {
50- & self . context . id
48+ self . context . id ( )
5149 }
5250
5351 pub fn with_id ( mut self , v : Id ) -> Self {
54- self . context . id = v ;
52+ self . context = self . context . with_id ( v ) ;
5553 self
5654 }
5755
5856 /// see <https://github.com/cdevents/spec/blob/main/spec.md#source-context>
5957 pub fn source ( & self ) -> & UriReference {
60- & self . context . source
58+ self . context . source ( )
6159 }
6260
6361 pub fn with_source ( mut self , v : UriReference ) -> Self {
64- self . context . source = v ;
62+ self . context = self . context . with_source ( v ) ;
6563 self
6664 }
6765
6866 /// see <https://github.com/cdevents/spec/blob/main/spec.md#timestamp>
6967 pub fn timestamp ( & self ) -> & time:: OffsetDateTime {
70- & self . context . timestamp
68+ self . context . timestamp ( )
7169 }
7270
7371 pub fn with_timestamp ( mut self , v : time:: OffsetDateTime ) -> Self {
74- self . context . timestamp = v ;
72+ self . context = self . context . with_timestamp ( v ) ;
7573 self
7674 }
7775
@@ -84,7 +82,7 @@ impl CDEvent {
8482 /// derived from subject.content
8583 pub fn ty ( & self ) -> & str {
8684 //self.subject.content().ty()
87- self . context . ty . as_str ( )
85+ self . context . ty ( )
8886 }
8987
9088 /// see <https://github.com/cdevents/spec/blob/main/spec.md#customdata>
@@ -127,6 +125,7 @@ impl<'de> Deserialize<'de> for CDEvent {
127125
128126 struct CDEventVisitor ;
129127
128+ // TODO remove dependencie to serde_json
130129 impl < ' de > Visitor < ' de > for CDEventVisitor {
131130 type Value = CDEvent ;
132131
@@ -138,17 +137,17 @@ impl<'de> Deserialize<'de> for CDEvent {
138137 where
139138 V : MapAccess < ' de > ,
140139 {
141- let mut context : Option < Context > = None ;
140+ let mut context_json : Option < serde_json :: value :: Value > = None ;
142141 let mut subject_json: Option < serde_json:: value:: Value > = None ;
143142 let mut custom_data = None ;
144143 let mut custom_data_content_type = None ;
145144 while let Some ( key) = map. next_key ( ) ? {
146145 match key {
147146 Field :: Context => {
148- if context . is_some ( ) {
147+ if context_json . is_some ( ) {
149148 return Err ( de:: Error :: duplicate_field ( "context" ) ) ;
150149 }
151- context = Some ( map. next_value ( ) ?) ;
150+ context_json = Some ( map. next_value ( ) ?) ;
152151 }
153152 Field :: Subject => {
154153 if subject_json. is_some ( ) {
@@ -170,11 +169,14 @@ impl<'de> Deserialize<'de> for CDEvent {
170169 }
171170 }
172171 }
173- let context = context. ok_or_else ( || de:: Error :: missing_field ( "context" ) ) ?;
174- let subject_json =
175- subject_json. ok_or_else ( || de:: Error :: missing_field ( "subject" ) ) ?;
176- let subject =
177- Subject :: from_json ( & context. ty , subject_json) . map_err ( de:: Error :: custom) ?;
172+ let context = {
173+ let context_json = context_json. ok_or_else ( || de:: Error :: missing_field ( "context" ) ) ?;
174+ ContextEnum :: from_json ( context_json) . map_err ( de:: Error :: custom) ?
175+ } ;
176+ let subject = {
177+ let subject_json = subject_json. ok_or_else ( || de:: Error :: missing_field ( "subject" ) ) ?;
178+ Subject :: from_json ( context. ty ( ) , subject_json) . map_err ( de:: Error :: custom) ?
179+ } ;
178180
179181 Ok ( CDEvent {
180182 context,
@@ -221,4 +223,4 @@ mod tests {
221223 assert_eq!( s, actual) ;
222224 }
223225 }
224- }
226+ }
0 commit comments