diff options
| -rw-r--r-- | mumd/src/state.rs | 15 | ||||
| -rw-r--r-- | mumlib/src/error.rs | 22 |
2 files changed, 28 insertions, 9 deletions
diff --git a/mumd/src/state.rs b/mumd/src/state.rs index d3f9a8e..623cfba 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -9,7 +9,7 @@ use mumlib::command::{Command, CommandResponse}; use mumlib::state::Server; use std::net::ToSocketAddrs; use tokio::sync::{mpsc, watch}; -use mumlib::error::Error; +use mumlib::error::{Error, ChannelIdentifierError}; #[derive(Clone, Debug, Eq, PartialEq)] pub enum StatePhase { @@ -61,12 +61,15 @@ impl State { let channels = self.server() .unwrap() .channels(); - let mut idents = channels.iter() - .map(|e| (e.0, e.1.path(channels))); - let id = match idents.find(|e| e.1.ends_with(&channel_identifier)) { - Some(v) => *v.0, - None => return (false, Err(Error::InvalidChannelIdentifierError(channel_identifier))), + let matches = channels.iter() + .map(|e| (e.0, e.1.path(channels))) + .filter(|e| e.1.ends_with(&channel_identifier)) + .collect::<Vec<_>>(); + let id = match matches.len() { + 0 => return (false, Err(Error::ChannelIdentifierError(channel_identifier, ChannelIdentifierError::Invalid))), + 1 => *matches.get(0).unwrap().0, + _ => return (false, Err(Error::ChannelIdentifierError(channel_identifier, ChannelIdentifierError::Ambiguous))), }; let mut msg = msgs::UserState::new(); diff --git a/mumlib/src/error.rs b/mumlib/src/error.rs index 3e514fd..6c66c1f 100644 --- a/mumlib/src/error.rs +++ b/mumlib/src/error.rs @@ -8,17 +8,33 @@ pub type Result<T> = std::result::Result<T, Error>; pub enum Error { DisconnectedError, AlreadyConnectedError, - InvalidChannelIdentifierError(String), + ChannelIdentifierError(String, ChannelIdentifierError), InvalidServerAddrError(String, u16), } + impl Display for Error { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { Error::DisconnectedError => write!(f, "Not connected to a server"), Error::AlreadyConnectedError => write!(f, "Already connected to a server"), - Error::InvalidChannelIdentifierError(id) => write!(f, "Couldn't find channel {}", id), - Error::InvalidServerAddrError(addr, port) => write!(f, "Invalid server address: {}:{}", addr, port), + Error::ChannelIdentifierError(id, kind) => write!(f, "{}: {}", kind, id), + Error::InvalidServerAddrError(addr, port) => write!(f, "Invalid server address: {}: {}", addr, port), + } + } +} + +#[derive(Debug, Serialize, Deserialize)] +pub enum ChannelIdentifierError { + Invalid, + Ambiguous, +} + +impl Display for ChannelIdentifierError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + ChannelIdentifierError::Invalid => write!(f, "Invalid channel identifier"), + ChannelIdentifierError::Ambiguous => write!(f, "Ambiguous channel identifier"), } } }
\ No newline at end of file |
