use crate::{IntoOwned, MaybeOwned, MaybeOwnedIndex};
pub struct Prefix<'a> {
pub(crate) data: &'a MaybeOwned<'a>,
pub(crate) index: PrefixIndex,
}
impl<'a> std::fmt::Debug for Prefix<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.data[self.index.as_index()].fmt(f)
}
}
impl<'a> Prefix<'a> {
pub fn is_server(&self) -> bool {
!self.is_user()
}
pub fn is_user(&self) -> bool {
matches!(self.index, PrefixIndex::User{ .. })
}
pub fn get_raw_prefix(&self) -> &'a str {
&self.data[self.index.as_index()]
}
pub fn get_nick(&self) -> Option<&'a str> {
self.index.nick_index().map(|index| &self.data[index])
}
}
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum PrefixIndex {
User {
nick: MaybeOwnedIndex,
},
Server {
host: MaybeOwnedIndex,
},
}
impl PrefixIndex {
pub fn is_server(&self) -> bool {
!self.is_nick()
}
pub fn is_nick(&self) -> bool {
matches!(self, Self::User{ .. })
}
pub fn nick_index(self) -> Option<MaybeOwnedIndex> {
match self {
Self::User { nick } => Some(nick),
Self::Server { .. } => None,
}
}
pub fn host_index(self) -> Option<MaybeOwnedIndex> {
match self {
Self::Server { host } => Some(host),
Self::User { .. } => None,
}
}
pub fn as_index(self) -> MaybeOwnedIndex {
match self {
Self::User { nick } => nick,
Self::Server { host } => host,
}
}
}
impl IntoOwned<'static> for PrefixIndex {
type Output = Self;
fn into_owned(self) -> Self::Output {
self
}
}