From 6fa328db646a0e1c33b883d387ad95ec971cf2e0 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Wed, 19 May 2021 00:19:40 +0200 Subject: refactor getting of channel data from name --- mumd/src/state/server.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'mumd/src/state') diff --git a/mumd/src/state/server.rs b/mumd/src/state/server.rs index c9f8a69..e44d1e8 100644 --- a/mumd/src/state/server.rs +++ b/mumd/src/state/server.rs @@ -3,6 +3,7 @@ use crate::state::user::User; use log::*; use mumble_protocol::control::msgs; +use mumlib::error::ChannelIdentifierError; use serde::{Deserialize, Serialize}; use std::collections::hash_map::Entry; use std::collections::HashMap; @@ -88,6 +89,44 @@ impl Server { &self.channels } + /// Takes a channel name and returns either a tuple with the channel id and a reference to the + /// channel struct if the channel name unambiguosly refers to a channel, or an error describing + /// if the channel identifier was ambigous or invalid. + /*/// note that doctests currently aren't run in binary crates yet (see #50784) + /// ``` + /// use crate::state::channel::Channel; + /// let mut server = Server::new(); + /// let channel = Channel { + /// name: "Foobar".to_owned(), + /// ..Default::default(), + /// }; + /// server.channels.insert(0, channel.clone); + /// assert_eq!(server.channel_name("Foobar"), Ok((0, &channel))); + /// ```*/ + pub fn channel_name(&self, channel_name: &str) -> Result<(u32, &Channel), ChannelIdentifierError> { + let matches = self.channels + .iter() + .map(|e| ((*e.0, e.1), e.1.path(&self.channels))) + .filter(|e| e.1.ends_with(channel_name)) + .collect::>(); + Ok(match matches.len() { + 0 => { + let soft_matches = self.channels + .iter() + .map(|e| ((*e.0, e.1), e.1.path(&self.channels).to_lowercase())) + .filter(|e| e.1.ends_with(&channel_name.to_lowercase())) + .collect::>(); + match soft_matches.len() { + 0 => return Err(ChannelIdentifierError::Invalid), + 1 => soft_matches.get(0).unwrap().0, + _ => return Err(ChannelIdentifierError::Ambiguous), + } + } + 1 => matches.get(0).unwrap().0, + _ => return Err(ChannelIdentifierError::Ambiguous), + }) + } + pub fn host_mut(&mut self) -> &mut Option { &mut self.host } -- cgit v1.2.1 From 7ac57f3803bcf0f357ee307a6f0daf0783efbf92 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Wed, 19 May 2021 00:20:33 +0200 Subject: add backend support for sending messages --- mumd/src/state/channel.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mumd/src/state') diff --git a/mumd/src/state/channel.rs b/mumd/src/state/channel.rs index 5b6d669..f58ed15 100644 --- a/mumd/src/state/channel.rs +++ b/mumd/src/state/channel.rs @@ -4,7 +4,7 @@ use mumble_protocol::control::msgs; use serde::{Deserialize, Serialize}; use std::collections::HashMap; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] pub struct Channel { description: Option, links: Vec, -- cgit v1.2.1 From 55a12fbdfb435886b2f211fe1fb00daafb32b6a7 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sun, 6 Jun 2021 23:17:39 +0200 Subject: unhide doctests --- mumd/src/state/server.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'mumd/src/state') diff --git a/mumd/src/state/server.rs b/mumd/src/state/server.rs index e44d1e8..78a10b9 100644 --- a/mumd/src/state/server.rs +++ b/mumd/src/state/server.rs @@ -92,7 +92,7 @@ impl Server { /// Takes a channel name and returns either a tuple with the channel id and a reference to the /// channel struct if the channel name unambiguosly refers to a channel, or an error describing /// if the channel identifier was ambigous or invalid. - /*/// note that doctests currently aren't run in binary crates yet (see #50784) + /// note that doctests currently aren't run in binary crates yet (see #50784) /// ``` /// use crate::state::channel::Channel; /// let mut server = Server::new(); @@ -102,7 +102,7 @@ impl Server { /// }; /// server.channels.insert(0, channel.clone); /// assert_eq!(server.channel_name("Foobar"), Ok((0, &channel))); - /// ```*/ + /// ``` pub fn channel_name(&self, channel_name: &str) -> Result<(u32, &Channel), ChannelIdentifierError> { let matches = self.channels .iter() -- cgit v1.2.1