[fractal] BKResponse, directory: remove error variants and compose over Result instead



commit 93431f5a27f9d356a5b9e343c87546cf459f1d16
Author: Alejandro Domínguez <adomu net-c com>
Date:   Sun Aug 25 23:14:12 2019 +0200

    BKResponse, directory: remove error variants and compose over Result instead

 fractal-gtk/src/app/backend_loop.rs         |  7 +++--
 fractal-matrix-api/src/backend/directory.rs | 46 ++++++++++-------------------
 fractal-matrix-api/src/backend/mod.rs       |  2 +-
 fractal-matrix-api/src/backend/types.rs     |  5 ++--
 fractal-matrix-api/src/util.rs              |  9 ++++++
 5 files changed, 32 insertions(+), 37 deletions(-)
---
diff --git a/fractal-gtk/src/app/backend_loop.rs b/fractal-gtk/src/app/backend_loop.rs
index d4f73196..73526692 100644
--- a/fractal-gtk/src/app/backend_loop.rs
+++ b/fractal-gtk/src/app/backend_loop.rs
@@ -126,10 +126,10 @@ pub fn backend_loop(rx: Receiver<BKResponse>) {
                     let initial = false;
                     APPOP!(sync, (initial));
                 }
-                Ok(BKResponse::DirectoryProtocols(protocols)) => {
+                Ok(BKResponse::DirectoryProtocols(Ok(protocols))) => {
                     APPOP!(set_protocols, (protocols));
                 }
-                Ok(BKResponse::DirectorySearch(rooms)) => {
+                Ok(BKResponse::DirectorySearch(Ok(rooms))) => {
                     APPOP!(append_directory_rooms, (rooms));
                 }
 
@@ -249,7 +249,8 @@ pub fn backend_loop(rx: Receiver<BKResponse>) {
                     let error = i18n("Error deleting message");
                     APPOP!(show_error, (error));
                 }
-                Ok(BKResponse::DirectoryError(_)) => {
+                Ok(BKResponse::DirectoryProtocols(Err(_)))
+                | Ok(BKResponse::DirectorySearch(Err(_))) => {
                     let error = i18n("Error searching for rooms");
                     APPOP!(reset_directory_state);
                     APPOP!(show_error, (error));
diff --git a/fractal-matrix-api/src/backend/directory.rs b/fractal-matrix-api/src/backend/directory.rs
index 9f990243..487026a6 100644
--- a/fractal-matrix-api/src/backend/directory.rs
+++ b/fractal-matrix-api/src/backend/directory.rs
@@ -40,23 +40,16 @@ pub fn protocols(bk: &Backend) {
                     .execute(request)?
                     .json::<SupportedProtocolsResponse>()
                     .map_err(Into::into)
-            });
-
-        match query {
-            Ok(response) => {
-                let protocols = response
+            })
+            .map(|response| {
+                response
                     .into_iter()
                     .flat_map(|(_, protocol)| protocol.instances.into_iter())
-                    .collect();
-
-                tx.send(BKResponse::DirectoryProtocols(protocols))
-                    .expect_log("Connection closed");
-            }
-            Err(err) => {
-                tx.send(BKResponse::DirectoryError(err))
-                    .expect_log("Connection closed");
-            }
-        }
+                    .collect()
+            });
+
+        tx.send(BKResponse::DirectoryProtocols(query))
+            .expect_log("Connection closed");
     });
 }
 
@@ -119,13 +112,11 @@ pub fn room_search(
                     .execute(request)?
                     .json::<PublicRoomsResponse>()
                     .map_err(Into::into)
-            });
-
-        match query {
-            Ok(response) => {
+            })
+            .map(|response| {
                 data.lock().unwrap().rooms_since = response.next_batch.unwrap_or_default();
 
-                let rooms = response
+                response
                     .chunk
                     .into_iter()
                     .map(Into::into)
@@ -137,16 +128,11 @@ pub fn room_search(
                             }
                         }
                     })
-                    .collect();
-
-                tx.send(BKResponse::DirectorySearch(rooms))
-                    .expect_log("Connection closed");
-            }
-            Err(err) => {
-                tx.send(BKResponse::DirectoryError(err))
-                    .expect_log("Connection closed");
-            }
-        }
+                    .collect()
+            });
+
+        tx.send(BKResponse::DirectorySearch(query))
+            .expect_log("Connection closed");
     });
 
     Ok(())
diff --git a/fractal-matrix-api/src/backend/mod.rs b/fractal-matrix-api/src/backend/mod.rs
index c2f493bb..4d178b22 100644
--- a/fractal-matrix-api/src/backend/mod.rs
+++ b/fractal-matrix-api/src/backend/mod.rs
@@ -286,7 +286,7 @@ impl Backend {
                 };
 
                 let r = directory::room_search(self, hs, q, tp, more);
-                bkerror!(r, tx, BKResponse::DirectoryError);
+                bkerror2!(r, tx, BKResponse::DirectorySearch);
             }
 
             // Stickers module
diff --git a/fractal-matrix-api/src/backend/types.rs b/fractal-matrix-api/src/backend/types.rs
index 4cf5434a..2fea1f52 100644
--- a/fractal-matrix-api/src/backend/types.rs
+++ b/fractal-matrix-api/src/backend/types.rs
@@ -117,8 +117,8 @@ pub enum BKResponse {
     RoomMembers(String, Vec<Member>),
     SentMsg(String, String),
     SentMsgRedaction(String, String),
-    DirectoryProtocols(Vec<ProtocolInstance>),
-    DirectorySearch(Vec<Room>),
+    DirectoryProtocols(Result<Vec<ProtocolInstance>, Error>),
+    DirectorySearch(Result<Vec<Room>, Error>),
     JoinRoom,
     LeaveRoom,
     MarkedAsRead(String, String),
@@ -162,7 +162,6 @@ pub enum BKResponse {
     SendTypingError(Error),
     SetRoomError(Error),
     CommandError(Error),
-    DirectoryError(Error),
     JoinRoomError(Error),
     MarkAsReadError(Error),
     LeaveRoomError(Error),
diff --git a/fractal-matrix-api/src/util.rs b/fractal-matrix-api/src/util.rs
index c013c942..0b798b91 100644
--- a/fractal-matrix-api/src/util.rs
+++ b/fractal-matrix-api/src/util.rs
@@ -104,6 +104,15 @@ macro_rules! bkerror {
     };
 }
 
+#[macro_export]
+macro_rules! bkerror2 {
+    ($result: expr, $tx: ident, $type: expr) => {
+        if let Err(e) = $result {
+            let _ = $tx.send($type(Err(e)));
+        }
+    };
+}
+
 #[macro_export]
 macro_rules! get {
     ($($args: expr),+) => {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]