Add dummy store

This commit is contained in:
Joscha 2022-06-13 09:57:27 +02:00
parent 0be5c7f67a
commit 4b28b0e3b2
3 changed files with 89 additions and 1 deletions

View file

@ -1,5 +1,6 @@
#![warn(clippy::use_self)]
mod chat;
mod traits;
mod ui;

87
cove-tui/src/store.rs Normal file
View file

@ -0,0 +1,87 @@
use std::collections::HashMap;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use crate::traits::{Msg, MsgStore};
pub struct DummyMsg {
id: usize,
parent: Option<usize>,
time: DateTime<Utc>,
nick: String,
content: String,
}
impl DummyMsg {
fn new<T, S>(id: usize, time: T, nick: S, content: S) -> Self
where
T: Into<DateTime<Utc>>,
S: Into<String>,
{
Self {
id,
parent: None,
time: time.into(),
nick: nick.into(),
content: content.into(),
}
}
fn parent(mut self, parent: usize) -> Self {
self.parent = Some(parent);
self
}
}
impl Msg for DummyMsg {
type Id = usize;
fn id(&self) -> Self::Id {
self.id
}
fn time(&self) -> DateTime<Utc> {
self.time
}
fn nick(&self) -> String {
self.nick.clone()
}
fn content(&self) -> String {
self.content.clone()
}
}
pub struct DummyStore {
msgs: HashMap<usize, DummyMsg>,
}
impl DummyStore {
fn new() -> Self {
Self {
msgs: HashMap::new(),
}
}
fn msg(mut self, msg: DummyMsg) -> Self {
self.msgs.insert(msg.id(), msg);
self
}
}
#[async_trait]
impl MsgStore<DummyMsg> for DummyStore {
async fn path(&self, _room: &str, mut id: usize) -> Vec<usize> {
let mut path = vec![id];
while let Some(parent) = self.msgs.get(&id).and_then(|msg| msg.parent) {
path.push(parent);
id = parent;
}
path.reverse();
path
}
}

View file

@ -12,5 +12,5 @@ pub trait Msg {
#[async_trait]
pub trait MsgStore<M: Msg> {
async fn path(room: &str, id: M::Id) -> Vec<M::Id>;
async fn path(&self, room: &str, id: M::Id) -> Vec<M::Id>;
}