From ee2e621d5b19de08becd33c13520f8826407cf84 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 17 Oct 2020 15:00:27 +0200 Subject: change data in response to channel-list to a tree --- mumlib/src/command.rs | 3 +- mumlib/src/state.rs | 218 +++++--------------------------------------------- 2 files changed, 22 insertions(+), 199 deletions(-) (limited to 'mumlib') diff --git a/mumlib/src/command.rs b/mumlib/src/command.rs index b2ac321..d273ee9 100644 --- a/mumlib/src/command.rs +++ b/mumlib/src/command.rs @@ -1,7 +1,6 @@ use crate::state::{Channel, Server}; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; #[derive(Clone, Debug, Deserialize, Serialize)] pub enum Command { @@ -22,7 +21,7 @@ pub enum Command { #[derive(Debug, Deserialize, Serialize)] pub enum CommandResponse { ChannelList { - channels: HashMap, + channels: Channel, }, Status { username: Option, diff --git a/mumlib/src/state.rs b/mumlib/src/state.rs index f90634e..59f9c64 100644 --- a/mumlib/src/state.rs +++ b/mumlib/src/state.rs @@ -1,217 +1,41 @@ -use log::*; -use mumble_protocol::control::msgs; use serde::{Deserialize, Serialize}; -use std::collections::hash_map::Entry; -use std::collections::HashMap; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Server { - channels: HashMap, - users: HashMap, - pub welcome_text: Option, + channels: Vec, + welcome_text: Option, } impl Server { pub fn new() -> Self { Self { - channels: HashMap::new(), - users: HashMap::new(), + channels: Vec::new(), welcome_text: None, } } - - pub fn parse_server_sync(&mut self, mut msg: msgs::ServerSync) { - if msg.has_welcome_text() { - self.welcome_text = Some(msg.take_welcome_text()); - } - } - - pub fn parse_channel_state(&mut self, msg: msgs::ChannelState) { - if !msg.has_channel_id() { - warn!("Can't parse channel state without channel id"); - return; - } - match self.channels.entry(msg.get_channel_id()) { - Entry::Vacant(e) => { - e.insert(Channel::new(msg)); - } - Entry::Occupied(mut e) => e.get_mut().parse_channel_state(msg), - } - } - - pub fn parse_channel_remove(&mut self, msg: msgs::ChannelRemove) { - if !msg.has_channel_id() { - warn!("Can't parse channel remove without channel id"); - return; - } - match self.channels.entry(msg.get_channel_id()) { - Entry::Vacant(_) => { - warn!("Attempted to remove channel that doesn't exist"); - } - Entry::Occupied(e) => { - e.remove(); - } - } - } - - pub fn parse_user_state(&mut self, msg: msgs::UserState) { - if !msg.has_session() { - warn!("Can't parse user state without session"); - return; - } - match self.users.entry(msg.get_session()) { - Entry::Vacant(e) => { - e.insert(User::new(msg)); - } - Entry::Occupied(mut e) => e.get_mut().parse_user_state(msg), - } - } - - pub fn channels(&self) -> &HashMap { - &self.channels - } - - pub fn users(&self) -> &HashMap { - &self.users - } } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Channel { - description: Option, - links: Vec, - max_users: u32, - name: String, - parent: Option, - position: i32, -} - -impl Channel { - pub fn new(mut msg: msgs::ChannelState) -> Self { - Self { - description: if msg.has_description() { - Some(msg.take_description()) - } else { - None - }, - links: Vec::new(), - max_users: msg.get_max_users(), - name: msg.take_name(), - parent: if msg.has_parent() { - Some(msg.get_parent()) - } else { - None - }, - position: msg.get_position(), - } - } - - pub fn parse_channel_state(&mut self, mut msg: msgs::ChannelState) { - if msg.has_description() { - self.description = Some(msg.take_description()); - } - self.links = msg.take_links(); - if msg.has_max_users() { - self.max_users = msg.get_max_users(); - } - if msg.has_name() { - self.name = msg.take_name(); - } - if msg.has_parent() { - self.parent = Some(msg.get_parent()); - } - if msg.has_position() { - self.position = msg.get_position(); - } - } - - pub fn name(&self) -> &str { - &self.name - } + pub description: Option, + pub links: Vec>, //to represent several walks through the tree to find channels its linked to + pub max_users: u32, + pub name: String, + pub children: Vec, + pub users: Vec, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct User { - channel: u32, - comment: Option, - hash: Option, - name: String, - priority_speaker: bool, - recording: bool, - - suppress: bool, // by me - self_mute: bool, // by self - self_deaf: bool, // by self - mute: bool, // by admin - deaf: bool, // by admin -} - -impl User { - pub fn new(mut msg: msgs::UserState) -> Self { - Self { - channel: msg.get_channel_id(), - comment: if msg.has_comment() { - Some(msg.take_comment()) - } else { - None - }, - hash: if msg.has_hash() { - Some(msg.take_hash()) - } else { - None - }, - name: msg.take_name(), - priority_speaker: msg.has_priority_speaker() && msg.get_priority_speaker(), - recording: msg.has_recording() && msg.get_recording(), - suppress: msg.has_suppress() && msg.get_suppress(), - self_mute: msg.has_self_mute() && msg.get_self_mute(), - self_deaf: msg.has_self_deaf() && msg.get_self_deaf(), - mute: msg.has_mute() && msg.get_mute(), - deaf: msg.has_deaf() && msg.get_deaf(), - } - } - - pub fn parse_user_state(&mut self, mut msg: msgs::UserState) { - if msg.has_channel_id() { - self.channel = msg.get_channel_id(); - } - if msg.has_comment() { - self.comment = Some(msg.take_comment()); - } - if msg.has_hash() { - self.hash = Some(msg.take_hash()); - } - if msg.has_name() { - self.name = msg.take_name(); - } - if msg.has_priority_speaker() { - self.priority_speaker = msg.get_priority_speaker(); - } - if msg.has_recording() { - self.recording = msg.get_recording(); - } - if msg.has_suppress() { - self.suppress = msg.get_suppress(); - } - if msg.has_self_mute() { - self.self_mute = msg.get_self_mute(); - } - if msg.has_self_deaf() { - self.self_deaf = msg.get_self_deaf(); - } - if msg.has_mute() { - self.mute = msg.get_mute(); - } - if msg.has_deaf() { - self.deaf = msg.get_deaf(); - } - } - - pub fn name(&self) -> &str { - &self.name - } - - pub fn channel(&self) -> u32 { - self.channel - } -} + pub comment: Option, + pub hash: Option, + pub name: String, + pub priority_speaker: bool, + pub recording: bool, + + pub suppress: bool, // by me + pub self_mute: bool, // by self + pub self_deaf: bool, // by self + pub mute: bool, // by admin + pub deaf: bool, // by admin +} \ No newline at end of file -- cgit v1.2.1 From b10d20a20496eb9287975fc9fd9b688d59896031 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 17 Oct 2020 16:16:23 +0200 Subject: change status data transfer struct --- mumlib/src/command.rs | 1 - mumlib/src/state.rs | 15 ++++----------- 2 files changed, 4 insertions(+), 12 deletions(-) (limited to 'mumlib') diff --git a/mumlib/src/command.rs b/mumlib/src/command.rs index d273ee9..a216445 100644 --- a/mumlib/src/command.rs +++ b/mumlib/src/command.rs @@ -24,7 +24,6 @@ pub enum CommandResponse { channels: Channel, }, Status { - username: Option, server_state: Server, }, } diff --git a/mumlib/src/state.rs b/mumlib/src/state.rs index 59f9c64..01a31ab 100644 --- a/mumlib/src/state.rs +++ b/mumlib/src/state.rs @@ -2,17 +2,10 @@ use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Server { - channels: Vec, - welcome_text: Option, -} - -impl Server { - pub fn new() -> Self { - Self { - channels: Vec::new(), - welcome_text: None, - } - } + pub channels: Channel, + pub welcome_text: Option, + pub username: String, + pub host: String, } #[derive(Clone, Debug, Deserialize, Serialize)] -- cgit v1.2.1 From c873e4250c58d872763129bff2c0b0e6c91c0a2a Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 17 Oct 2020 18:39:36 +0200 Subject: add pretty printing for mumctl status --- mumlib/src/state.rs | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) (limited to 'mumlib') diff --git a/mumlib/src/state.rs b/mumlib/src/state.rs index 01a31ab..96598de 100644 --- a/mumlib/src/state.rs +++ b/mumlib/src/state.rs @@ -1,4 +1,6 @@ use serde::{Deserialize, Serialize}; +use std::fmt::Display; +use serde::export::Formatter; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Server { @@ -18,6 +20,92 @@ pub struct Channel { pub users: Vec, } +impl Channel { + pub fn iter(&self) -> Iter<'_> { + Iter { + me: Some(&self), + channel: if self.children.len() > 0 { Some(0) } else { None }, + channels: self.children.iter().map(|e| e.iter()).collect() + } + } + + pub fn users_iter(&self) -> UsersIter<'_> { + UsersIter { + channels: self.children.iter().map(|e| e.users_iter()).collect(), + channel: if self.children.len() > 0 { Some(0) } else { None }, + user: if self.users.len() > 0 { Some(0) } else { None }, + users: &self.users + } + } +} + +pub struct Iter<'a> { + me: Option<&'a Channel>, + channel: Option, + channels: Vec>, +} + +impl<'a> Iterator for Iter<'a> { + type Item = &'a Channel; + + fn next(&mut self) -> Option { + if self.me.is_some() { + self.me.take() + } else if let Some(mut c) = self.channel { + let mut n = self.channels[c].next(); + while n.is_none() { + c += 1; + if c >= self.channels.len() { + self.channel = None; + return None; + } + n = self.channels[c].next(); + } + self.channel = Some(c); + n + } else { + None + } + } +} + +pub struct UsersIter<'a> { + channel: Option, + channels: Vec>, + user: Option, + users: &'a [User], +} + +impl<'a> Iterator for UsersIter<'a> { + type Item = &'a User; + + fn next(&mut self) -> Option { + if let Some(u) = self.user { + let ret = Some(&self.users[u]); + if u + 1 < self.users.len() { + self.user = Some(u + 1); + } else { + self.user = None; + } + ret + } else if let Some(mut c) = self.channel { + let mut n = self.channels[c].next(); + while n.is_none() { + c += 1; + if c >= self.channels.len() { + self.channel = None; + return None; + } + n = self.channels[c].next(); + } + self.channel = Some(c); + n + } else { + None + } + } +} + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct User { pub comment: Option, @@ -31,4 +119,10 @@ pub struct User { pub self_deaf: bool, // by self pub mute: bool, // by admin pub deaf: bool, // by admin +} + +impl Display for User { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.name) + } } \ No newline at end of file -- cgit v1.2.1 From 821107122299eebde5da1223ea328f63782ceac9 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 17 Oct 2020 20:44:54 +0200 Subject: cargo fmt --- mumlib/src/command.rs | 8 ++------ mumlib/src/error.rs | 10 ++++++---- mumlib/src/lib.rs | 2 +- mumlib/src/state.rs | 20 ++++++++++++++------ 4 files changed, 23 insertions(+), 17 deletions(-) (limited to 'mumlib') diff --git a/mumlib/src/command.rs b/mumlib/src/command.rs index a216445..e8f4a4d 100644 --- a/mumlib/src/command.rs +++ b/mumlib/src/command.rs @@ -20,10 +20,6 @@ pub enum Command { #[derive(Debug, Deserialize, Serialize)] pub enum CommandResponse { - ChannelList { - channels: Channel, - }, - Status { - server_state: Server, - }, + ChannelList { channels: Channel }, + Status { server_state: Server }, } diff --git a/mumlib/src/error.rs b/mumlib/src/error.rs index cb88aa7..9728f32 100644 --- a/mumlib/src/error.rs +++ b/mumlib/src/error.rs @@ -1,6 +1,6 @@ -use serde::{Serialize, Deserialize}; -use std::fmt::Display; use serde::export::Formatter; +use serde::{Deserialize, Serialize}; +use std::fmt::Display; pub type Result = std::result::Result; @@ -18,7 +18,9 @@ impl Display for Error { Error::DisconnectedError => write!(f, "Not connected to a server"), Error::AlreadyConnectedError => write!(f, "Already connected to a server"), Error::InvalidChannelIdError(id) => write!(f, "Invalid channel id: {}", id), - Error::InvalidServerAddrError(addr, port) => write!(f, "Invalid server address: {}:{}", addr, port), + Error::InvalidServerAddrError(addr, port) => { + write!(f, "Invalid server address: {}:{}", addr, port) + } } } -} \ No newline at end of file +} diff --git a/mumlib/src/lib.rs b/mumlib/src/lib.rs index b77653c..d989a7e 100644 --- a/mumlib/src/lib.rs +++ b/mumlib/src/lib.rs @@ -1,6 +1,6 @@ pub mod command; -pub mod state; pub mod error; +pub mod state; use colored::*; use log::*; diff --git a/mumlib/src/state.rs b/mumlib/src/state.rs index 96598de..b09726e 100644 --- a/mumlib/src/state.rs +++ b/mumlib/src/state.rs @@ -1,6 +1,6 @@ +use serde::export::Formatter; use serde::{Deserialize, Serialize}; use std::fmt::Display; -use serde::export::Formatter; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Server { @@ -24,17 +24,25 @@ impl Channel { pub fn iter(&self) -> Iter<'_> { Iter { me: Some(&self), - channel: if self.children.len() > 0 { Some(0) } else { None }, - channels: self.children.iter().map(|e| e.iter()).collect() + channel: if self.children.len() > 0 { + Some(0) + } else { + None + }, + channels: self.children.iter().map(|e| e.iter()).collect(), } } pub fn users_iter(&self) -> UsersIter<'_> { UsersIter { channels: self.children.iter().map(|e| e.users_iter()).collect(), - channel: if self.children.len() > 0 { Some(0) } else { None }, + channel: if self.children.len() > 0 { + Some(0) + } else { + None + }, user: if self.users.len() > 0 { Some(0) } else { None }, - users: &self.users + users: &self.users, } } } @@ -125,4 +133,4 @@ impl Display for User { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.name) } -} \ No newline at end of file +} -- cgit v1.2.1