From 28263e6a5c1681bc75898f8dfdc9684c4314b3cb Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 23 Jun 2022 22:25:23 +0200 Subject: [PATCH] Remove dummy store --- src/main.rs | 2 +- src/store.rs | 2 - src/store/dummy.rs | 156 --------------------------------------------- src/ui.rs | 22 ++----- src/vault.rs | 1 + 5 files changed, 8 insertions(+), 175 deletions(-) delete mode 100644 src/store/dummy.rs diff --git a/src/main.rs b/src/main.rs index 3869b59..f6a299a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,7 @@ async fn main() -> anyhow::Result<()> { let mut terminal = Terminal::new()?; // terminal.set_measuring(true); - Ui::run(&mut terminal, logger, logger_rx).await?; + Ui::run(&mut terminal, vault.clone(), logger, logger_rx).await?; drop(terminal); // So the vault can print again vault.close().await; diff --git a/src/store.rs b/src/store.rs index 14c5867..5b96d46 100644 --- a/src/store.rs +++ b/src/store.rs @@ -1,5 +1,3 @@ -pub mod dummy; - use std::collections::HashMap; use std::fmt::Debug; use std::hash::Hash; diff --git a/src/store/dummy.rs b/src/store/dummy.rs deleted file mode 100644 index 17cb8db..0000000 --- a/src/store/dummy.rs +++ /dev/null @@ -1,156 +0,0 @@ -use std::collections::{HashMap, HashSet}; - -use async_trait::async_trait; -use chrono::{DateTime, TimeZone, Utc}; - -use super::{Msg, MsgStore, Path, Tree}; - -#[derive(Clone)] -pub struct DummyMsg { - id: usize, - parent: Option, - time: DateTime, - nick: String, - content: String, -} - -impl DummyMsg { - pub fn new(id: usize, nick: S, content: S) -> Self - where - S: Into, - { - Self { - id, - parent: None, - time: Utc.timestamp(0, 0), - nick: nick.into(), - content: content.into(), - } - } - - pub 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 parent(&self) -> Option { - self.parent - } - - 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, - children: HashMap>, -} - -impl DummyStore { - pub fn new() -> Self { - Self { - msgs: HashMap::new(), - children: HashMap::new(), - } - } - - pub fn msg(mut self, msg: DummyMsg) -> Self { - if let Some(parent) = msg.parent { - self.children.entry(parent).or_default().push(msg.id()); - } - self.msgs.insert(msg.id(), msg); - self - } - - fn collect_tree(&self, id: usize, result: &mut Vec) { - if let Some(msg) = self.msgs.get(&id) { - result.push(msg.clone()); - } - if let Some(children) = self.children.get(&id) { - for child in children { - self.collect_tree(*child, result); - } - } - } - - fn trees(&self) -> Vec { - let mut trees = HashSet::new(); - for m in self.msgs.values() { - match m.parent() { - Some(parent) if !self.msgs.contains_key(&parent) => { - trees.insert(parent); - } - Some(_) => {} - None => { - trees.insert(m.id()); - } - } - } - let mut trees: Vec = trees.into_iter().collect(); - trees.sort_unstable(); - trees - } -} - -#[async_trait] -impl MsgStore for DummyStore { - async fn path(&self, id: &usize) -> Path { - let mut id = *id; - let mut segments = vec![id]; - while let Some(parent) = self.msgs.get(&id).and_then(|msg| msg.parent) { - segments.push(parent); - id = parent; - } - segments.reverse(); - Path::new(segments) - } - - async fn tree(&self, root: &usize) -> Tree { - let mut msgs = vec![]; - self.collect_tree(*root, &mut msgs); - Tree::new(*root, msgs) - } - - async fn prev_tree(&self, tree: &usize) -> Option { - let trees = self.trees(); - trees - .iter() - .zip(trees.iter().skip(1)) - .find(|(_, t)| *t == tree) - .map(|(t, _)| *t) - } - - async fn next_tree(&self, tree: &usize) -> Option { - let trees = self.trees(); - trees - .iter() - .zip(trees.iter().skip(1)) - .find(|(t, _)| *t == tree) - .map(|(_, t)| *t) - } - - async fn first_tree(&self) -> Option { - self.trees().first().cloned() - } - - async fn last_tree(&self) -> Option { - self.trees().last().cloned() - } -} diff --git a/src/ui.rs b/src/ui.rs index 2d4a930..ef1845c 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -13,7 +13,7 @@ use toss::terminal::Terminal; use crate::chat::Chat; use crate::euph; use crate::logger::{LogMsg, Logger}; -use crate::store::dummy::{DummyMsg, DummyStore}; +use crate::vault::{EuphMsg, EuphVault, Vault}; #[derive(Debug)] pub enum UiEvent { @@ -33,10 +33,11 @@ enum Visible { pub struct Ui { event_tx: UnboundedSender, + vault: Vault, visible: Visible, room: euph::Room, - chat: Chat, + chat: Chat, log_chat: Chat, } @@ -45,6 +46,7 @@ impl Ui { pub async fn run( terminal: &mut Terminal, + vault: Vault, logger: Logger, logger_rx: mpsc::UnboundedReceiver<()>, ) -> anyhow::Result<()> { @@ -58,20 +60,7 @@ impl Ui { Self::poll_crossterm_events(event_tx_clone, weak_crossterm_lock) }); - // Prepare dummy message store and chat for testing - let store = DummyStore::new() - .msg(DummyMsg::new(1, "nick", "content")) - .msg(DummyMsg::new(2, "Some1Else", "reply").parent(1)) - .msg(DummyMsg::new(3, "Some1Else", "deeper reply").parent(2)) - .msg(DummyMsg::new(4, "abc123", "even deeper reply").parent(3)) - .msg(DummyMsg::new(5, "Some1Else", "another reply").parent(1)) - .msg(DummyMsg::new(6, "Some1Else", "third reply").parent(1)) - .msg(DummyMsg::new(8, "nick", "reply to nothing").parent(7)) - .msg(DummyMsg::new(9, "nick", "another reply to nothing").parent(7)) - .msg(DummyMsg::new(10, "abc123", "reply to reply to nothing").parent(8)) - .msg(DummyMsg::new(11, "nick", "yet another reply to nothing").parent(7)) - .msg(DummyMsg::new(12, "abc123", "beep\nboop").parent(11)); - let chat = Chat::new(store); + let chat = Chat::new(vault.euph("test".to_string())); // Run main UI. // @@ -84,6 +73,7 @@ impl Ui { // the rest of the UI is also shut down and the client stops. let mut ui = Self { event_tx: event_tx.clone(), + vault, visible: Visible::Log, room: euph::Room::new("test".to_string()), chat, diff --git a/src/vault.rs b/src/vault.rs index 3cc8c66..63a8a3a 100644 --- a/src/vault.rs +++ b/src/vault.rs @@ -15,6 +15,7 @@ enum Request { Euph(EuphRequest), } +#[derive(Debug, Clone)] pub struct Vault { tx: mpsc::Sender, }