diff options
| author | Max Audron <audron@cocaine.farm> | 2021-07-25 17:43:15 +0200 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2021-07-25 17:43:15 +0200 |
| commit | e40c337a28c4507a043973bdd1f06bca6cce0269 (patch) | |
| tree | d48fb8a09e76eabbd80d7bbcef208ea95c4cc625 /src/message/signalproxy | |
| parent | cleanup objects::Types (diff) | |
add Traits for syncable objects
Diffstat (limited to '')
| -rw-r--r-- | src/message/signalproxy/mod.rs | 80 | ||||
| -rw-r--r-- | src/message/signalproxy/objects/aliasmanager.rs | 57 | ||||
| -rw-r--r-- | src/message/signalproxy/objects/ignorelistmanager.rs | 7 | ||||
| -rw-r--r-- | src/message/signalproxy/syncmessage.rs | 8 |
4 files changed, 107 insertions, 45 deletions
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 {} |
