From f726a42f6190ee3f0438a83823b89fa038eb8301 Mon Sep 17 00:00:00 2001 From: Vika Date: Mon, 21 Feb 2022 20:51:00 +0300 Subject: database: code cleanup --- src/database/mod.rs | 49 ++++++++++++++----------------------------------- 1 file changed, 14 insertions(+), 35 deletions(-) (limited to 'src/database/mod.rs') diff --git a/src/database/mod.rs b/src/database/mod.rs index 55ab027..6fdb9b1 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -42,25 +42,6 @@ pub struct StorageError { kind: ErrorKind, } -/*impl From for tide::Response { - fn from(err: StorageError) -> Self { - tide::Response::builder(match err.kind() { - ErrorKind::BadRequest => 400, - ErrorKind::NotFound => 404, - _ => 500, - }) - .body(serde_json::json!({ - "error": match err.kind() { - ErrorKind::BadRequest => "invalid_request", - ErrorKind::NotFound => "not_found", - _ => "database_error" - }, - "error_description": err - })) - .build() - } -}*/ - impl std::error::Error for StorageError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { self.source @@ -198,7 +179,7 @@ pub fn filter_post( /// Implementations should note that all methods listed on this trait MUST be fully atomic /// or lock the database so that write conflicts or reading half-written data should not occur. #[async_trait] -pub trait Storage: Clone + Send + Sync { +pub trait Storage: std::fmt::Debug + Clone + Send + Sync { /// Check if a post exists in the database. async fn post_exists(&self, url: &str) -> Result; @@ -208,7 +189,7 @@ pub trait Storage: Clone + Send + Sync { /// Save a post to the database as an MF2-JSON structure. /// /// Note that the `post` object MUST have `post["properties"]["uid"][0]` defined. - async fn put_post<'a>(&self, post: &'a serde_json::Value, user: &'a str) -> Result<()>; + async fn put_post(&self, post: &'_ serde_json::Value, user: &'_ str) -> Result<()>; /// Modify a post using an update object as defined in the Micropub spec. /// @@ -217,10 +198,10 @@ pub trait Storage: Clone + Send + Sync { /// /// You can assume concurrent updates will never contradict each other, since that will be dumb. /// The last update always wins. - async fn update_post<'a>(&self, url: &'a str, update: serde_json::Value) -> Result<()>; + async fn update_post(&self, url: &'_ str, update: serde_json::Value) -> Result<()>; /// Get a list of channels available for the user represented by the URL `user` to write to. - async fn get_channels<'a>(&self, user: &'a str) -> Result>; + async fn get_channels(&self, user: &'_ str) -> Result>; /// Fetch a feed at `url` and return a an h-feed object containing /// `limit` posts after a post by url `after`, filtering the content @@ -239,28 +220,26 @@ pub trait Storage: Clone + Send + Sync { /// from the database, preferably make this method use a connection pool /// to reduce overhead of creating a database connection per post for /// parallel fetching. - async fn read_feed_with_limit<'a>( + async fn read_feed_with_limit( &self, - url: &'a str, - after: &'a Option, + url: &'_ str, + after: &'_ Option, limit: usize, - user: &'a Option, + user: &'_ Option, ) -> Result>; /// Deletes a post from the database irreversibly. 'nuff said. Must be idempotent. - async fn delete_post<'a>(&self, url: &'a str) -> Result<()>; + async fn delete_post(&self, url: &'_ str) -> Result<()>; /// Gets a setting from the setting store and passes the result. - async fn get_setting<'a>(&self, setting: &'a str, user: &'a str) -> Result; + async fn get_setting(&self, setting: &'_ str, user: &'_ str) -> Result; /// Commits a setting to the setting store. - async fn set_setting<'a>(&self, setting: &'a str, user: &'a str, value: &'a str) -> Result<()>; + async fn set_setting(&self, setting: &'_ str, user: &'_ str, value: &'_ str) -> Result<()>; } #[cfg(test)] mod tests { - //#[cfg(feature="redis")] - //use super::redis::tests::get_redis_instance; use super::{MicropubChannel, Storage}; use paste::paste; use serde_json::json; @@ -425,10 +404,10 @@ mod tests { ); } - /*macro_rules! file_test { + macro_rules! file_test { ($func_name:expr) => { paste! { - #[async_std::test] + #[tokio::test] async fn [] () { test_logger::ensure_env_logger_initialized(); let tempdir = tempdir::TempDir::new("file").expect("Failed to create tempdir"); @@ -443,5 +422,5 @@ mod tests { file_test!(test_backend_get_channel_list); file_test!(test_backend_settings); file_test!(test_backend_update); - */ + } -- cgit 1.4.1