diff options
| author | Max Audron <audron@cocaine.farm> | 2021-07-21 18:33:34 +0200 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2021-07-21 18:33:34 +0200 |
| commit | 557699e8d72f637823487220da6b23655cc3f23d (patch) | |
| tree | d12e0be0f1610e097eafd057d9cab1dca742b53d /src/message/signalproxy/objects/buffersyncer.rs | |
| parent | rewrite network derive to work more consistently (diff) | |
migrate BufferSyncer to use Network derive and add to central Types
Diffstat (limited to 'src/message/signalproxy/objects/buffersyncer.rs')
| -rw-r--r-- | src/message/signalproxy/objects/buffersyncer.rs | 157 |
1 files changed, 14 insertions, 143 deletions
diff --git a/src/message/signalproxy/objects/buffersyncer.rs b/src/message/signalproxy/objects/buffersyncer.rs index cc26310..ecacbc9 100644 --- a/src/message/signalproxy/objects/buffersyncer.rs +++ b/src/message/signalproxy/objects/buffersyncer.rs @@ -1,150 +1,21 @@ -use std::{collections::HashMap, convert::TryInto}; +use std::collections::HashMap; -use num_traits::{FromPrimitive, ToPrimitive}; +use crate::primitive::MessageType; +use libquassel_derive::Network; -use itertools::Itertools; - -use crate::{ - message::signalproxy::Network, - primitive::{MessageType, Variant, VariantList}, -}; - -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Network)] +#[network(repr = "list")] pub struct BufferSyncer { + #[network(rename = "Activities", network, variant = "VariantList")] pub activities: HashMap<i32, MessageType>, + #[network(rename = "HighlightCounts", network, variant = "VariantList")] pub highlight_counts: HashMap<i32, i32>, + #[network(rename = "LastSeenMsg", network, variant = "VariantList")] pub last_seen_msg: HashMap<i32, i64>, + #[network(rename = "MarkerLines", network, variant = "VariantList")] pub marker_line: HashMap<i32, i64>, } -impl Network for BufferSyncer { - type Item = VariantList; - - fn to_network(&self) -> Self::Item { - let mut res = Self::Item::new(); - - res.push(Variant::ByteArray(s!("Activities"))); - res.push(Variant::VariantList({ - let mut res = VariantList::new(); - - self.activities.iter().for_each(|(k, v)| { - res.push(Variant::i32(*k)); - res.push(Variant::i32(v.to_i32().unwrap())); - }); - - res - })); - - res.push(Variant::ByteArray(s!("HighlightCounts"))); - res.push(Variant::VariantList({ - let mut res = VariantList::new(); - - self.highlight_counts.iter().for_each(|(k, v)| { - res.push(Variant::i32(*k)); - res.push(Variant::i32(*v)); - }); - - res - })); - - res.push(Variant::ByteArray(s!("LastSeenMsg"))); - res.push(Variant::VariantList({ - let mut res = VariantList::new(); - - self.last_seen_msg.iter().for_each(|(k, v)| { - res.push(Variant::i32(*k)); - res.push(Variant::i64(*v)); - }); - - res - })); - - res.push(Variant::ByteArray(s!("MarkerLines"))); - res.push(Variant::VariantList({ - let mut res = VariantList::new(); - - self.marker_line.iter().for_each(|(k, v)| { - res.push(Variant::i32(*k)); - res.push(Variant::i64(*v)); - }); - - res - })); - - res - } - - fn from_network(input: &mut Self::Item) -> Self { - let mut i = input.iter().cycle(); - - i.position(|x| *x == crate::primitive::Variant::ByteArray(s!("Activities"))) - .unwrap(); - let activities: VariantList = i.next().unwrap().try_into().unwrap(); - let activities = activities - .iter() - .batching(|it| match it.next() { - None => None, - Some(x) => match it.next() { - None => None, - Some(y) => Some(( - x.try_into().unwrap(), - MessageType::from_i32(y.try_into().unwrap()).unwrap(), - )), - }, - }) - .collect(); - - i.position(|x| *x == crate::primitive::Variant::ByteArray(s!("HighlightCounts"))) - .unwrap(); - let highlight_counts: VariantList = i.next().unwrap().try_into().unwrap(); - let highlight_counts = highlight_counts - .iter() - .batching(|it| match it.next() { - None => None, - Some(x) => match it.next() { - None => None, - Some(y) => Some((x.try_into().unwrap(), y.try_into().unwrap())), - }, - }) - .collect(); - - i.position(|x| *x == crate::primitive::Variant::ByteArray(s!("LastSeenMsg"))) - .unwrap(); - let last_seen_msg: VariantList = i.next().unwrap().try_into().unwrap(); - let last_seen_msg = last_seen_msg - .iter() - .batching(|it| match it.next() { - None => None, - Some(x) => match it.next() { - None => None, - Some(y) => Some((x.try_into().unwrap(), y.try_into().unwrap())), - }, - }) - .collect(); - - i.position(|x| *x == crate::primitive::Variant::ByteArray(s!("MarkerLines"))) - .unwrap(); - let marker_line: VariantList = i.next().unwrap().try_into().unwrap(); - let marker_line = marker_line - .iter() - .batching(|it| match it.next() { - None => None, - Some(x) => match it.next() { - None => None, - Some(y) => Some((x.try_into().unwrap(), y.try_into().unwrap())), - }, - }) - .collect(); - - Self { - activities, - highlight_counts, - last_seen_msg, - marker_line, - } - } -} - pub trait BufferSyncerServer { fn activities(self: &Self) -> &HashMap<u32, MessageType>; fn activities_mut(self: &mut Self) -> &mut HashMap<u32, MessageType>; @@ -275,11 +146,11 @@ mod tests { fn get_runtime() -> BufferSyncer { BufferSyncer { activities: map! { - 1 => MessageType::None, - 2 => MessageType::None, - 3 => MessageType::None, - 4 => MessageType::None, - 5 => MessageType::None, + 1 => MessageType::NONE, + 2 => MessageType::NONE, + 3 => MessageType::NONE, + 4 => MessageType::NONE, + 5 => MessageType::NONE, }, highlight_counts: map! { 1 => 0, |
