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(Serialize, Clone, Debug)] pub struct User { username: String, id: UID, deleted: bool, } impl User { pub fn new(username: String, id: UID) -> User { User { username, id, deleted: false, } } pub fn construct(username: String, id: UID, deleted: bool) -> User { User { username, id, deleted, } } 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(), } } }