[fractal] Backend: implement Deserialize for protocol types



commit ce6582d623ffb895c056c6c562cf65f231c97091
Author: Alejandro Domínguez <adomu net-c com>
Date:   Sun Jan 13 04:47:41 2019 +0100

    Backend: implement Deserialize for protocol types

 fractal-matrix-api/src/backend/directory.rs | 39 +++++++----------------------
 fractal-matrix-api/src/model/protocol.rs    |  6 +++--
 fractal-matrix-api/src/types.rs             |  1 +
 3 files changed, 14 insertions(+), 32 deletions(-)
---
diff --git a/fractal-matrix-api/src/backend/directory.rs b/fractal-matrix-api/src/backend/directory.rs
index 5a7d25d7..da04250e 100644
--- a/fractal-matrix-api/src/backend/directory.rs
+++ b/fractal-matrix-api/src/backend/directory.rs
@@ -7,15 +7,14 @@ 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;
 use crate::util::json_q;
 use crate::util::media;
 
-use crate::types::ProtocolInstance;
 use crate::types::{Room, RoomMembership};
+use crate::types::SupportedProtocols;
 
 pub fn protocols(bk: &Backend) {
     let baseu = bk.get_base_url();
@@ -31,34 +30,14 @@ pub fn protocols(bk: &Backend) {
     get!(
         &url,
         move |r: JsonValue| {
-            let mut protocols = vec![ProtocolInstance {
-                id: Default::default(),
-                desc: baseu
-                    .path_segments()
-                    .and_then(Split::last)
-                    .map(Into::into)
-                    .unwrap_or_default(),
-                icon: Default::default(),
-                fields: Default::default(),
-            }];
-
-            if let Some(prs) = r.as_object() {
-                for k in prs.keys() {
-                    let ins = prs[k]["instances"].as_array();
-                    for i in ins.unwrap_or(&vec![]) {
-                        let p = ProtocolInstance {
-                            id: i["instance_id"]
-                                .as_str()
-                                .map(Into::into)
-                                .unwrap_or_default(),
-                            desc: i["desc"].as_str().map(Into::into).unwrap_or_default(),
-                            icon: i["icon"].as_str().map(Into::into),
-                            fields: i["fields"].clone(),
-                        };
-                        protocols.push(p);
-                    }
-                }
-            }
+            let protocols = serde_json::from_value(r)
+                .map(|protocols: SupportedProtocols| {
+                    protocols
+                        .into_iter()
+                        .flat_map(|(_, protocol)| protocol.instances.into_iter())
+                        .collect()
+                })
+                .unwrap_or_default();
 
             tx.send(BKResponse::DirectoryProtocols(protocols)).unwrap();
         },
diff --git a/fractal-matrix-api/src/model/protocol.rs b/fractal-matrix-api/src/model/protocol.rs
index 12be8c29..040ed1d8 100644
--- a/fractal-matrix-api/src/model/protocol.rs
+++ b/fractal-matrix-api/src/model/protocol.rs
@@ -8,7 +8,9 @@ pub type SupportedProtocols = BTreeMap<String, Protocol>;
 pub struct Protocol {
     pub user_fields: Vec<String>,
     pub location_fields: Vec<String>,
-    pub icon: String,
+    // This field is documented as "required",
+    // but for some reason matrix.org does not send this
+    pub icon: Option<String>,
     pub field_types: BTreeMap<String, FieldType>,
     pub instances: Vec<ProtocolInstance>,
 }
@@ -21,7 +23,7 @@ pub struct FieldType {
 
 #[derive(Debug, Clone, Deserialize)]
 pub struct ProtocolInstance {
-    #[serde(rename = "rename_id")]
+    #[serde(rename = "network_id")]
     pub id: String,
     pub desc: String,
     pub icon: Option<String>,
diff --git a/fractal-matrix-api/src/types.rs b/fractal-matrix-api/src/types.rs
index af44e8ce..ac7ddaf1 100644
--- a/fractal-matrix-api/src/types.rs
+++ b/fractal-matrix-api/src/types.rs
@@ -3,6 +3,7 @@ pub use crate::model::member::Member;
 pub use crate::model::member::MemberList;
 pub use crate::model::message::Message;
 pub use crate::model::protocol::ProtocolInstance;
+pub use crate::model::protocol::SupportedProtocols;
 pub use crate::model::room::Room;
 pub use crate::model::room::RoomList;
 pub use crate::model::room::RoomMembership;


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