@@ -3,7 +3,7 @@ use pyo3::{
33 types:: { PyAnyMethods , PyBytes , PyDict } ,
44} ;
55
6- use crate :: exceptions:: rust_err:: NatsrpyResult ;
6+ use crate :: { exceptions:: rust_err:: NatsrpyResult , utils :: headers :: NatsrpyHeadermapExt } ;
77
88#[ pyo3:: pyclass( get_all, set_all) ]
99#[ derive( Debug ) ]
@@ -18,29 +18,16 @@ pub struct Message {
1818}
1919
2020impl Message {
21- pub fn from_nats_message (
22- py : Python < ' _ > ,
23- message : async_nats:: Message ,
24- ) -> NatsrpyResult < Self > {
25- let headers = PyDict :: new ( py) ;
26- if let Some ( headermap) = message. headers {
27- for ( header_name, header_val) in headermap. iter ( ) {
28- let py_val = header_val
29- . iter ( )
30- . map ( std:: string:: ToString :: to_string)
31- . collect :: < Vec < _ > > ( ) ;
32- if py_val. len ( ) == 1 {
33- headers. set_item ( header_name. to_string ( ) , py_val. first ( ) ) ?;
34- continue ;
35- }
36- headers. set_item ( header_name. to_string ( ) , py_val) ?;
37- }
38- }
21+ pub fn from_nats_message ( py : Python < ' _ > , message : async_nats:: Message ) -> NatsrpyResult < Self > {
22+ let headers = match message. headers {
23+ Some ( headermap) => headermap. to_pydict ( py) ?,
24+ None => PyDict :: new ( py) . unbind ( ) ,
25+ } ;
3926 Ok ( Self {
4027 subject : message. subject . to_string ( ) ,
4128 reply : message. reply . as_deref ( ) . map ( ToString :: to_string) ,
4229 payload : PyBytes :: new ( py, & message. payload ) . unbind ( ) ,
43- headers : headers. unbind ( ) ,
30+ headers : headers,
4431 status : message. status . map ( Into :: < u16 > :: into) ,
4532 description : message. description ,
4633 length : message. length ,
@@ -50,7 +37,7 @@ impl Message {
5037
5138#[ pyo3:: pymethods]
5239impl Message {
53- #[ must_use]
40+ #[ must_use]
5441 pub fn __repr__ ( & self ) -> String {
5542 format ! (
5643 r#"Message<subject="{subject}", reply={reply:?}, payload={payload}, headers={headers}, description={description:?}, length={len}>"# ,
0 commit comments