1- use pyo3:: { Bound , Py , PyAny , Python , types:: PyDict } ;
1+ use pyo3:: {
2+ Bound , Py , PyAny , Python ,
3+ types:: { PyBytes , PyDict } ,
4+ } ;
25use std:: sync:: Arc ;
36use tokio:: sync:: RwLock ;
47
58use crate :: {
6- exceptions:: rust_err:: NatsrpyResult ,
7- utils:: { headers :: NatsrpyHeadermapExt , natsrpy_future, py_types:: TimeValue } ,
9+ exceptions:: rust_err:: { NatsrpyError , NatsrpyResult } ,
10+ utils:: { natsrpy_future, py_types:: TimeValue } ,
811} ;
912
1013#[ pyo3:: pyclass]
1114pub struct JetStreamMessage {
12- message : async_nats:: Message ,
13- headers : Option < Py < PyDict > > ,
15+ message : crate :: message:: Message ,
1416 acker : Arc < RwLock < async_nats:: jetstream:: message:: Acker > > ,
1517}
1618
17- impl From < async_nats:: jetstream:: Message > for JetStreamMessage {
18- fn from ( value : async_nats:: jetstream:: Message ) -> Self {
19+ impl TryFrom < async_nats:: jetstream:: Message > for JetStreamMessage {
20+ type Error = NatsrpyError ;
21+
22+ fn try_from ( value : async_nats:: jetstream:: Message ) -> Result < Self , Self :: Error > {
1923 let ( message, acker) = value. split ( ) ;
20- Self {
21- message,
22- headers : None ,
24+ Ok ( Self {
25+ message : message. try_into ( ) ?,
2326 acker : Arc :: new ( RwLock :: new ( acker) ) ,
24- }
27+ } )
2528 }
2629}
2730
@@ -47,27 +50,23 @@ impl JetStreamMessage {
4750#[ pyo3:: pymethods]
4851impl JetStreamMessage {
4952 #[ getter]
50- pub fn subject ( & self ) -> & str {
53+ #[ must_use]
54+ pub const fn subject ( & self ) -> & str {
5155 self . message . subject . as_str ( )
5256 }
5357 #[ getter]
54- pub fn reply ( & self ) -> Option < & str > {
55- self . message . reply . as_ref ( ) . map ( async_nats:: Subject :: as_str)
58+ #[ must_use]
59+ pub const fn reply ( & self ) -> & Option < String > {
60+ & self . message . reply
5661 }
5762 #[ getter]
58- pub fn payload ( & self ) -> & [ u8 ] {
63+ #[ must_use]
64+ pub const fn payload ( & self ) -> & Py < PyBytes > {
5965 & self . message . payload
6066 }
6167 #[ getter]
62- pub fn headers ( & mut self , py : Python < ' _ > ) -> NatsrpyResult < Py < PyDict > > {
63- if let Some ( headers) = & self . headers {
64- Ok ( headers. clone_ref ( py) )
65- } else {
66- let headermap = self . message . headers . clone ( ) . unwrap_or_default ( ) ;
67- let headers = headermap. to_pydict ( py) ?. unbind ( ) ;
68- self . headers = Some ( headers. clone_ref ( py) ) ;
69- Ok ( headers)
70- }
68+ pub const fn headers ( & mut self ) -> & Py < PyDict > {
69+ & self . message . headers
7170 }
7271
7372 #[ pyo3( signature=( double=false ) ) ]
@@ -107,4 +106,9 @@ impl JetStreamMessage {
107106 pub fn term < ' py > ( & self , py : Python < ' py > , double : bool ) -> NatsrpyResult < Bound < ' py , PyAny > > {
108107 self . inner_ack ( py, async_nats:: jetstream:: message:: AckKind :: Term , double)
109108 }
109+
110+ #[ must_use]
111+ pub fn __repr__ ( & self ) -> String {
112+ self . message . __repr__ ( )
113+ }
110114}
0 commit comments