Remove dummy store

This commit is contained in:
Joscha 2022-06-23 22:25:23 +02:00
parent e0db158ece
commit 28263e6a5c
5 changed files with 8 additions and 175 deletions

View file

@ -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;

View file

@ -1,5 +1,3 @@
pub mod dummy;
use std::collections::HashMap;
use std::fmt::Debug;
use std::hash::Hash;

View file

@ -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<usize>,
time: DateTime<Utc>,
nick: String,
content: String,
}
impl DummyMsg {
pub fn new<S>(id: usize, nick: S, content: S) -> Self
where
S: Into<String>,
{
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::Id> {
self.parent
}
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>,
children: HashMap<usize, Vec<usize>>,
}
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<DummyMsg>) {
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<usize> {
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<usize> = trees.into_iter().collect();
trees.sort_unstable();
trees
}
}
#[async_trait]
impl MsgStore<DummyMsg> for DummyStore {
async fn path(&self, id: &usize) -> Path<usize> {
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<DummyMsg> {
let mut msgs = vec![];
self.collect_tree(*root, &mut msgs);
Tree::new(*root, msgs)
}
async fn prev_tree(&self, tree: &usize) -> Option<usize> {
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<usize> {
let trees = self.trees();
trees
.iter()
.zip(trees.iter().skip(1))
.find(|(t, _)| *t == tree)
.map(|(_, t)| *t)
}
async fn first_tree(&self) -> Option<usize> {
self.trees().first().cloned()
}
async fn last_tree(&self) -> Option<usize> {
self.trees().last().cloned()
}
}

View file

@ -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<UiEvent>,
vault: Vault,
visible: Visible,
room: euph::Room,
chat: Chat<DummyMsg, DummyStore>,
chat: Chat<EuphMsg, EuphVault>,
log_chat: Chat<LogMsg, Logger>,
}
@ -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,

View file

@ -15,6 +15,7 @@ enum Request {
Euph(EuphRequest),
}
#[derive(Debug, Clone)]
pub struct Vault {
tx: mpsc::Sender<Request>,
}