[fractal] Backend: Use Url type in BackendData.server_url



commit 09f504471c4d47a0abf71c2df9aed34f7195d8c0
Author: Alejandro Domínguez <adomu net-c com>
Date:   Sun Dec 30 00:25:04 2018 +0100

    Backend: Use Url type in BackendData.server_url

 fractal-matrix-api/src/backend/directory.rs |  8 ++++++--
 fractal-matrix-api/src/backend/mod.rs       |  6 +++---
 fractal-matrix-api/src/backend/register.rs  | 11 ++++-------
 fractal-matrix-api/src/backend/stickers.rs  |  3 +--
 fractal-matrix-api/src/backend/types.rs     |  2 +-
 5 files changed, 15 insertions(+), 15 deletions(-)
---
diff --git a/fractal-matrix-api/src/backend/directory.rs b/fractal-matrix-api/src/backend/directory.rs
index 6b09bc02..c0668b49 100644
--- a/fractal-matrix-api/src/backend/directory.rs
+++ b/fractal-matrix-api/src/backend/directory.rs
@@ -7,6 +7,7 @@ use crate::globals;
 use crate::backend::types::BKResponse;
 use crate::backend::types::Backend;
 use crate::error::Error;
+use std::str::Split;
 use std::thread;
 
 use crate::util::cache_path;
@@ -25,7 +26,6 @@ pub fn protocols(bk: &Backend) -> Result<(), Error> {
         .append_pair("access_token", &tk);
 
     let tx = bk.tx.clone();
-    let s = bk.data.lock().unwrap().server_url.clone();
     get!(
         &url,
         move |r: JsonValue| {
@@ -33,7 +33,11 @@ pub fn protocols(bk: &Backend) -> Result<(), Error> {
 
             protocols.push(Protocol {
                 id: String::new(),
-                desc: String::from(s.split('/').last().unwrap_or_default()),
+                desc: baseu
+                    .path_segments()
+                    .and_then(Split::last)
+                    .map(Into::into)
+                    .unwrap_or_default(),
             });
 
             if let Some(prs) = r.as_object() {
diff --git a/fractal-matrix-api/src/backend/mod.rs b/fractal-matrix-api/src/backend/mod.rs
index d46e16ea..acbffab3 100644
--- a/fractal-matrix-api/src/backend/mod.rs
+++ b/fractal-matrix-api/src/backend/mod.rs
@@ -34,7 +34,8 @@ impl Backend {
         let data = BackendData {
             user_id: String::from("Guest"),
             access_token: String::new(),
-            server_url: String::from("https://matrix.org";),
+            server_url: Url::parse("https://matrix.org";)
+                .expect("Wrong server_url value in BackendData"),
             scalar_token: None,
             scalar_url: String::from("https://scalar.vector.im";),
             sticker_widget: None,
@@ -53,8 +54,7 @@ impl Backend {
     }
 
     fn get_base_url(&self) -> Result<Url, Error> {
-        let s = self.data.lock().unwrap().server_url.clone();
-        let url = Url::parse(&s)?;
+        let url = self.data.lock().unwrap().server_url.clone();
         Ok(url)
     }
 
diff --git a/fractal-matrix-api/src/backend/register.rs b/fractal-matrix-api/src/backend/register.rs
index fa33cd86..b04e7a9c 100644
--- a/fractal-matrix-api/src/backend/register.rs
+++ b/fractal-matrix-api/src/backend/register.rs
@@ -15,7 +15,7 @@ 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 = String::from(server);
+    bk.data.lock().unwrap().server_url = Url::parse(server)?;
 
     let data = bk.data.clone();
     let tx = bk.tx.clone();
@@ -67,8 +67,7 @@ fn build_login_attrs(user: &str, password: &str) -> Result<JsonValue, Error> {
 }
 
 pub fn login(bk: &Backend, user: &str, password: &str, server: &str) -> Result<(), Error> {
-    let s = String::from(server);
-    bk.data.lock().unwrap().server_url = s;
+    bk.data.lock().unwrap().server_url = Url::parse(server)?;
     let url = bk.url("login", vec![])?;
 
     let attrs = build_login_attrs(user, password)?;
@@ -100,8 +99,7 @@ pub fn login(bk: &Backend, user: &str, password: &str, server: &str) -> Result<(
 }
 
 pub fn set_token(bk: &Backend, token: String, uid: String, server: &str) -> Result<(), Error> {
-    let s = String::from(server);
-    bk.data.lock().unwrap().server_url = s;
+    bk.data.lock().unwrap().server_url = Url::parse(server)?;
     bk.data.lock().unwrap().access_token = token.clone();
     bk.data.lock().unwrap().user_id = uid.clone();
     bk.data.lock().unwrap().since = None;
@@ -131,8 +129,7 @@ pub fn logout(bk: &Backend) -> Result<(), Error> {
 }
 
 pub fn register(bk: &Backend, user: &str, password: &str, server: &str) -> Result<(), Error> {
-    let s = String::from(server);
-    bk.data.lock().unwrap().server_url = s;
+    bk.data.lock().unwrap().server_url = Url::parse(server)?;
     let url = bk.url("register", vec![("kind", String::from("user"))])?;
 
     let attrs = json!({
diff --git a/fractal-matrix-api/src/backend/stickers.rs b/fractal-matrix-api/src/backend/stickers.rs
index 0771753c..d9781db7 100644
--- a/fractal-matrix-api/src/backend/stickers.rs
+++ b/fractal-matrix-api/src/backend/stickers.rs
@@ -166,8 +166,7 @@ pub fn purchase(bk: &Backend, group: &StickerGroup) -> Result<(), Error> {
 }
 
 fn get_base_url(data: &Arc<Mutex<BackendData>>) -> Result<Url, Error> {
-    let s = data.lock().unwrap().server_url.clone();
-    let url = Url::parse(&s)?;
+    let url = data.lock().unwrap().server_url.clone();
     Ok(url)
 }
 
diff --git a/fractal-matrix-api/src/backend/types.rs b/fractal-matrix-api/src/backend/types.rs
index a0c7b3eb..1a960380 100644
--- a/fractal-matrix-api/src/backend/types.rs
+++ b/fractal-matrix-api/src/backend/types.rs
@@ -186,7 +186,7 @@ pub enum RoomType {
 pub struct BackendData {
     pub user_id: String,
     pub access_token: String,
-    pub server_url: String,
+    pub server_url: Url,
     pub scalar_token: Option<String>,
     pub scalar_url: String,
     pub sticker_widget: Option<String>,


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