aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--Cargo.toml6
-rw-r--r--derive/src/network/maplist.rs4
-rw-r--r--src/message/signalproxy/mod.rs80
-rw-r--r--src/message/signalproxy/objects/aliasmanager.rs57
-rw-r--r--src/message/signalproxy/objects/ignorelistmanager.rs7
-rw-r--r--src/message/signalproxy/syncmessage.rs8
6 files changed, 114 insertions, 48 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 620f4e0..520b917 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -38,6 +38,10 @@ framing = ["tokio", "tokio-util", "flate2", "bytes"]
# Enable all the quassel features
all-quassel-features = ["long-message-id", "long-time", "rich-messages", "sender-prefixes", "authenticators"]
+# Either act as a client or a server
+client = []
+server = []
+
# Serialize message IDs as i64
long-message-id = []
# Serialize Message Time as i64
@@ -50,7 +54,7 @@ sender-prefixes = []
authenticators = []
-default = []
+default = ["client"]
[package.metadata.docs.rs]
# document all features
diff --git a/derive/src/network/maplist.rs b/derive/src/network/maplist.rs
index 09fbe50..5b9b4d1 100644
--- a/derive/src/network/maplist.rs
+++ b/derive/src/network/maplist.rs
@@ -3,7 +3,7 @@ use quote::quote;
use crate::network::get_field_variant_type;
-use super::{get_field_type, NetworkField};
+use super::NetworkField;
pub(crate) fn to(fields: &Vec<NetworkField>) -> Vec<TokenStream> {
fields
@@ -167,7 +167,7 @@ pub(crate) fn from_vec(type_name: &Ident, fields: &Vec<NetworkField>) -> TokenSt
let _field_name = field.ident.as_ref().unwrap();
- let field_type = get_field_variant_type(field);
+ let _field_type = get_field_variant_type(field);
let field_variant = match &field.variant {
None => quote! {crate::primitive::VariantList},
diff --git a/src/message/signalproxy/mod.rs b/src/message/signalproxy/mod.rs
index 2887407..b06bcee 100644
--- a/src/message/signalproxy/mod.rs
+++ b/src/message/signalproxy/mod.rs
@@ -1,4 +1,11 @@
-use crate::{deserialize::Deserialize, serialize::Serialize};
+use std::convert::TryInto;
+
+use crate::{
+ deserialize::Deserialize,
+ primitive::{VariantList, VariantMap},
+ serialize::Serialize,
+ session::Session,
+};
use num_derive::{FromPrimitive, ToPrimitive};
@@ -18,6 +25,77 @@ pub use initrequest::*;
pub use rpccall::*;
pub use syncmessage::*;
+/// SyncProxy sends sync and rpc messages
+pub trait SyncProxy: Session {
+ fn sync(
+ &self,
+ class_name: &str,
+ object_name: Option<&str>,
+ function: &str,
+ params: VariantList,
+ );
+
+ /// Send a RpcCall
+ fn rpc(&self, function: &str, params: VariantList);
+}
+
+/// A base Syncable Object
+pub trait Syncable {
+ /// Send a SyncMessage.
+ ///
+ /// Must implement a call to session.sync() that sets the class and object names
+ ///
+ /// Example:
+ /// ```ignore
+ /// impl Syncable for AliasManager {
+ /// fn sync(&self, session: impl SyncProxy, function: &str, params: VariantList) {
+ /// session.sync("AliasManager", None, function, params)
+ /// }
+ /// }
+ /// ```
+ fn sync(&self, session: impl SyncProxy, function: &str, params: VariantList);
+
+ /// Send a RpcCall
+ fn rpc(&self, session: impl SyncProxy, function: &str, params: VariantList) {
+ session.rpc(function, params);
+ }
+}
+
+// TODO handle client vs server with feature flag
+/// A Stateful Syncable Object
+#[allow(unused_variables)]
+pub trait StatefulSyncable: Syncable {
+ /// Client -> Server: Update the whole object with received data
+ fn update(&mut self, session: impl SyncProxy, params: VariantMap)
+ where
+ Self: Sized + From<VariantMap>,
+ {
+ #[cfg(feature = "client")]
+ {
+ self.sync(session, "update", vec![params.into()]);
+ }
+ #[cfg(feature = "server")]
+ {
+ *self = params.try_into().unwrap();
+ }
+ }
+
+ /// Server -> Client: Update the whole object with received data
+ fn request_update(&mut self, session: impl SyncProxy, params: VariantMap)
+ where
+ Self: Sized + From<VariantMap>,
+ {
+ #[cfg(feature = "client")]
+ {
+ *self = params.try_into().unwrap();
+ }
+ #[cfg(feature = "server")]
+ {
+ self.sync(session, "requestUpdate", vec![params.into()]);
+ }
+ }
+}
+
#[derive(Clone, Debug, std::cmp::PartialEq)]
pub enum Message {
/// Bidirectional
diff --git a/src/message/signalproxy/objects/aliasmanager.rs b/src/message/signalproxy/objects/aliasmanager.rs
index b536cde..d0e5d5b 100644
--- a/src/message/signalproxy/objects/aliasmanager.rs
+++ b/src/message/signalproxy/objects/aliasmanager.rs
@@ -1,5 +1,10 @@
use libquassel_derive::Network;
+use crate::message::{StatefulSyncable, SyncProxy, Syncable};
+
+/// AliasManager
+/// keeps a list of all registered aliases
+/// syncable
#[derive(Clone, Debug, std::cmp::PartialEq, Network)]
#[network(repr = "list")]
pub struct AliasManager {
@@ -7,6 +12,24 @@ pub struct AliasManager {
pub aliases: Vec<Alias>,
}
+impl AliasManager {
+ pub fn add_alias(&mut self, alias: Alias) {
+ // TODO check if name is equal
+ if !self.aliases.contains(&alias) {
+ self.aliases.push(alias)
+ }
+ }
+}
+
+impl StatefulSyncable for AliasManager {}
+impl Syncable for AliasManager {
+ fn sync(&self, session: impl SyncProxy, function: &str, params: crate::primitive::VariantList) {
+ session.sync("AliasManager", None, function, params)
+ }
+}
+
+/// Alias
+/// Represents a signle alias
#[derive(Clone, Debug, std::cmp::PartialEq, Network)]
#[network(repr = "maplist")]
pub struct Alias {
@@ -78,37 +101,3 @@ mod tests {
assert_eq!(AliasManager::from_network(&mut get_dest()), get_src())
}
}
-
-// impl AliasManager {
-// /// Client to Server
-// ///
-// /// Replaces all properties of the object with the content of the
-// /// "properties" parameter. This parameter is in network representation.
-// ///
-// fn request_update(self: &mut Self, properties: VariantMap) {
-// self.update(properties);
-// }
-
-// /// Server to Client
-// fn add_alias(self: &mut Self, name: String, expansion: String) {
-// self.aliases.push(Alias { name, expansion });
-// }
-
-// /// Server to Client
-// ///
-// /// Replaces all properties of the object with the content of the
-// /// "properties" parameter. This parameter is in network representation.
-// ///
-// fn update(self: &mut Self, properties: VariantMap) {
-// let mut alias: Vec<Alias> = Vec::new();
-
-// // for (i, name) in match_variant!(properties[&"Aliases".to_string()], Variant::String) {
-// // alias.push(Alias {
-// // name,
-// // expansion: match_variant!(properties["Aliases"], Variant::String)["expansions"][i],
-// // })
-// // }
-
-// self.aliases = alias
-// }
-// }
diff --git a/src/message/signalproxy/objects/ignorelistmanager.rs b/src/message/signalproxy/objects/ignorelistmanager.rs
index 66d3556..cb51f21 100644
--- a/src/message/signalproxy/objects/ignorelistmanager.rs
+++ b/src/message/signalproxy/objects/ignorelistmanager.rs
@@ -1,10 +1,5 @@
use libquassel_derive::Network;
-use std::{
- collections::HashMap,
- convert::{TryFrom, TryInto},
-};
-
-use crate::primitive::{Variant, VariantList};
+use std::convert::TryFrom;
#[derive(Debug, Clone, PartialEq, Network)]
#[network(repr = "list")]
diff --git a/src/message/signalproxy/syncmessage.rs b/src/message/signalproxy/syncmessage.rs
index 9c1716b..bbf4994 100644
--- a/src/message/signalproxy/syncmessage.rs
+++ b/src/message/signalproxy/syncmessage.rs
@@ -4,10 +4,10 @@ use crate::{deserialize::Deserialize, serialize::Serialize};
#[derive(Clone, Debug, std::cmp::PartialEq)]
pub struct SyncMessage {
- class_name: String,
- object_name: String,
- slot_name: String,
- params: VariantList,
+ pub class_name: String,
+ pub object_name: String,
+ pub slot_name: String,
+ pub params: VariantList,
}
// impl Act for SyncMessage {}