use serde::{Serialize, Deserialize}; use chrono::Utc; #[derive(Deserialize, Serialize, Copy, Clone, Debug)] // Date is the seconds from UNIX_EPOCH pub struct Date(u64); impl Date { pub fn now() -> Date { // Get the current time in utc as seconds let time = Utc::now().timestamp().try_into().unwrap_or(0); Date( time ) } } impl std::convert::From for Date { fn from(t: i64) -> Date { if t < 0 { Date(0) } else { Date(t as u64) } } } impl std::convert::From for sqlite::Value { fn from(t: Date) -> sqlite::Value { (t.0 as i64).into() } } #[derive(Clone, Copy, Debug, Serialize, Deserialize)] pub enum Status { Offline, Online, Away, Busy, } impl std::convert::From for i64 { fn from(s: Status) -> i64 { match s { Status::Offline => 0, Status::Online => 1, Status::Away => 2, Status::Busy => 3, } } } impl std::convert::From for Status { fn from(u: i64) -> Status { match u { 1 => Status::Online, 2 => Status::Away, 3 => Status::Busy, _ => Status::Offline } } } #[derive(Deserialize, Copy, Clone)] pub enum DataField { Desc, StatusText, DisplayName, } impl std::convert::From for &str { fn from(f: DataField) -> &'static str { match f { DataField::Desc => "desc", DataField::StatusText => "statustext", DataField::DisplayName => "displayname", } } } impl std::fmt::Display for DataField { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { DataField::Desc => write!(f, "desc"), DataField::StatusText => write!(f, "statustext"), DataField::DisplayName => write!(f, "displayname"), } } } #[derive(Serialize, Clone, Debug)] pub struct User { username: String, //Limit 20 id: UID, deleted: bool, desc: String, // Limit 500 displayname: String, // Limit 30 statustext: String, //Limit 60 status: Status, } impl User { pub fn new(username: String, id: UID) -> User { User { username: username.clone(), id, deleted: false, desc: String::from(""), displayname: username, statustext: String::from(""), status: Status::Offline, } } pub fn construct(username: String, id: UID, deleted: bool, desc: String, displayname: String, statustext: String, status: i64) -> User { User { username, id, deleted, desc, displayname, statustext, status: Status::from(status), } } pub fn deleted(&self) -> bool { self.deleted } } #[derive(Deserialize, Serialize, Copy, Clone, Debug)] pub struct UID(u64); impl UID { pub fn new(id: u64) -> UID { UID(id) } } impl std::convert::From for UID { fn from(t: u64) -> UID { UID(t) } } impl std::convert::From for UID { fn from(t: i64) -> UID { if t < 0 { UID(0) } else { UID(t as u64) } } } impl std::convert::From for i64 { fn from(t :UID) -> i64 { t.0 as i64 } } impl std::convert::From for sqlite::Value { fn from(t: UID) -> sqlite::Value { (t.0 as i64).into() } } #[derive(Deserialize, Serialize, Clone, Debug)] pub struct Message { id: UID, sender: UID, message: String, reply_to: Option, deleted: bool, date: Date, } impl Message { pub fn construct(msg: String, sender: UID, id: UID, reply_to: Option, date: Date, deleted: bool) -> Message { Message { date: date, sender: sender, message: msg, id: id, reply_to: reply_to, deleted: deleted, } } pub fn id(&self) -> UID { self.id } pub fn sender(&self) -> UID { self.sender } pub fn message(&self) -> String { self.message.clone() } pub fn reply_to(&self) -> Option { self.reply_to } pub fn date(&self) -> Date { self.date } pub fn set_message(&mut self, s: String) { self.message = s; } } #[derive(Serialize)] pub struct Info { name: String, version: &'static str, users: u64, } impl Info { pub fn get(db: std::sync::MutexGuard) -> Info { Info{ name: String::from("Testing"), version: option_env!("CARGO_PKG_VERSION").unwrap(), users: db.get_user_count(), } } } #[derive(Deserialize, Serialize)] pub struct ReceiveMessage { sender: UID, message: String, reply_to: Option, } impl ReceiveMessage { pub fn fill(&self, id: UID) -> Message { Message { id, sender: self.sender, message: self.message.clone(), reply_to: self.reply_to, deleted: false, date: Date::now(), } } } #[derive(Deserialize, Clone)] pub struct ReceiveUpdateField {pub user: UID, pub field: DataField, pub data: String} #[derive(Deserialize, Clone)] pub struct ReceiveStatus {pub user: UID, pub status: Status}