use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::fmt;
use crate::{Tag, Timestamp};
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum StatusType {
#[default]
General,
Music,
Custom(String),
}
impl fmt::Display for StatusType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl StatusType {
pub fn as_str(&self) -> &str {
match self {
Self::General => "general",
Self::Music => "music",
Self::Custom(s) => s.as_str(),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LiveStatus {
pub status_type: StatusType,
pub expiration: Option<Timestamp>,
pub reference: Option<String>,
}
impl LiveStatus {
#[inline]
pub fn new(status_type: StatusType) -> Self {
Self {
status_type,
expiration: None,
reference: None,
}
}
}
impl From<LiveStatus> for Vec<Tag> {
fn from(
LiveStatus {
status_type,
expiration,
reference,
}: LiveStatus,
) -> Self {
let mut tags =
Vec::with_capacity(1 + expiration.is_some() as usize + reference.is_some() as usize);
tags.push(Tag::identifier(status_type.to_string()));
if let Some(expire_at) = expiration {
tags.push(Tag::expiration(expire_at));
}
if let Some(content) = reference {
tags.push(Tag::new(vec![String::from("r"), content]));
}
tags
}
}