use std::collections::BTreeMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Mutex, PoisonError};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use super::{ChannelAnswer, ChannelInbound, ChannelNotice, ChannelSink, DecisionCard, StopNotice};
use crate::error::Result;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", rename_all_fields = "camelCase", tag = "kind")]
#[non_exhaustive]
pub enum Delivered {
Message {
text: String,
},
Decision {
card: DecisionCard,
},
Stopped {
notice: StopNotice,
},
Notice {
notice: ChannelNotice,
},
}
#[derive(Debug)]
pub struct InMemoryChannel {
name: String,
threads: Mutex<BTreeMap<String, Vec<Delivered>>>,
next_message: AtomicU64,
}
impl InMemoryChannel {
pub fn new(name: impl Into<String>) -> Self {
Self { name: name.into(), threads: Mutex::default(), next_message: AtomicU64::new(1) }
}
fn push(&self, thread: &str, delivered: Delivered) {
self.threads
.lock()
.unwrap_or_else(PoisonError::into_inner)
.entry(thread.to_string())
.or_default()
.push(delivered);
}
pub fn delivered(&self, thread: &str) -> Vec<Delivered> {
self.threads
.lock()
.unwrap_or_else(PoisonError::into_inner)
.get(thread)
.cloned()
.unwrap_or_default()
}
pub fn wire(&self, thread: &str) -> String {
self.delivered(thread)
.iter()
.map(|delivered| serde_json::to_string(delivered).unwrap_or_default())
.collect::<Vec<_>>()
.join("\n")
}
pub fn messages(&self, thread: &str) -> Vec<String> {
self.delivered(thread)
.into_iter()
.filter_map(|delivered| match delivered {
Delivered::Message { text } => Some(text),
_ => None,
})
.collect()
}
pub fn notices(&self, thread: &str) -> Vec<ChannelNotice> {
self.delivered(thread)
.into_iter()
.filter_map(|delivered| match delivered {
Delivered::Notice { notice } => Some(notice),
_ => None,
})
.collect()
}
pub fn last_card(&self, thread: &str) -> Option<DecisionCard> {
self.delivered(thread).into_iter().rev().find_map(|delivered| match delivered {
Delivered::Decision { card } => Some(card),
_ => None,
})
}
pub fn pairing_code(&self, thread: &str) -> Option<String> {
self.notices(thread).into_iter().rev().find_map(|notice| match notice {
ChannelNotice::PairingCode { code, .. } => Some(code),
_ => None,
})
}
pub fn clear(&self, thread: &str) {
self.threads.lock().unwrap_or_else(PoisonError::into_inner).remove(thread);
}
fn message_id(&self) -> String {
format!("m{}", self.next_message.fetch_add(1, Ordering::SeqCst))
}
pub fn text(&self, account: &str, thread: &str, text: &str) -> ChannelInbound {
ChannelInbound::text(&self.name, account, thread, self.message_id(), text)
}
pub fn press(
&self,
account: &str,
thread: &str,
card: &DecisionCard,
answer: ChannelAnswer,
) -> Option<ChannelInbound> {
card.answers.contains(&answer).then(|| {
ChannelInbound::answer(&self.name, account, thread, self.message_id(), card, answer)
})
}
}
#[async_trait]
impl ChannelSink for InMemoryChannel {
fn channel(&self) -> &str {
&self.name
}
async fn send_message(&self, thread: &str, text: &str) -> Result<()> {
self.push(thread, Delivered::Message { text: text.to_string() });
Ok(())
}
async fn send_decision(&self, thread: &str, card: &DecisionCard) -> Result<()> {
self.push(thread, Delivered::Decision { card: card.clone() });
Ok(())
}
async fn send_stopped(&self, thread: &str, notice: &StopNotice) -> Result<()> {
self.push(thread, Delivered::Stopped { notice: notice.clone() });
Ok(())
}
async fn send_notice(&self, thread: &str, notice: &ChannelNotice) -> Result<()> {
self.push(thread, Delivered::Notice { notice: notice.clone() });
Ok(())
}
}