Skip to content

Commit 6cf9414

Browse files
lu-zerododomorandi
andcommitted
Apply the impl Into<String>/owned Value pattern
Co-authored-by: Edoardo Morandi <morandidodo@gmail.com>
1 parent aca4ae7 commit 6cf9414

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

dht-cache/src/cache.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl Builder {
5757
std::fs::write(pk_path, pem)?;
5858
der
5959
}
60-
Err(e) => Err(e)?,
60+
Err(e) => return Err(e.into()),
6161
}
6262
} else {
6363
generate_rsa_key().1
@@ -99,7 +99,7 @@ impl Cache {
9999
/// Send a volatile message
100100
///
101101
/// Volatile messages are unstructured and do not persist in the DHT.
102-
pub fn send(&self, value: &Value) -> Result<(), Error> {
102+
pub fn send(&self, value: Value) -> Result<(), Error> {
103103
self.cmd
104104
.send(Command::Broadcast(value.to_owned()))
105105
.map_err(|_| Error::Channel)?;
@@ -110,11 +110,16 @@ impl Cache {
110110
/// Persist a value within the DHT
111111
///
112112
/// It is identified by the topic and uuid value
113-
pub async fn put(&self, topic: &str, uuid: &str, value: &Value) -> Result<(), Error> {
113+
pub async fn put(
114+
&self,
115+
topic: impl Into<String>,
116+
uuid: impl Into<String>,
117+
value: Value,
118+
) -> Result<(), Error> {
114119
let elem = DomoCacheElement {
115-
topic_name: topic.to_string(),
116-
topic_uuid: uuid.to_string(),
117-
value: value.to_owned(),
120+
topic_name: topic.into(),
121+
topic_uuid: uuid.into(),
122+
value,
118123
publication_timestamp: utils::get_epoch_ms(),
119124
publisher_peer_id: self.peer_id.clone(),
120125
..Default::default()
@@ -133,10 +138,14 @@ impl Cache {
133138
///
134139
/// It inserts the deletion entry and the entry value will be marked as deleted and removed
135140
/// from the stored cache.
136-
pub async fn del(&self, topic: &str, uuid: &str) -> Result<(), Error> {
141+
pub async fn del(
142+
&self,
143+
topic: impl Into<String>,
144+
uuid: impl Into<String>,
145+
) -> Result<(), Error> {
137146
let elem = DomoCacheElement {
138-
topic_name: topic.to_string(),
139-
topic_uuid: uuid.to_string(),
147+
topic_name: topic.into(),
148+
topic_uuid: uuid.into(),
140149
publication_timestamp: utils::get_epoch_ms(),
141150
publisher_peer_id: self.peer_id.clone(),
142151
deleted: true,
@@ -317,6 +326,10 @@ pub fn cache_channel(
317326
let mut elem = elem.to_owned();
318327
log::debug!("resending {}", elem.topic_uuid);
319328
elem.republication_timestamp = utils::get_epoch_ms();
329+
330+
// This cannot fail because `cmd` is the sender part of the
331+
// `stream` we are currently reading. In practice, we are
332+
// queueing the commands in order to read them later.
320333
cmd.send(Command::Publish(serde_json::to_value(&elem).unwrap()))
321334
.unwrap();
322335
});
@@ -411,7 +424,7 @@ mod test {
411424
.put(
412425
"Topic",
413426
&format!("uuid-{uuid}"),
414-
&serde_json::json!({"key": uuid}),
427+
serde_json::json!({"key": uuid}),
415428
)
416429
.await;
417430
}

0 commit comments

Comments
 (0)