From 4b28b0e3b2e123693cf33404cd5f13019cc3805a Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 13 Jun 2022 09:57:27 +0200 Subject: [PATCH] Add dummy store --- cove-tui/src/main.rs | 1 + cove-tui/src/store.rs | 87 ++++++++++++++++++++++++++++++++++++++++++ cove-tui/src/traits.rs | 2 +- 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 cove-tui/src/store.rs diff --git a/cove-tui/src/main.rs b/cove-tui/src/main.rs index 5c1f7bf..f096e69 100644 --- a/cove-tui/src/main.rs +++ b/cove-tui/src/main.rs @@ -1,5 +1,6 @@ #![warn(clippy::use_self)] +mod chat; mod traits; mod ui; diff --git a/cove-tui/src/store.rs b/cove-tui/src/store.rs new file mode 100644 index 0000000..d703953 --- /dev/null +++ b/cove-tui/src/store.rs @@ -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, + time: DateTime, + nick: String, + content: String, +} + +impl DummyMsg { + fn new(id: usize, time: T, nick: S, content: S) -> Self + where + T: Into>, + S: Into, + { + 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 { + self.time + } + + fn nick(&self) -> String { + self.nick.clone() + } + + fn content(&self) -> String { + self.content.clone() + } +} + +pub struct DummyStore { + msgs: HashMap, +} + +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 for DummyStore { + async fn path(&self, _room: &str, mut id: usize) -> Vec { + 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 + } +} diff --git a/cove-tui/src/traits.rs b/cove-tui/src/traits.rs index fcfebe3..3f1b0f9 100644 --- a/cove-tui/src/traits.rs +++ b/cove-tui/src/traits.rs @@ -12,5 +12,5 @@ pub trait Msg { #[async_trait] pub trait MsgStore { - async fn path(room: &str, id: M::Id) -> Vec; + async fn path(&self, room: &str, id: M::Id) -> Vec; }