From bb861cb828dedaae880d1f0cea759d79020f6c90 Mon Sep 17 00:00:00 2001 From: Max Audron Date: Wed, 26 Feb 2025 17:20:15 +0100 Subject: add MsgId and BufferId to objects where needed some objects where still handling BufferId or MsgId as their raw types which lead to errors now that the Types are properly parsed in the varinats --- src/message/signalproxy/objects/buffersyncer.rs | 135 ++++++++++----------- .../signalproxy/objects/bufferviewconfig.rs | 116 ++++++++---------- 2 files changed, 114 insertions(+), 137 deletions(-) (limited to 'src/message') diff --git a/src/message/signalproxy/objects/buffersyncer.rs b/src/message/signalproxy/objects/buffersyncer.rs index f085423..b2e12c4 100644 --- a/src/message/signalproxy/objects/buffersyncer.rs +++ b/src/message/signalproxy/objects/buffersyncer.rs @@ -1,8 +1,8 @@ use std::collections::HashMap; use crate::{ - message::{Syncable, Class}, - primitive::MessageType, + message::{Class, Syncable}, + primitive::{BufferId, MessageType, MsgId}, }; use libquassel_derive::{sync, NetworkList, NetworkMap}; @@ -10,13 +10,13 @@ use libquassel_derive::{sync, NetworkList, NetworkMap}; #[derive(Default, Debug, Clone, PartialEq, NetworkList, NetworkMap)] pub struct BufferSyncer { #[network(rename = "Activities", network = "list", variant = "VariantList")] - pub activities: HashMap, + pub activities: HashMap, #[network(rename = "HighlightCounts", network = "list", variant = "VariantList")] - pub highlight_counts: HashMap, + pub highlight_counts: HashMap, #[network(rename = "LastSeenMsg", network = "list", variant = "VariantList")] - pub last_seen_msg: HashMap, + pub last_seen_msg: HashMap, #[network(rename = "MarkerLines", network = "list", variant = "VariantList")] - pub marker_line: HashMap, + pub marker_line: HashMap, } impl BufferSyncer { @@ -50,7 +50,7 @@ impl BufferSyncer { // // S->C calls - pub fn mark_buffer_as_read(&mut self, id: i32) { + pub fn mark_buffer_as_read(&mut self, id: BufferId) { self.set_buffer_activity(id, MessageType::NONE); self.set_highlight_count(id, 0); @@ -58,7 +58,7 @@ impl BufferSyncer { sync!("markBufferAsRead", [id]); } - pub fn merge_buffers_permanently(&mut self, target: i32, source: i32) { + pub fn merge_buffers_permanently(&mut self, target: BufferId, source: BufferId) { if let Some(activities) = self.activities.remove(&source) { *self.activities.entry(target).or_insert(MessageType::NONE) |= activities; } @@ -86,7 +86,7 @@ impl BufferSyncer { } // TODO remove buffer from bufferviews - pub fn remove_buffer(&mut self, id: i32) { + pub fn remove_buffer(&mut self, id: BufferId) { self.activities.remove(&id); self.highlight_counts.remove(&id); self.last_seen_msg.remove(&id); @@ -104,28 +104,28 @@ impl BufferSyncer { sync!("renameBuffer", [id, name]); } - pub fn set_buffer_activity(&mut self, id: i32, activity: MessageType) { + pub fn set_buffer_activity(&mut self, id: BufferId, activity: MessageType) { *self.activities.entry(id).or_insert(MessageType::NONE) = activity; #[cfg(feature = "server")] sync!("setBufferActivity", [id, activity.bits()]); } - pub fn set_highlight_count(&mut self, id: i32, count: i32) { + pub fn set_highlight_count(&mut self, id: BufferId, count: i32) { *self.highlight_counts.entry(id).or_default() = count; #[cfg(feature = "server")] sync!("setHighlightCount", [id, count]); } - pub fn set_last_seen_msg(&mut self, id: i32, msg_id: i64) { + pub fn set_last_seen_msg(&mut self, id: BufferId, msg_id: MsgId) { *self.last_seen_msg.entry(id).or_default() = msg_id; #[cfg(feature = "server")] sync!("setHighlightCount", [id, msg_id]); } - pub fn set_marker_line(&mut self, id: i32, msg_id: i64) { + pub fn set_marker_line(&mut self, id: BufferId, msg_id: MsgId) { *self.marker_line.entry(id).or_default() = msg_id; #[cfg(feature = "server")] @@ -141,9 +141,7 @@ impl crate::message::StatefulSyncableClient for BufferSyncer { { match msg.slot_name.as_str() { "markBufferAsRead" => self.mark_buffer_as_read(get_param!(msg)), - "mergeBuffersPermanently" => { - self.merge_buffers_permanently(get_param!(msg), get_param!(msg)) - } + "mergeBuffersPermanently" => self.merge_buffers_permanently(get_param!(msg), get_param!(msg)), "removeBuffer" => self.remove_buffer(get_param!(msg)), "renameBuffer" => self.rename_buffer(get_param!(msg), get_param!(msg)), "setBufferActivity" => self.set_buffer_activity( @@ -194,55 +192,55 @@ mod tests { vec![ Variant::ByteArray(s!("Activities")), Variant::VariantList(vec![ - Variant::i32(1), + Variant::BufferId(BufferId(1)), Variant::i32(0), - Variant::i32(2), + Variant::BufferId(BufferId(2)), Variant::i32(0), - Variant::i32(3), + Variant::BufferId(BufferId(3)), Variant::i32(0), - Variant::i32(4), + Variant::BufferId(BufferId(4)), Variant::i32(0), - Variant::i32(5), + Variant::BufferId(BufferId(5)), Variant::i32(0), ]), Variant::ByteArray(s!("HighlightCounts")), Variant::VariantList(vec![ - Variant::i32(1), + Variant::BufferId(BufferId(1)), Variant::i32(0), - Variant::i32(2), + Variant::BufferId(BufferId(2)), Variant::i32(0), - Variant::i32(3), + Variant::BufferId(BufferId(3)), Variant::i32(0), - Variant::i32(4), + Variant::BufferId(BufferId(4)), Variant::i32(0), - Variant::i32(5), + Variant::BufferId(BufferId(5)), Variant::i32(0), ]), Variant::ByteArray(s!("LastSeenMsg")), Variant::VariantList(vec![ - Variant::i32(1), - Variant::i64(2185), - Variant::i32(2), - Variant::i64(2188), - Variant::i32(3), - Variant::i64(860), - Variant::i32(4), - Variant::i64(2183), - Variant::i32(5), - Variant::i64(2180), + Variant::BufferId(BufferId(1)), + Variant::MsgId(MsgId(2185)), + Variant::BufferId(BufferId(2)), + Variant::MsgId(MsgId(2188)), + Variant::BufferId(BufferId(3)), + Variant::MsgId(MsgId(860)), + Variant::BufferId(BufferId(4)), + Variant::MsgId(MsgId(2183)), + Variant::BufferId(BufferId(5)), + Variant::MsgId(MsgId(2180)), ]), Variant::ByteArray(s!("MarkerLines")), Variant::VariantList(vec![ - Variant::i32(1), - Variant::i64(2185), - Variant::i32(2), - Variant::i64(2188), - Variant::i32(3), - Variant::i64(860), - Variant::i32(4), - Variant::i64(1527), - Variant::i32(5), - Variant::i64(2180), + Variant::BufferId(BufferId(1)), + Variant::MsgId(MsgId(2185)), + Variant::BufferId(BufferId(2)), + Variant::MsgId(MsgId(2188)), + Variant::BufferId(BufferId(3)), + Variant::MsgId(MsgId(860)), + Variant::BufferId(BufferId(4)), + Variant::MsgId(MsgId(1527)), + Variant::BufferId(BufferId(5)), + Variant::MsgId(MsgId(2180)), ]), ] } @@ -250,32 +248,32 @@ mod tests { fn get_runtime() -> BufferSyncer { BufferSyncer { activities: map! { - 1 => MessageType::NONE, - 2 => MessageType::NONE, - 3 => MessageType::NONE, - 4 => MessageType::NONE, - 5 => MessageType::NONE, + BufferId(1) => MessageType::NONE, + BufferId(2) => MessageType::NONE, + BufferId(3) => MessageType::NONE, + BufferId(4) => MessageType::NONE, + BufferId(5) => MessageType::NONE, }, highlight_counts: map! { - 1 => 0, - 2 => 0, - 3 => 0, - 4 => 0, - 5 => 0, + BufferId(1) => 0, + BufferId(2) => 0, + BufferId(3) => 0, + BufferId(4) => 0, + BufferId(5) => 0, }, last_seen_msg: map! { - 1 => 2185, - 2 => 2188, - 3 => 860, - 4 => 2183, - 5 => 2180, + BufferId(1) => MsgId(2185), + BufferId(2) => MsgId(2188), + BufferId(3) => MsgId(860), + BufferId(4) => MsgId(2183), + BufferId(5) => MsgId(2180), }, marker_line: map! { - 1 => 2185, - 2 => 2188, - 3 => 860, - 4 => 1527, - 5 => 2180, + BufferId(1) => MsgId(2185), + BufferId(2) => MsgId(2188), + BufferId(3) => MsgId(860), + BufferId(4) => MsgId(1527), + BufferId(5) => MsgId(2180), }, } } @@ -288,9 +286,6 @@ mod tests { #[test] fn buffersyncer_from_network() { - assert_eq!( - BufferSyncer::from_network_list(&mut get_network()), - get_runtime() - ) + assert_eq!(BufferSyncer::from_network_list(&mut get_network()), get_runtime()) } } diff --git a/src/message/signalproxy/objects/bufferviewconfig.rs b/src/message/signalproxy/objects/bufferviewconfig.rs index bc60768..a31ec92 100644 --- a/src/message/signalproxy/objects/bufferviewconfig.rs +++ b/src/message/signalproxy/objects/bufferviewconfig.rs @@ -7,20 +7,20 @@ use crate::message::StatefulSyncableClient; use crate::message::StatefulSyncableServer; use crate::message::{Class, Syncable}; -use crate::primitive::VariantList; +use crate::primitive::{BufferId, VariantList}; #[derive(Debug, Default, Clone, PartialEq, NetworkList, NetworkMap)] pub struct BufferViewConfig { #[network(rename = "BufferList", network = "map", variant = "VariantList")] - pub buffers: Vec, + pub buffers: Vec, #[network(rename = "RemovedBuffers", network = "map", variant = "VariantList")] - pub removed_buffers: Vec, + pub removed_buffers: Vec, #[network( rename = "TemporarilyRemovedBuffers", network = "map", variant = "VariantList" )] - pub temporarily_removed_buffers: Vec, + pub temporarily_removed_buffers: Vec, #[network(rename = "bufferViewId", default, skip)] pub buffer_view_id: i32, @@ -49,11 +49,11 @@ pub struct BufferViewConfig { #[allow(dead_code)] impl BufferViewConfig { - pub fn request_add_buffer(&self, id: i32, pos: usize) { + pub fn request_add_buffer(&self, id: BufferId, pos: usize) { sync!("requestAddBuffer", [id, (pos as i32)]); } - pub fn add_buffer(&mut self, id: i32, pos: usize) { + pub fn add_buffer(&mut self, id: BufferId, pos: usize) { if !self.buffers.contains(&id) { self.buffers.insert(pos, id) } @@ -62,11 +62,7 @@ impl BufferViewConfig { self.removed_buffers.remove(old_pos); } - if let Some(old_pos) = self - .temporarily_removed_buffers - .iter() - .position(|&x| x == id) - { + if let Some(old_pos) = self.temporarily_removed_buffers.iter().position(|&x| x == id) { self.temporarily_removed_buffers.remove(old_pos); } @@ -74,11 +70,11 @@ impl BufferViewConfig { sync!("addBuffer", [id, (pos as i32)]); } - pub fn request_move_buffer(&self, id: i32, pos: usize) { + pub fn request_move_buffer(&self, id: BufferId, pos: usize) { sync!("requestMoveBuffer", [id, (pos as i32)]); } - pub fn move_buffer(&mut self, id: i32, pos: usize) { + pub fn move_buffer(&mut self, id: BufferId, pos: usize) { let old_pos = self.buffers.iter().position(|&x| x == id).unwrap(); self.buffers.remove(old_pos); self.buffers.insert(pos, id); @@ -87,11 +83,11 @@ impl BufferViewConfig { sync!("moveBuffer", [id, (pos as i32)]); } - pub fn request_remove_buffer(&mut self, id: i32) { + pub fn request_remove_buffer(&mut self, id: BufferId) { sync!("requestRemoveBuffer", [id]); } - pub fn remove_buffer(&mut self, id: i32) { + pub fn remove_buffer(&mut self, id: BufferId) { if let Some(old_pos) = self.buffers.iter().position(|&x| x == id) { self.buffers.remove(old_pos); } @@ -108,20 +104,16 @@ impl BufferViewConfig { sync!("removeBuffer", [id]); } - pub fn request_remove_buffer_permanently(&mut self, id: i32) { + pub fn request_remove_buffer_permanently(&mut self, id: BufferId) { sync!("requestRemoveBufferPermanently", [id]); } - pub fn remove_buffer_permanently(&mut self, id: i32) { + pub fn remove_buffer_permanently(&mut self, id: BufferId) { if let Some(old_pos) = self.buffers.iter().position(|&x| x == id) { self.buffers.remove(old_pos); } - if let Some(old_pos) = self - .temporarily_removed_buffers - .iter() - .position(|&x| x == id) - { + if let Some(old_pos) = self.temporarily_removed_buffers.iter().position(|&x| x == id) { self.temporarily_removed_buffers.remove(old_pos); } @@ -181,27 +173,17 @@ impl StatefulSyncableServer for BufferViewConfig { "setAddNewBuffersAutomatically" => { self.add_new_buffers_automatically = msg.params.remove(0).try_into().unwrap() } - "setAllowedBufferTypes" => { - self.allowed_buffer_types = msg.params.remove(0).try_into().unwrap() - } + "setAllowedBufferTypes" => self.allowed_buffer_types = msg.params.remove(0).try_into().unwrap(), "setBufferViewName" => self.buffer_view_name = msg.params.remove(0).try_into().unwrap(), - "setDisableDecoration" => { - self.disable_decoration = msg.params.remove(0).try_into().unwrap() - } - "setHideInactiveBuffers" => { - self.hide_inactive_buffers = msg.params.remove(0).try_into().unwrap() - } + "setDisableDecoration" => self.disable_decoration = msg.params.remove(0).try_into().unwrap(), + "setHideInactiveBuffers" => self.hide_inactive_buffers = msg.params.remove(0).try_into().unwrap(), "setHideInactiveNetworks" => { self.hide_inactive_networks = msg.params.remove(0).try_into().unwrap() } - "setMinimumActivity" => { - self.minimum_activity = msg.params.remove(0).try_into().unwrap() - } + "setMinimumActivity" => self.minimum_activity = msg.params.remove(0).try_into().unwrap(), "setNetworkId" => self.network_id = msg.params.remove(0).try_into().unwrap(), "setShowSearch" => self.show_search = msg.params.remove(0).try_into().unwrap(), - "setSortAlphabetically" => { - self.sort_alphabetically = msg.params.remove(0).try_into().unwrap() - } + "setSortAlphabetically" => self.sort_alphabetically = msg.params.remove(0).try_into().unwrap(), _ => (), } } @@ -226,9 +208,9 @@ mod tests { fn bufferviewconfig_sample() -> BufferViewConfig { BufferViewConfig { - buffers: vec![1, 2, 3], - removed_buffers: vec![4, 5], - temporarily_removed_buffers: vec![6, 7], + buffers: vec![1.into(), 2.into(), 3.into()], + removed_buffers: vec![4.into(), 5.into()], + temporarily_removed_buffers: vec![6.into(), 7.into()], ..Default::default() } } @@ -237,17 +219,17 @@ mod tests { fn bufferviewconfig_add_buffer() { // Add existing buffer, no change let mut buffer_view_config = bufferviewconfig_sample(); - buffer_view_config.add_buffer(1, 2); + buffer_view_config.add_buffer(1.into(), 2); assert_eq!(bufferviewconfig_sample(), buffer_view_config); // Add new buffer let mut buffer_view_config = bufferviewconfig_sample(); - buffer_view_config.add_buffer(10, 1); + buffer_view_config.add_buffer(10.into(), 1); assert_eq!( BufferViewConfig { - buffers: vec![1, 10, 2, 3], - removed_buffers: vec![4, 5], - temporarily_removed_buffers: vec![6, 7], + buffers: vec![1.into(), 10.into(), 2.into(), 3.into()], + removed_buffers: vec![4.into(), 5.into()], + temporarily_removed_buffers: vec![6.into(), 7.into()], ..Default::default() }, buffer_view_config @@ -255,17 +237,17 @@ mod tests { // Add new buffer, remove from removed buffers let mut buffer_view_config = BufferViewConfig { - buffers: vec![1, 2, 3], - removed_buffers: vec![4, 5, 10], - temporarily_removed_buffers: vec![6, 7, 10], + buffers: vec![1.into(), 2.into(), 3.into()], + removed_buffers: vec![4.into(), 5.into(), 10.into()], + temporarily_removed_buffers: vec![6.into(), 7.into(), 10.into()], ..Default::default() }; - buffer_view_config.add_buffer(10, 1); + buffer_view_config.add_buffer(10.into(), 1); assert_eq!( BufferViewConfig { - buffers: vec![1, 10, 2, 3], - removed_buffers: vec![4, 5], - temporarily_removed_buffers: vec![6, 7], + buffers: vec![1.into(), 10.into(), 2.into(), 3.into()], + removed_buffers: vec![4.into(), 5.into()], + temporarily_removed_buffers: vec![6.into(), 7.into()], ..Default::default() }, buffer_view_config @@ -276,17 +258,17 @@ mod tests { fn bufferviewconfig_remove_buffer() { // Remove already removed buffer let mut buffer_view_config = bufferviewconfig_sample(); - buffer_view_config.remove_buffer(6); + buffer_view_config.remove_buffer(6.into()); assert_eq!(bufferviewconfig_sample(), buffer_view_config); // Remove buffer let mut buffer_view_config = bufferviewconfig_sample(); - buffer_view_config.remove_buffer(1); + buffer_view_config.remove_buffer(1.into()); assert_eq!( BufferViewConfig { - buffers: vec![2, 3], - removed_buffers: vec![4, 5], - temporarily_removed_buffers: vec![6, 7, 1], + buffers: vec![2.into(), 3.into()], + removed_buffers: vec![4.into(), 5.into()], + temporarily_removed_buffers: vec![6.into(), 7.into(), 1.into()], ..Default::default() }, buffer_view_config @@ -297,17 +279,17 @@ mod tests { fn bufferviewconfig_remove_buffer_permanently() { // Remove already removed buffer let mut buffer_view_config = bufferviewconfig_sample(); - buffer_view_config.remove_buffer_permanently(4); + buffer_view_config.remove_buffer_permanently(4.into()); assert_eq!(bufferviewconfig_sample(), buffer_view_config); // Remove buffer let mut buffer_view_config = bufferviewconfig_sample(); - buffer_view_config.remove_buffer_permanently(1); + buffer_view_config.remove_buffer_permanently(1.into()); assert_eq!( BufferViewConfig { - buffers: vec![2, 3], - removed_buffers: vec![4, 5, 1], - temporarily_removed_buffers: vec![6, 7], + buffers: vec![2.into(), 3.into()], + removed_buffers: vec![4.into(), 5.into(), 1.into()], + temporarily_removed_buffers: vec![6.into(), 7.into()], ..Default::default() }, buffer_view_config @@ -318,17 +300,17 @@ mod tests { fn bufferviewconfig_move_buffer() { // Do nothing let mut buffer_view_config = bufferviewconfig_sample(); - buffer_view_config.move_buffer(1, 0); + buffer_view_config.move_buffer(1.into(), 0); assert_eq!(bufferviewconfig_sample(), buffer_view_config); // Move buffer let mut buffer_view_config = bufferviewconfig_sample(); - buffer_view_config.move_buffer(1, 1); + buffer_view_config.move_buffer(1.into(), 1); assert_eq!( BufferViewConfig { - buffers: vec![2, 1, 3], - removed_buffers: vec![4, 5], - temporarily_removed_buffers: vec![6, 7], + buffers: vec![2.into(), 1.into(), 3.into()], + removed_buffers: vec![4.into(), 5.into()], + temporarily_removed_buffers: vec![6.into(), 7.into()], ..Default::default() }, buffer_view_config -- cgit v1.2.3