Skip to content

Commit 147164a

Browse files
committed
Small code cleanup.
1 parent c8d6b4d commit 147164a

2 files changed

Lines changed: 29 additions & 22 deletions

File tree

src/message.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2020
impl 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]
5239
impl 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}>"#,

src/utils/headers.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use pyo3::{
2-
Bound,
2+
Bound, Py, Python,
33
types::{PyAnyMethods, PyDict},
44
};
55

66
use crate::exceptions::rust_err::NatsrpyResult;
77

88
pub trait NatsrpyHeadermapExt: Sized {
99
fn from_pydict(pydict: Bound<PyDict>) -> NatsrpyResult<Self>;
10+
fn to_pydict(&self, py: Python) -> NatsrpyResult<Py<PyDict>>;
1011
}
1112

1213
impl NatsrpyHeadermapExt for async_nats::HeaderMap {
@@ -28,4 +29,23 @@ impl NatsrpyHeadermapExt for async_nats::HeaderMap {
2829
}
2930
Ok(headermap)
3031
}
32+
33+
fn to_pydict(&self, py: Python) -> NatsrpyResult<Py<PyDict>> {
34+
let dict = PyDict::new(py);
35+
for (header_name, header_val) in self.iter() {
36+
let py_val = header_val
37+
.iter()
38+
.map(std::string::ToString::to_string)
39+
.collect::<Vec<_>>();
40+
if py_val.is_empty() {
41+
continue;
42+
}
43+
if py_val.len() == 1 {
44+
dict.set_item(header_name.to_string(), py_val.first())?;
45+
continue;
46+
}
47+
dict.set_item(header_name.to_string(), py_val)?;
48+
}
49+
Ok(dict.unbind())
50+
}
3151
}

0 commit comments

Comments
 (0)