1
- use fe2o3_amqp_types:: primitives :: { SimpleValue , Symbol , Binary } ;
2
- use fe2o3_amqp_types:: messaging :: { Data as AmqpData } ;
1
+ use fe2o3_amqp_types:: messaging :: { Data as AmqpData , Properties , ApplicationProperties } ;
2
+ use fe2o3_amqp_types:: primitives :: { Binary , SimpleValue , Symbol } ;
3
3
4
+ use crate :: binding:: header_prefix;
4
5
use crate :: message:: StructuredSerializer ;
5
- use crate :: { message:: { BinarySerializer , MessageAttributeValue , Error } , event:: SpecVersion } ;
6
+ use crate :: {
7
+ event:: SpecVersion ,
8
+ message:: { BinarySerializer , Error , MessageAttributeValue } ,
9
+ } ;
6
10
7
11
use super :: constants:: DATACONTENTTYPE ;
8
- use super :: { AmqpCloudEvent , ATTRIBUTE_PREFIX , AmqpBody } ;
12
+ use super :: { AmqpBody , AmqpMessage , ATTRIBUTE_PREFIX } ;
9
13
10
- impl BinarySerializer < AmqpCloudEvent > for AmqpCloudEvent {
14
+ impl BinarySerializer < AmqpMessage > for AmqpMessage {
11
15
fn set_spec_version ( mut self , spec_version : SpecVersion ) -> crate :: message:: Result < Self > {
12
16
let key = String :: from ( "cloudEvents:specversion" ) ;
13
17
let value = String :: from ( spec_version. as_str ( ) ) ;
14
- self . application_properties . insert ( key, SimpleValue :: from ( value) ) ;
18
+ self . application_properties
19
+ . get_or_insert ( ApplicationProperties :: default ( ) )
20
+ . insert ( key, SimpleValue :: from ( value) ) ;
15
21
Ok ( self )
16
22
}
17
23
18
- fn set_attribute ( mut self , name : & str , value : MessageAttributeValue ) -> crate :: message:: Result < Self > {
24
+ fn set_attribute (
25
+ mut self ,
26
+ name : & str ,
27
+ value : MessageAttributeValue ,
28
+ ) -> crate :: message:: Result < Self > {
19
29
// For the binary mode, the AMQP content-type property field value maps directly to the
20
30
// CloudEvents datacontenttype attribute.
21
- //
31
+ //
22
32
// All CloudEvents attributes with exception of datacontenttype MUST be individually mapped
23
33
// to and from the AMQP application-properties section.
24
34
if name == DATACONTENTTYPE {
25
- self . content_type = match value {
35
+ self . properties
36
+ . get_or_insert ( Properties :: default ( ) )
37
+ . content_type = match value {
26
38
MessageAttributeValue :: String ( s) => Some ( Symbol :: from ( s) ) ,
27
- _ => return Err ( Error :: WrongEncoding { } )
39
+ _ => return Err ( Error :: WrongEncoding { } ) ,
28
40
}
29
41
} else {
30
42
// CloudEvent attributes are prefixed with "cloudEvents:" for use in the
31
43
// application-properties section
32
- let key = format ! ( "{}:{}" , ATTRIBUTE_PREFIX , name) ;
44
+ let key = header_prefix ( ATTRIBUTE_PREFIX , name) ;
33
45
let value = SimpleValue :: from ( value) ;
34
- self . application_properties . insert ( key, value) ;
46
+ self . application_properties
47
+ . get_or_insert ( ApplicationProperties :: default ( ) )
48
+ . insert ( key, value) ;
35
49
}
36
50
37
51
Ok ( self )
@@ -43,10 +57,16 @@ impl BinarySerializer<AmqpCloudEvent> for AmqpCloudEvent {
43
57
// systems that also process the message. Extension specifications that do this SHOULD specify
44
58
// how receivers are to interpret messages if the copied values differ from the cloud-event
45
59
// serialized values.
46
- fn set_extension ( mut self , name : & str , value : MessageAttributeValue ) -> crate :: message:: Result < Self > {
47
- let key = format ! ( "{}:{}" , ATTRIBUTE_PREFIX , name) ;
60
+ fn set_extension (
61
+ mut self ,
62
+ name : & str ,
63
+ value : MessageAttributeValue ,
64
+ ) -> crate :: message:: Result < Self > {
65
+ let key = header_prefix ( ATTRIBUTE_PREFIX , name) ;
48
66
let value = SimpleValue :: from ( value) ;
49
- self . application_properties . insert ( key, value) ;
67
+ self . application_properties
68
+ . get_or_insert ( ApplicationProperties :: default ( ) )
69
+ . insert ( key, value) ;
50
70
Ok ( self )
51
71
}
52
72
@@ -61,9 +81,11 @@ impl BinarySerializer<AmqpCloudEvent> for AmqpCloudEvent {
61
81
}
62
82
}
63
83
64
- impl StructuredSerializer < AmqpCloudEvent > for AmqpCloudEvent {
84
+ impl StructuredSerializer < AmqpMessage > for AmqpMessage {
65
85
fn set_structured_event ( mut self , bytes : Vec < u8 > ) -> crate :: message:: Result < Self > {
66
- self . content_type = Some ( Symbol :: from ( "application/cloudevents+json; charset=utf-8" ) ) ;
86
+ self . properties
87
+ . get_or_insert ( Properties :: default ( ) )
88
+ . content_type = Some ( Symbol :: from ( "application/cloudevents+json; charset=utf-8" ) ) ;
67
89
self . body = AmqpBody :: Data ( AmqpData ( Binary :: from ( bytes) ) ) ;
68
90
Ok ( self )
69
91
}
0 commit comments