use async_std::sync::Mutex; use std::{ collections::HashSet, io, sync::Arc, }; use drk::{ net, serial::{Decodable, Encodable}, Result, }; pub type PrivMsgId = u32; #[derive(Debug, Clone)] pub struct PrivMsg { pub id: PrivMsgId, pub nickname: String, pub channel: String, pub message: String, } impl net::Message for PrivMsg { fn name() -> &'static str { "privmsg" } } impl Encodable for PrivMsg { fn encode(&self, mut s: S) -> Result { let mut len = 0; len += self.id.encode(&mut s)?; len += self.nickname.encode(&mut s)?; len += self.channel.encode(&mut s)?; len += self.message.encode(&mut s)?; Ok(len) } } impl Decodable for PrivMsg { fn decode(mut d: D) -> Result { Ok(Self { id: Decodable::decode(&mut d)?, nickname: Decodable::decode(&mut d)?, channel: Decodable::decode(&mut d)?, message: Decodable::decode(&mut d)?, }) } } pub struct SeenPrivMsgIds { privmsg_ids: Mutex>, } pub type SeenPrivMsgIdsPtr = Arc; impl SeenPrivMsgIds { pub fn new() -> Arc { Arc::new(Self { privmsg_ids: Mutex::new(HashSet::new()) }) } pub async fn add_seen(&self, id: u32) { self.privmsg_ids.lock().await.insert(id); } pub async fn is_seen(&self, id: u32) -> bool { self.privmsg_ids.lock().await.contains(&id) } }