[fractal] Return Url directly when a Err() isn't possible



commit a78e678f325dba2c0e2d12ce7356c39b48ae31f7
Author: Alejandro Domínguez <adomu net-c com>
Date:   Sun Dec 30 00:56:31 2018 +0100

    Return Url directly when a Err() isn't possible
    
    Some functions never return Err, so to make it more clear and use simpler types
    we changed them to return just the plain type.

 fractal-matrix-api/src/backend/directory.rs | 12 ++++++------
 fractal-matrix-api/src/backend/media.rs     | 10 +++++-----
 fractal-matrix-api/src/backend/mod.rs       | 13 +++++--------
 fractal-matrix-api/src/backend/register.rs  |  9 +++++----
 fractal-matrix-api/src/backend/room.rs      | 10 +++++-----
 fractal-matrix-api/src/backend/stickers.rs  | 11 ++++++-----
 fractal-matrix-api/src/backend/sync.rs      |  2 +-
 fractal-matrix-api/src/backend/user.rs      | 18 +++++++++---------
 8 files changed, 42 insertions(+), 43 deletions(-)
---
diff --git a/fractal-matrix-api/src/backend/directory.rs b/fractal-matrix-api/src/backend/directory.rs
index c0668b49..1b535b6c 100644
--- a/fractal-matrix-api/src/backend/directory.rs
+++ b/fractal-matrix-api/src/backend/directory.rs
@@ -17,10 +17,12 @@ use crate::util::media;
 use crate::types::Protocol;
 use crate::types::Room;
 
