Skip to content

Commit 9cff7c7

Browse files
committed
Fixed clippy issues.
1 parent d768eed commit 9cff7c7

10 files changed

Lines changed: 166 additions & 133 deletions

File tree

src/js/jetstream.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct JetStream {
1919
}
2020

2121
impl JetStream {
22+
#[must_use]
2223
pub fn new(ctx: async_nats::jetstream::Context) -> Self {
2324
Self {
2425
ctx: Arc::new(RwLock::new(ctx)),
@@ -40,7 +41,7 @@ impl JetStream {
4041
&self,
4142
py: Python<'py>,
4243
subject: String,
43-
payload: Bound<PyBytes>,
44+
payload: &Bound<PyBytes>,
4445
headers: Option<Bound<PyDict>>,
4546
reply: Option<String>,
4647
err_on_disconnect: bool,
@@ -51,25 +52,28 @@ impl JetStream {
5152
.map(async_nats::HeaderMap::from_pydict)
5253
.transpose()?;
5354
natsrpy_future(py, async move {
54-
let js = ctx.read().await;
55-
if err_on_disconnect && js.client().connection_state() == State::Disconnected {
55+
if err_on_disconnect
56+
&& ctx.read().await.client().connection_state() == State::Disconnected
57+
{
5658
return Err(NatsrpyError::Disconnected);
5759
}
58-
js.publish_message(async_nats::message::OutboundMessage {
59-
subject: Subject::from(subject),
60-
payload: data,
61-
headers: headermap,
62-
reply: reply.map(Subject::from),
63-
})
64-
.await?;
60+
ctx.read()
61+
.await
62+
.publish_message(async_nats::message::OutboundMessage {
63+
subject: Subject::from(subject),
64+
payload: data,
65+
headers: headermap,
66+
reply: reply.map(Subject::from),
67+
})
68+
.await?;
6569
Ok(())
6670
})
6771
}
6872

6973
pub fn create_kv<'py>(
7074
&self,
7175
py: Python<'py>,
72-
config: Bound<'py, KVConfig>,
76+
config: &Bound<'py, KVConfig>,
7377
) -> NatsrpyResult<Bound<'py, PyAny>> {
7478
let ctx = self.ctx.clone();
7579
let config = config.borrow().deref().clone().try_into()?;
@@ -91,7 +95,7 @@ impl JetStream {
9195
pub fn update_kv<'py>(
9296
&self,
9397
py: Python<'py>,
94-
config: Bound<'py, KVConfig>,
98+
config: &Bound<'py, KVConfig>,
9599
) -> NatsrpyResult<Bound<'py, PyAny>> {
96100
let ctx = self.ctx.clone();
97101
let config = config.borrow().deref().clone().try_into()?;

src/js/kv.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ impl KVConfig {
5252
placement=None,
5353
limit_markers=None,
5454
))]
55-
pub fn __new__(
55+
#[must_use]
56+
pub const fn __new__(
5657
bucket: String,
5758
description: Option<String>,
5859
max_value_size: Option<i32>,
@@ -100,29 +101,32 @@ impl TryFrom<KVConfig> for async_nats::jetstream::kv::Config {
100101
history: value.history.unwrap_or_default(),
101102
max_age: value
102103
.max_age
103-
.map(|val| std::time::Duration::from_secs_f32(val))
104+
.map(std::time::Duration::from_secs_f32)
104105
.unwrap_or_default(),
105106
max_bytes: value.max_bytes.unwrap_or_default(),
106107
storage: value.storage.unwrap_or_default().into(),
107108
num_replicas: value.num_replicas.unwrap_or_default(),
108-
republish: value.republish.map(|r| r.into()),
109-
mirror: value.mirror.map(|m| m.try_into()).transpose()?,
109+
republish: value.republish.map(std::convert::Into::into),
110+
mirror: value
111+
.mirror
112+
.map(std::convert::TryInto::try_into)
113+
.transpose()?,
110114
sources: value
111115
.sources
112116
.map(|srcs| {
113117
// Collect the results of trying to convert each source, and if any conversion
114118
// fails, return the error
115119
srcs.into_iter()
116-
.map(|s| s.try_into())
120+
.map(std::convert::TryInto::try_into)
117121
.collect::<Result<Vec<_>, _>>()
118122
})
119123
// Now it's a Option<Result<_>>,
120124
// we transpose it to Result<Option<_>>
121125
.transpose()?,
122126
mirror_direct: value.mirror_direct.unwrap_or_default(),
123127
compression: value.compression.unwrap_or_default(),
124-
placement: value.placement.map(|p| p.into()),
125-
limit_markers: value.limit_markers.map(|val| Duration::from_secs_f32(val)),
128+
placement: value.placement.map(std::convert::Into::into),
129+
limit_markers: value.limit_markers.map(Duration::from_secs_f32),
126130
})
127131
}
128132
}
@@ -133,6 +137,7 @@ pub struct KeyValue {
133137
}
134138

135139
impl KeyValue {
140+
#[must_use]
136141
pub fn new(store: async_nats::jetstream::kv::Store) -> Self {
137142
Self {
138143
store: Arc::new(RwLock::new(store)),
@@ -145,27 +150,25 @@ impl KeyValue {
145150
pub fn get<'py>(&self, py: Python<'py>, key: String) -> NatsrpyResult<Bound<'py, PyAny>> {
146151
let store = self.store.clone();
147152
natsrpy_future(py, async move {
148-
let kv = store.read().await;
149-
if let Some(data) = kv.get(key).await? {
150-
let pybytes = Python::attach(move |gil| PyBytes::new(gil, &data).unbind());
151-
Ok(Some(pybytes))
152-
} else {
153-
Ok(None)
154-
}
153+
Ok(store
154+
.read()
155+
.await
156+
.get(key)
157+
.await?
158+
.map(|data| Python::attach(move |gil| PyBytes::new(gil, &data).unbind())))
155159
})
156160
}
157161

158162
pub fn put<'py>(
159163
&self,
160164
py: Python<'py>,
161165
key: String,
162-
value: Bound<'py, PyBytes>,
166+
value: &Bound<'py, PyBytes>,
163167
) -> NatsrpyResult<Bound<'py, PyAny>> {
164168
let store = self.store.clone();
165169
let data = bytes::Bytes::copy_from_slice(value.as_bytes());
166170
natsrpy_future(py, async move {
167-
let kv = store.read().await;
168-
let status = kv.put(key, data).await?;
171+
let status = store.read().await.put(key, data).await?;
169172
Ok(status)
170173
})
171174
}

src/js/stream.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl From<Republish> for async_nats::jetstream::stream::Republish {
3636
Self {
3737
source: value.source.clone(),
3838
destination: value.destination.clone(),
39-
headers_only: value.headers_only.clone(),
39+
headers_only: value.headers_only,
4040
}
4141
}
4242
}
@@ -52,10 +52,11 @@ pub struct External {
5252
impl External {
5353
#[new]
5454
#[pyo3(signature = (api_prefix, delivery_prefix=None))]
55-
pub fn __new__(api_prefix: String, delivery_prefix: Option<String>) -> Self {
55+
#[must_use]
56+
pub const fn __new__(api_prefix: String, delivery_prefix: Option<String>) -> Self {
5657
Self {
57-
api_prefix: api_prefix,
58-
delivery_prefix: delivery_prefix,
58+
api_prefix,
59+
delivery_prefix,
5960
}
6061
}
6162
}
@@ -104,18 +105,18 @@ impl TryFrom<Source> for async_nats::jetstream::stream::Source {
104105
Ok(Self {
105106
name: value.name.clone(),
106107
filter_subject: value.filter_subject.clone(),
107-
external: value.external.as_ref().map(|e| e.into()),
108+
external: value.external.as_ref().map(std::convert::Into::into),
108109
start_sequence: value.start_sequence,
109110
start_time: value
110111
.start_time
111-
.map(|val| time::OffsetDateTime::from_unix_timestamp(val))
112+
.map(time::OffsetDateTime::from_unix_timestamp)
112113
.transpose()?,
113114
domain: value.domain.clone(),
114115
subject_transforms: value
115116
.subject_transforms
116-
.clone()
117+
117118
.into_iter()
118-
.map(|val| val.into())
119+
.map(std::convert::Into::into)
119120
.collect(),
120121
})
121122
}
@@ -168,6 +169,7 @@ pub struct Placement {
168169
impl Placement {
169170
#[new]
170171
#[pyo3(signature=(cluster=None, tags=None))]
172+
#[must_use]
171173
pub fn __new__(cluster: Option<String>, tags: Option<Vec<String>>) -> Self {
172174
Self {
173175
cluster,

src/lib.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
1+
#![warn(
2+
// Base lints.
3+
clippy::all,
4+
// Some pedantic lints.
5+
clippy::pedantic,
6+
// New lints which are cool.
7+
clippy::nursery,
8+
)]
9+
#![
10+
allow(
11+
// I don't care about this.
12+
clippy::module_name_repetitions,
13+
// Yo, the hell you should put
14+
// it in docs, if signature is clear as sky.
15+
clippy::missing_errors_doc,
16+
// Because pythonic way is
17+
// to have many args with defaults.
18+
clippy::too_many_arguments
19+
)]
120
pub mod exceptions;
221
pub mod js;
22+
pub mod message;
323
pub mod nats_cls;
424
pub mod subscription;
525
pub mod utils;
6-
pub mod message;
726

827
#[pyo3::pymodule]
928
pub mod _inner {
1029
use pyo3::{Bound, PyResult, types::PyModule};
1130

1231
use crate::utils::py::PyModuleSubmoduleExt;
1332

33+
#[pymodule_export]
34+
use super::message::Message;
1435
#[pymodule_export]
1536
use super::nats_cls::NatsCls;
1637
#[pymodule_export]
1738
use super::subscription::Subscription;
18-
#[pymodule_export]
19-
use super::message::Message;
2039

2140
#[pymodule_export]
2241
use super::js::pymod as js;

src/message.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use pyo3::{
66
use crate::exceptions::rust_err::NatsrpyResult;
77

88
#[pyo3::pyclass(get_all, set_all)]
9+
#[derive(Debug)]
910
pub struct Message {
1011
pub subject: String,
1112
pub reply: Option<String>,
@@ -17,23 +18,22 @@ pub struct Message {
1718
}
1819

1920
impl Message {
20-
pub fn from_nats_message<'py>(
21-
py: Python<'py>,
21+
pub fn from_nats_message(
22+
py: Python<'_>,
2223
message: async_nats::Message,
2324
) -> NatsrpyResult<Self> {
2425
let headers = PyDict::new(py);
2526
if let Some(headermap) = message.headers {
2627
for (header_name, header_val) in headermap.iter() {
2728
let py_val = header_val
2829
.iter()
29-
.map(|val| val.to_string())
30+
.map(std::string::ToString::to_string)
3031
.collect::<Vec<_>>();
3132
if py_val.len() == 1 {
3233
headers.set_item(header_name.to_string(), py_val.first())?;
3334
continue;
34-
} else {
35-
headers.set_item(header_name.to_string(), py_val)?;
3635
}
36+
headers.set_item(header_name.to_string(), py_val)?;
3737
}
3838
}
3939
Ok(Self {
@@ -50,20 +50,15 @@ impl Message {
5050

5151
#[pyo3::pymethods]
5252
impl Message {
53+
#[must_use]
5354
pub fn __repr__(&self) -> String {
54-
self.to_string()
55-
}
56-
}
57-
58-
impl ToString for Message {
59-
fn to_string(&self) -> String {
6055
format!(
61-
r#"Message<subject="{subject}", reply={reply}, payload={payload}, headers={headers}, description={description}, length={len}>"#,
56+
r#"Message<subject="{subject}", reply={reply:?}, payload={payload}, headers={headers}, description={description:?}, length={len}>"#,
6257
subject = self.subject,
63-
reply = format!("{:?}", self.reply),
64-
payload = self.payload.to_string(),
65-
headers = self.headers.to_string(),
66-
description = format!("{:?}", self.description),
58+
reply = self.reply,
59+
payload = self.payload,
60+
headers = self.headers,
61+
description = self.description,
6762
len = self.length,
6863
)
6964
}

0 commit comments

Comments
 (0)