From c017b87aea27b61b5b3eb98d2d6cf53ea3d116b6 Mon Sep 17 00:00:00 2001 From: Vika Date: Sun, 15 Aug 2021 15:10:01 +0300 Subject: Improved Redis module code quality --- src/database/redis/mod.rs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'src/database/redis') diff --git a/src/database/redis/mod.rs b/src/database/redis/mod.rs index 3a0d5fc..5b3506a 100644 --- a/src/database/redis/mod.rs +++ b/src/database/redis/mod.rs @@ -128,20 +128,20 @@ impl Storage for RedisStorage { async fn post_exists(&self, url: &str) -> Result { let mut conn = self.redis.get().await.map_err(|e| StorageError::with_source(ErrorKind::Backend, "Error getting a connection from the pool", Box::new(e)))?; - Ok(conn.hexists::<&str, &str, bool>(&"posts", url).await?) + Ok(conn.hexists::<&str, &str, bool>("posts", url).await?) } async fn get_post(&self, url: &str) -> Result> { let mut conn = self.redis.get().await.map_err(|e| StorageError::with_source(ErrorKind::Backend, "Error getting a connection from the pool", Box::new(e)))?; match conn - .hget::<&str, &str, Option>(&"posts", url) + .hget::<&str, &str, Option>("posts", url) .await? { Some(val) => { let parsed = serde_json::from_str::(&val)?; if let Some(new_url) = parsed["see_other"].as_str() { match conn - .hget::<&str, &str, Option>(&"posts", new_url) + .hget::<&str, &str, Option>("posts", new_url) .await? { Some(val) => Ok(Some(serde_json::from_str::(&val)?)), @@ -194,7 +194,7 @@ impl Storage for RedisStorage { )) } } - conn.hset::<&str, &str, String, ()>(&"posts", key, post.to_string()) + conn.hset::<&str, &str, String, ()>("posts", key, post.to_string()) .await?; if post["properties"]["url"].is_array() { for url in post["properties"]["url"] @@ -205,7 +205,7 @@ impl Storage for RedisStorage { { if url != key && url.starts_with(user) { conn.hset::<&str, &str, String, ()>( - &"posts", + "posts", &url, json!({ "see_other": key }).to_string(), ) @@ -239,7 +239,7 @@ impl Storage for RedisStorage { let mut conn = self.redis.get().await?; let mut feed; match conn - .hget::<&str, &str, Option>(&"posts", url) + .hget::<&str, &str, Option>("posts", url) .await .map_err(|e| StorageError::with_source(ErrorKind::Backend, "Error getting a connection from the pool", Box::new(e)))? { @@ -248,7 +248,7 @@ impl Storage for RedisStorage { } if feed["see_other"].is_string() { match conn - .hget::<&str, &str, Option>(&"posts", feed["see_other"].as_str().unwrap()) + .hget::<&str, &str, Option>("posts", feed["see_other"].as_str().unwrap()) .await? { Some(post) => feed = serde_json::from_str::(&post)?, @@ -285,10 +285,7 @@ impl Storage for RedisStorage { match conn.hget::<&str, &str, Option>("posts", &url).await { Ok(post) => match post { Some(post) => { - match serde_json::from_str::(&post) { - Ok(post) => Ok(Some(post)), - Err(err) => Err(StorageError::from(err)) - } + Ok(Some(serde_json::from_str(&post)?)) } // Happens because of a broken link (result of an improper deletion?) None => Ok(None), @@ -425,10 +422,10 @@ pub mod tests { } } - return RedisInstance { + RedisInstance { uri, child: redis_child, _tempdir: tempdir, - }; + } } } -- cgit 1.4.1