[fractal] Add types related to third party protocol module API



commit 58c86df86416e93f423bf9588eb10e5d1adedfb9
Author: Alejandro Domínguez <adomu net-c com>
Date:   Sat Jan 12 22:01:49 2019 +0100

    Add types related to third party protocol module API

 fractal-gtk/src/appop/directory.rs          |  4 ++--
 fractal-matrix-api/src/backend/directory.rs | 23 ++++++++++++++---------
 fractal-matrix-api/src/backend/types.rs     |  4 ++--
 fractal-matrix-api/src/model/protocol.rs    | 26 +++++++++++++++++++++++++-
 fractal-matrix-api/src/types.rs             |  2 +-
 5 files changed, 44 insertions(+), 15 deletions(-)
---
diff --git a/fractal-gtk/src/appop/directory.rs b/fractal-gtk/src/appop/directory.rs
index a868ebf9..650893de 100644
--- a/fractal-gtk/src/appop/directory.rs
+++ b/fractal-gtk/src/appop/directory.rs
@@ -7,7 +7,7 @@ use crate::appop::AppOp;
 use crate::backend::BKCommand;
 use crate::widgets;
 
-use crate::types::Protocol;
+use crate::types::ProtocolInstance;
 use crate::types::Room;
 
 impl AppOp {
@@ -15,7 +15,7 @@ impl AppOp {
         self.backend.send(BKCommand::DirectoryProtocols).unwrap();
     }
 
-    pub fn set_protocols(&self, protocols: Vec<Protocol>) {
+    pub fn set_protocols(&self, protocols: Vec<ProtocolInstance>) {
         let combo = self
             .ui
             .builder
diff --git a/fractal-matrix-api/src/backend/directory.rs b/fractal-matrix-api/src/backend/directory.rs
index 8c71c0ea..5a7d25d7 100644
--- a/fractal-matrix-api/src/backend/directory.rs
+++ b/fractal-matrix-api/src/backend/directory.rs
@@ -14,7 +14,7 @@ use crate::util::cache_path;
 use crate::util::json_q;
 use crate::util::media;
 
-use crate::types::Protocol;
+use crate::types::ProtocolInstance;
 use crate::types::{Room, RoomMembership};
 
 pub fn protocols(bk: &Backend) {
@@ -31,24 +31,29 @@ pub fn protocols(bk: &Backend) {
     get!(
         &url,
         move |r: JsonValue| {
-            let mut protocols: Vec<Protocol> = vec![];
-
-            protocols.push(Protocol {
-                id: String::new(),
+            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 = Protocol {
-                            id: String::from(i["instance_id"].as_str().unwrap_or_default()),
-                            desc: String::from(i["desc"].as_str().unwrap_or_default()),
+                        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);
                     }
diff --git a/fractal-matrix-api/src/backend/types.rs b/fractal-matrix-api/src/backend/types.rs
index 73d8e0eb..6f2b8149 100644
--- a/fractal-matrix-api/src/backend/types.rs
+++ b/fractal-matrix-api/src/backend/types.rs
@@ -7,7 +7,7 @@ use crate::error::Error;
 use crate::types::Event;
 use crate::types::Member;
 use crate::types::Message;
-use crate::types::Protocol;
+use crate::types::ProtocolInstance;
 use crate::types::Room;
 use crate::types::Sticker;
 use crate::types::StickerGroup;
@@ -115,7 +115,7 @@ pub enum BKResponse {
     RoomMembers(String, Vec<Member>),
     SentMsg(String, String),
     SentMsgRedaction(String, String),
-    DirectoryProtocols(Vec<Protocol>),
+    DirectoryProtocols(Vec<ProtocolInstance>),
     DirectorySearch(Vec<Room>),
     JoinRoom,
     LeaveRoom,
diff --git a/fractal-matrix-api/src/model/protocol.rs b/fractal-matrix-api/src/model/protocol.rs
index d6a0dfd4..12be8c29 100644
--- a/fractal-matrix-api/src/model/protocol.rs
+++ b/fractal-matrix-api/src/model/protocol.rs
@@ -1,5 +1,29 @@
-#[derive(Debug, Clone)]
+use serde::Deserialize;
+use serde_json::Value as JsonValue;
+use std::collections::BTreeMap;
+
+pub type SupportedProtocols = BTreeMap<String, Protocol>;
+
+#[derive(Debug, Clone, Deserialize)]
 pub struct Protocol {
+    pub user_fields: Vec<String>,
+    pub location_fields: Vec<String>,
+    pub icon: String,
+    pub field_types: BTreeMap<String, FieldType>,
+    pub instances: Vec<ProtocolInstance>,
+}
+
+#[derive(Debug, Clone, Deserialize)]
+pub struct FieldType {
+    pub regexp: String,
+    pub placeholder: String,
+}
+
+#[derive(Debug, Clone, Deserialize)]
+pub struct ProtocolInstance {
+    #[serde(rename = "rename_id")]
     pub id: String,
     pub desc: String,
+    pub icon: Option<String>,
+    pub fields: JsonValue,
 }
diff --git a/fractal-matrix-api/src/types.rs b/fractal-matrix-api/src/types.rs
index be276b39..af44e8ce 100644
--- a/fractal-matrix-api/src/types.rs
+++ b/fractal-matrix-api/src/types.rs
@@ -2,7 +2,7 @@ pub use crate::model::event::Event;
 pub use crate::model::member::Member;
 pub use crate::model::member::MemberList;
 pub use crate::model::message::Message;
-pub use crate::model::protocol::Protocol;
+pub use crate::model::protocol::ProtocolInstance;
 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]