use std::fmt;
use gtk::glib;
use matrix_sdk::RoomState;
use crate::session::model::CategoryType;
#[derive(Debug, Default, Hash, Eq, PartialEq, Clone, Copy, glib::Enum)]
#[repr(u32)]
#[enum_type(name = "RoomType")]
pub enum RoomType {
Invited = 0,
Favorite = 1,
#[default]
Normal = 2,
LowPriority = 3,
Left = 4,
Outdated = 5,
Space = 6,
Ignored = 7,
}
impl RoomType {
pub fn can_change_to(&self, category: RoomType) -> bool {
match self {
Self::Invited => {
matches!(
category,
Self::Favorite | Self::Normal | Self::LowPriority | Self::Left
)
}
Self::Favorite => {
matches!(category, Self::Normal | Self::LowPriority | Self::Left)
}
Self::Normal => {
matches!(category, Self::Favorite | Self::LowPriority | Self::Left)
}
Self::LowPriority => {
matches!(category, Self::Favorite | Self::Normal | Self::Left)
}
Self::Left => {
matches!(category, Self::Favorite | Self::Normal | Self::LowPriority)
}
Self::Ignored | Self::Outdated | Self::Space => false,
}
}
pub fn is_state(&self, state: RoomState) -> bool {
match self {
RoomType::Invited | RoomType::Ignored => state == RoomState::Invited,
RoomType::Favorite
| RoomType::Normal
| RoomType::LowPriority
| RoomType::Outdated
| RoomType::Space => state == RoomState::Joined,
RoomType::Left => state == RoomState::Left,
}
}
}
impl fmt::Display for RoomType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
CategoryType::from(self).fmt(f)
}
}
impl TryFrom<CategoryType> for RoomType {
type Error = &'static str;
fn try_from(category_type: CategoryType) -> Result<Self, Self::Error> {
Self::try_from(&category_type)
}
}
impl TryFrom<&CategoryType> for RoomType {
type Error = &'static str;
fn try_from(category_type: &CategoryType) -> Result<Self, Self::Error> {
match category_type {
CategoryType::None => Err("CategoryType::None cannot be a RoomType"),
CategoryType::Invited => Ok(Self::Invited),
CategoryType::Favorite => Ok(Self::Favorite),
CategoryType::Normal => Ok(Self::Normal),
CategoryType::LowPriority => Ok(Self::LowPriority),
CategoryType::Left => Ok(Self::Left),
CategoryType::Outdated => Ok(Self::Outdated),
CategoryType::VerificationRequest => {
Err("CategoryType::VerificationRequest cannot be a RoomType")
}
CategoryType::Space => Ok(Self::Space),
CategoryType::Ignored => Ok(Self::Ignored),
}
}
}