-pub fn protocols(bk: &Backend) -> Result<(), Error> {
-    let baseu = bk.get_base_url()?;
+pub fn protocols(bk: &Backend) {
+    let baseu = bk.get_base_url();
     let tk = bk.data.lock().unwrap().access_token.clone();
-    let mut url = baseu.join("/_matrix/client/unstable/thirdparty/protocols")?;
+    let mut url = baseu
+        .join("/_matrix/client/unstable/thirdparty/protocols")
+        .expect("Wrong URL in protocols()");
     url.query_pairs_mut()
         .clear()
         .append_pair("access_token", &tk);
@@ -59,8 +61,6 @@ pub fn protocols(bk: &Backend) -> Result<(), Error> {
             tx.send(BKResponse::DirectoryError(err)).unwrap();
         }
     );
-
-    Ok(())
 }
 
 pub fn room_search(
@@ -82,7 +82,7 @@ pub fn room_search(
     }
 
     let url = bk.url("publicRooms", params)?;
-    let base = bk.get_base_url()?;
+    let base = bk.get_base_url();
 
     let mut attrs = json!({ "limit": globals::ROOM_DIRECTORY_LIMIT });
 
diff --git a/fractal-matrix-api/src/backend/media.rs b/fractal-matrix-api/src/backend/media.rs
index 1f0050c5..8bd0ce59 100644
--- a/fractal-matrix-api/src/backend/media.rs
+++ b/fractal-matrix-api/src/backend/media.rs
@@ -16,7 +16,7 @@ use crate::util::thumb;
 use crate::types::Message;
 
 pub fn get_thumb_async(bk: &Backend, media: String, tx: Sender<String>) -> Result<(), Error> {
-    let baseu = bk.get_base_url()?;
+    let baseu = bk.get_base_url();
 
     semaphore(bk.limit_threads.clone(), move || {
         match thumb(&baseu, &media, None) {
@@ -33,7 +33,7 @@ pub fn get_thumb_async(bk: &Backend, media: String, tx: Sender<String>) -> Resul
 }
 
 pub fn get_media_async(bk: &Backend, media: String, tx: Sender<String>) -> Result<(), Error> {
-    let baseu = bk.get_base_url()?;
+    let baseu = bk.get_base_url();
 
     semaphore(bk.limit_threads.clone(), move || {
         match util::media(&baseu, &media, None) {
@@ -56,7 +56,7 @@ pub fn get_media_list_async(
     prev_batch: Option<String>,
     tx: Sender<(Vec<Message>, String)>,
 ) -> Result<(), Error> {
-    let baseu = bk.get_base_url()?;
+    let baseu = bk.get_base_url();
     let tk = bk.data.lock().unwrap().access_token.clone();
     let room = String::from(roomid);
 
@@ -80,7 +80,7 @@ pub fn get_media_list_async(
 }
 
 pub fn get_media(bk: &Backend, media: String) -> Result<(), Error> {
-    let baseu = bk.get_base_url()?;
+    let baseu = bk.get_base_url();
 
     let tx = bk.tx.clone();
     thread::spawn(move || {
@@ -98,7 +98,7 @@ pub fn get_media(bk: &Backend, media: String) -> Result<(), Error> {
 }
 
 pub fn get_media_url(bk: &Backend, media: String, tx: Sender<String>) -> Result<(), Error> {
-    let baseu = bk.get_base_url()?;
+    let baseu = bk.get_base_url();
 
     semaphore(bk.limit_threads.clone(), move || {
         match resolve_media_url(&baseu, &media, false, 0, 0) {
diff --git a/fractal-matrix-api/src/backend/mod.rs b/fractal-matrix-api/src/backend/mod.rs
index 326f329a..7153dfc6 100644
--- a/fractal-matrix-api/src/backend/mod.rs
+++ b/fractal-matrix-api/src/backend/mod.rs
@@ -54,13 +54,12 @@ impl Backend {
         }
     }
 
-    fn get_base_url(&self) -> Result<Url, Error> {
-        let url = self.data.lock().unwrap().server_url.clone();
-        Ok(url)
+    fn get_base_url(&self) -> Url {
+        self.data.lock().unwrap().server_url.clone()
     }
 
     fn url(&self, path: &str, mut params: Vec<(&str, String)>) -> Result<Url, Error> {
-        let base = self.get_base_url()?;
+        let base = self.get_base_url();
         let tk = self.data.lock().unwrap().access_token.clone();
 
         params.push(("access_token", tk));
@@ -138,8 +137,7 @@ impl Backend {
                 bkerror!(r, tx, BKResponse::AddThreePIDError);
             }
             Ok(BKCommand::DeleteThreePID(medium, address)) => {
-                let r = user::delete_three_pid(self, &medium, &address);
-                bkerror!(r, tx, BKResponse::DeleteThreePIDError);
+                user::delete_three_pid(self, &medium, &address);
             }
             Ok(BKCommand::ChangePassword(username, old_password, new_password)) => {
                 let r = user::change_password(self, &username, &old_password, &new_password);
@@ -302,8 +300,7 @@ impl Backend {
 
             // Directory module
             Ok(BKCommand::DirectoryProtocols) => {
-                let r = directory::protocols(self);
-                bkerror!(r, tx, BKResponse::DirectoryError);
+                directory::protocols(self);
             }
             Ok(BKCommand::DirectorySearch(dhs, dq, dtp, more)) => {
                 let hs = match dhs {
diff --git a/fractal-matrix-api/src/backend/register.rs b/fractal-matrix-api/src/backend/register.rs
index b04e7a9c..3c5c3a1b 100644
--- a/fractal-matrix-api/src/backend/register.rs
+++ b/fractal-matrix-api/src/backend/register.rs
@@ -12,10 +12,11 @@ use crate::backend::types::BKResponse;
 use crate::backend::types::Backend;
 
 pub fn guest(bk: &Backend, server: &str) -> Result<(), Error> {
-    let url = Url::parse(server)
-        .unwrap()
-        .join("/_matrix/client/r0/register?kind=guest")?;
-    bk.data.lock().unwrap().server_url = Url::parse(server)?;
+    let baseu = Url::parse(server)?;
+    let url = baseu
+        .join("/_matrix/client/r0/register?kind=guest")
+        .expect("Wrong URL in guest()");
+    bk.data.lock().unwrap().server_url = baseu;
 
     let data = bk.data.clone();
     let tx = bk.tx.clone();
diff --git a/fractal-matrix-api/src/backend/room.rs b/fractal-matrix-api/src/backend/room.rs
index c6164f69..a2870e79 100644
--- a/fractal-matrix-api/src/backend/room.rs
+++ b/fractal-matrix-api/src/backend/room.rs
@@ -58,7 +58,7 @@ pub fn get_room_detail(bk: &Backend, roomid: String, key: String) -> Result<(),
 
 pub fn get_room_avatar(bk: &Backend, roomid: String) -> Result<(), Error> {
     let userid = bk.data.lock().unwrap().user_id.clone();
-    let baseu = bk.get_base_url()?;
+    let baseu = bk.get_base_url();
     let tk = bk.data.lock().unwrap().access_token.clone();
     let url = bk.url(&format!("rooms/{}/state/m.room.avatar", roomid), vec![])?;
 
@@ -153,7 +153,7 @@ pub fn get_room_messages(bk: &Backend, roomid: String, from: String) -> Result<(
 pub fn get_room_messages_from_msg(bk: &Backend, roomid: String, msg: Message) -> Result<(), Error> {
     // first of all, we calculate the from param using the context api, then we call the
     // normal get_room_messages
-    let baseu = bk.get_base_url()?;
+    let baseu = bk.get_base_url();
     let tk = bk.data.lock().unwrap().access_token.clone();
     let id = msg.id.unwrap_or_default();
     let tx = bk.internal_tx.clone();
@@ -226,7 +226,7 @@ fn parse_context(
 
 pub fn get_message_context(bk: &Backend, msg: Message) -> Result<(), Error> {
     let tx = bk.tx.clone();
-    let baseu = bk.get_base_url()?;
+    let baseu = bk.get_base_url();
     let roomid = msg.room.clone();
     let msgid = msg.id.unwrap_or_default();
     let tk = bk.data.lock().unwrap().access_token.clone();
@@ -436,7 +436,7 @@ pub fn set_room_topic(bk: &Backend, roomid: &str, topic: &str) -> Result<(), Err
 }
 
 pub fn set_room_avatar(bk: &Backend, roomid: &str, avatar: &str) -> Result<(), Error> {
-    let baseu = bk.get_base_url()?;
+    let baseu = bk.get_base_url();
     let tk = bk.data.lock().unwrap().access_token.clone();
     let params = &[("access_token", tk.clone())];
     let mediaurl = media_url(&baseu, "upload", params)?;
@@ -480,7 +480,7 @@ pub fn attach_file(bk: &Backend, msg: Message) -> Result<(), Error> {
     let mut contents: Vec<u8> = vec![];
     file.read_to_end(&mut contents)?;
 
-    let baseu = bk.get_base_url()?;
+    let baseu = bk.get_base_url();
     let tk = bk.data.lock().unwrap().access_token.clone();
     let params = &[("access_token", tk.clone())];
     let mediaurl = media_url(&baseu, "upload", params)?;
diff --git a/fractal-matrix-api/src/backend/stickers.rs b/fractal-matrix-api/src/backend/stickers.rs
index 67dc71df..30331f3b 100644
--- a/fractal-matrix-api/src/backend/stickers.rs
+++ b/fractal-matrix-api/src/backend/stickers.rs
@@ -165,9 +165,8 @@ pub fn purchase(bk: &Backend, group: &StickerGroup) -> Result<(), Error> {
     Ok(())
 }
 
-fn get_base_url(data: &Arc<Mutex<BackendData>>) -> Result<Url, Error> {
-    let url = data.lock().unwrap().server_url.clone();
-    Ok(url)
+fn get_base_url(data: &Arc<Mutex<BackendData>>) -> Url {
+    data.lock().unwrap().server_url.clone()
 }
 
 fn url(
@@ -175,7 +174,7 @@ fn url(
     path: &str,
     mut params: Vec<(&str, String)>,
 ) -> Result<Url, Error> {
-    let base = get_base_url(data)?;
+    let base = get_base_url(data);
     let tk = data.lock().unwrap().access_token.clone();
 
     params.push(("access_token", tk.clone()));
@@ -190,7 +189,9 @@ fn get_scalar_token(data: &Arc<Mutex<BackendData>>) -> Result<String, Error> {
     let url = url(data, &format!("user/{}/openid/request_token", uid), vec![])?;
     let js = json_q("post", &url, &json!({}), globals::TIMEOUT)?;
 
-    let vurl = base.join("/api/register")?;
+    let vurl = base
+        .join("/api/register")
+        .expect("Wrong URL in get_scalar_token()");
     let js = json_q("post", &vurl, &js, globals::TIMEOUT)?;
 
     match js["scalar_token"].as_str() {
diff --git a/fractal-matrix-api/src/backend/sync.rs b/fractal-matrix-api/src/backend/sync.rs
index 5c01492c..d19284a5 100644
--- a/fractal-matrix-api/src/backend/sync.rs
+++ b/fractal-matrix-api/src/backend/sync.rs
@@ -58,7 +58,7 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) -> Result<()
         timeout = 0;
     }
 
-    let baseu = bk.get_base_url()?;
+    let baseu = bk.get_base_url();
     let url = bk.url("sync", params)?;
 
     let tx = bk.tx.clone();
diff --git a/fractal-matrix-api/src/backend/user.rs b/fractal-matrix-api/src/backend/user.rs
index 768bb0c4..8471b707 100644
--- a/fractal-matrix-api/src/backend/user.rs
+++ b/fractal-matrix-api/src/backend/user.rs
@@ -252,10 +252,12 @@ pub fn submit_phone_token(
     Ok(())
 }
 
-pub fn delete_three_pid(bk: &Backend, medium: &str, address: &str) -> Result<(), Error> {
-    let baseu = bk.get_base_url()?;
+pub fn delete_three_pid(bk: &Backend, medium: &str, address: &str) {
+    let baseu = bk.get_base_url();
     let tk = bk.data.lock().unwrap().access_token.clone();
-    let mut url = baseu.join("/_matrix/client/unstable/account/3pid/delete")?;
+    let mut url = baseu
+        .join("/_matrix/client/unstable/account/3pid/delete")
+        .expect("Wrong URL in delete_three_pid()");
     url.query_pairs_mut()
         .clear()
         .append_pair("access_token", &tk);
@@ -275,8 +277,6 @@ pub fn delete_three_pid(bk: &Backend, medium: &str, address: &str) -> Result<(),
             tx.send(BKResponse::DeleteThreePIDError(err)).unwrap();
         }
     );
-
-    Ok(())
 }
 
 pub fn change_password(
@@ -346,7 +346,7 @@ pub fn account_destruction(
 }
 
 pub fn get_avatar(bk: &Backend) -> Result<(), Error> {
-    let baseu = bk.get_base_url()?;
+    let baseu = bk.get_base_url();
     let userid = bk.data.lock().unwrap().user_id.clone();
 
     let tx = bk.tx.clone();
@@ -367,7 +367,7 @@ pub fn get_user_info_async(
     uid: &str,
     tx: Option<Sender<(String, String)>>,
 ) -> Result<(), Error> {
-    let baseu = bk.get_base_url()?;
+    let baseu = bk.get_base_url();
 
     let u = String::from(uid);
 
@@ -429,7 +429,7 @@ pub fn get_avatar_async(
     member: Option<Member>,
     tx: Sender<String>,
 ) -> Result<(), Error> {
-    let baseu = bk.get_base_url()?;
+    let baseu = bk.get_base_url();
 
     if member.is_none() {
         tx.send(String::new()).unwrap();
@@ -458,7 +458,7 @@ pub fn get_avatar_async(
 }
 
 pub fn set_user_avatar(bk: &Backend, avatar: String) -> Result<(), Error> {
-    let baseu = bk.get_base_url()?;
+    let baseu = bk.get_base_url();
     let id = bk.data.lock().unwrap().user_id.clone();
     let tk = bk.data.lock().unwrap().access_token.clone();
     let params = &[("access_token", tk.clone())];


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