Rename some methods for consistency

This commit is contained in:
Joscha 2022-07-24 17:44:05 +02:00
parent ee216d407e
commit 282609916a
6 changed files with 71 additions and 57 deletions

View file

@ -23,11 +23,11 @@ pub async fn export(vault: &Vault, room: String, file: &Path) -> anyhow::Result<
let mut exported_trees = 0; let mut exported_trees = 0;
let mut exported_msgs = 0; let mut exported_msgs = 0;
let mut tree_id = vault.first_tree().await; let mut tree_id = vault.first_tree_id().await;
while let Some(some_tree_id) = tree_id { while let Some(some_tree_id) = tree_id {
let tree = vault.tree(&some_tree_id).await; let tree = vault.tree(&some_tree_id).await;
write_tree(&mut file, &tree, some_tree_id, 0)?; write_tree(&mut file, &tree, some_tree_id, 0)?;
tree_id = vault.next_tree(&some_tree_id).await; tree_id = vault.next_tree_id(&some_tree_id).await;
exported_trees += 1; exported_trees += 1;
exported_msgs += tree.len(); exported_msgs += tree.len();

View file

@ -77,21 +77,21 @@ impl MsgStore<LogMsg> for Logger {
Tree::new(*root, msgs) Tree::new(*root, msgs)
} }
async fn prev_tree(&self, tree: &usize) -> Option<usize> { async fn prev_tree_id(&self, tree: &usize) -> Option<usize> {
tree.checked_sub(1) tree.checked_sub(1)
} }
async fn next_tree(&self, tree: &usize) -> Option<usize> { async fn next_tree_id(&self, tree: &usize) -> Option<usize> {
let len = self.messages.lock().len(); let len = self.messages.lock().len();
tree.checked_add(1).filter(|t| *t < len) tree.checked_add(1).filter(|t| *t < len)
} }
async fn first_tree(&self) -> Option<usize> { async fn first_tree_id(&self) -> Option<usize> {
let empty = self.messages.lock().is_empty(); let empty = self.messages.lock().is_empty();
Some(0).filter(|_| !empty) Some(0).filter(|_| !empty)
} }
async fn last_tree(&self) -> Option<usize> { async fn last_tree_id(&self) -> Option<usize> {
self.messages.lock().len().checked_sub(1) self.messages.lock().len().checked_sub(1)
} }
} }

View file

@ -142,8 +142,8 @@ impl<M: Msg> Tree<M> {
pub trait MsgStore<M: Msg> { pub trait MsgStore<M: Msg> {
async fn path(&self, id: &M::Id) -> Path<M::Id>; async fn path(&self, id: &M::Id) -> Path<M::Id>;
async fn tree(&self, root: &M::Id) -> Tree<M>; async fn tree(&self, root: &M::Id) -> Tree<M>;
async fn prev_tree(&self, tree: &M::Id) -> Option<M::Id>; async fn prev_tree_id(&self, tree: &M::Id) -> Option<M::Id>;
async fn next_tree(&self, tree: &M::Id) -> Option<M::Id>; async fn next_tree_id(&self, tree: &M::Id) -> Option<M::Id>;
async fn first_tree(&self) -> Option<M::Id>; async fn first_tree_id(&self) -> Option<M::Id>;
async fn last_tree(&self) -> Option<M::Id>; async fn last_tree_id(&self) -> Option<M::Id>;
} }

View file

@ -85,7 +85,7 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
} else if tree.parent(id).is_none() { } else if tree.parent(id).is_none() {
// We're at the root of our tree, so we need to move to the root of // We're at the root of our tree, so we need to move to the root of
// the previous tree. // the previous tree.
if let Some(prev_tree_id) = store.prev_tree(tree.root()).await { if let Some(prev_tree_id) = store.prev_tree_id(tree.root()).await {
*tree = store.tree(&prev_tree_id).await; *tree = store.tree(&prev_tree_id).await;
*id = prev_tree_id; *id = prev_tree_id;
true true
@ -107,7 +107,7 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
} else if tree.parent(id).is_none() { } else if tree.parent(id).is_none() {
// We're at the root of our tree, so we need to move to the root of // We're at the root of our tree, so we need to move to the root of
// the next tree. // the next tree.
if let Some(next_tree_id) = store.next_tree(tree.root()).await { if let Some(next_tree_id) = store.next_tree_id(tree.root()).await {
*tree = store.tree(&next_tree_id).await; *tree = store.tree(&next_tree_id).await;
*id = next_tree_id; *id = next_tree_id;
true true
@ -157,7 +157,7 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
pub async fn move_cursor_up(&mut self) { pub async fn move_cursor_up(&mut self) {
match &mut self.cursor { match &mut self.cursor {
Cursor::Bottom => { Cursor::Bottom => {
if let Some(last_tree_id) = self.store.last_tree().await { if let Some(last_tree_id) = self.store.last_tree_id().await {
let tree = self.store.tree(&last_tree_id).await; let tree = self.store.tree(&last_tree_id).await;
let mut id = last_tree_id; let mut id = last_tree_id;
while Self::find_last_child(&tree, &mut id) {} while Self::find_last_child(&tree, &mut id) {}
@ -186,8 +186,8 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
} }
pub async fn move_cursor_to_top(&mut self) { pub async fn move_cursor_to_top(&mut self) {
if let Some(tree_id) = self.store.first_tree().await { if let Some(first_tree_id) = self.store.first_tree_id().await {
self.cursor = Cursor::Msg(tree_id); self.cursor = Cursor::Msg(first_tree_id);
self.make_cursor_visible = true; self.make_cursor_visible = true;
} }
} }

View file

@ -11,7 +11,7 @@ use super::{util, InnerTreeViewState};
impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> { impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
async fn cursor_path(&self, cursor: &Cursor<M::Id>) -> Path<M::Id> { async fn cursor_path(&self, cursor: &Cursor<M::Id>) -> Path<M::Id> {
match cursor { match cursor {
Cursor::Bottom => match self.store.last_tree().await { Cursor::Bottom => match self.store.last_tree_id().await {
Some(id) => Path::new(vec![id]), Some(id) => Path::new(vec![id]),
None => Path::new(vec![M::last_possible_id()]), None => Path::new(vec![M::last_possible_id()]),
}, },
@ -217,9 +217,9 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
async fn expand_blocks_up(&self, frame: &mut Frame, blocks: &mut Blocks<M::Id>) { async fn expand_blocks_up(&self, frame: &mut Frame, blocks: &mut Blocks<M::Id>) {
while blocks.top_line > 0 { while blocks.top_line > 0 {
let tree_id = if let Some((root_top, _)) = &blocks.roots { let tree_id = if let Some((root_top, _)) = &blocks.roots {
self.store.prev_tree(root_top).await self.store.prev_tree_id(root_top).await
} else { } else {
self.store.last_tree().await self.store.last_tree_id().await
}; };
if let Some(tree_id) = tree_id { if let Some(tree_id) = tree_id {
@ -234,7 +234,7 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
async fn expand_blocks_down(&self, frame: &mut Frame, blocks: &mut Blocks<M::Id>) { async fn expand_blocks_down(&self, frame: &mut Frame, blocks: &mut Blocks<M::Id>) {
while blocks.bottom_line < frame.size().height as i32 { while blocks.bottom_line < frame.size().height as i32 {
let tree_id = if let Some((_, root_bot)) = &blocks.roots { let tree_id = if let Some((_, root_bot)) = &blocks.roots {
self.store.next_tree(root_bot).await self.store.next_tree_id(root_bot).await
} else { } else {
// We assume that a Blocks without roots is at the bottom of the // We assume that a Blocks without roots is at the bottom of the
// room's history. Therefore, there are no more messages below. // room's history. Therefore, there are no more messages below.

View file

@ -88,7 +88,7 @@ impl Vault {
pub async fn euph_rooms(&self) -> Vec<String> { pub async fn euph_rooms(&self) -> Vec<String> {
// TODO vault::Error // TODO vault::Error
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
let request = EuphRequest::Rooms { result: tx }; let request = EuphRequest::GetRooms { result: tx };
let _ = self.tx.send(request.into()); let _ = self.tx.send(request.into());
rx.await.unwrap() rx.await.unwrap()
} }
@ -139,7 +139,7 @@ impl EuphVault {
pub async fn last_span(&self) -> Option<(Option<Snowflake>, Option<Snowflake>)> { pub async fn last_span(&self) -> Option<(Option<Snowflake>, Option<Snowflake>)> {
// TODO vault::Error // TODO vault::Error
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
let request = EuphRequest::LastSpan { let request = EuphRequest::GetLastSpan {
room: self.room.clone(), room: self.room.clone(),
result: tx, result: tx,
}; };
@ -153,7 +153,7 @@ impl MsgStore<EuphMsg> for EuphVault {
async fn path(&self, id: &Snowflake) -> Path<Snowflake> { async fn path(&self, id: &Snowflake) -> Path<Snowflake> {
// TODO vault::Error // TODO vault::Error
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
let request = EuphRequest::Path { let request = EuphRequest::GetPath {
room: self.room.clone(), room: self.room.clone(),
id: *id, id: *id,
result: tx, result: tx,
@ -165,7 +165,7 @@ impl MsgStore<EuphMsg> for EuphVault {
async fn tree(&self, root: &Snowflake) -> Tree<EuphMsg> { async fn tree(&self, root: &Snowflake) -> Tree<EuphMsg> {
// TODO vault::Error // TODO vault::Error
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
let request = EuphRequest::Tree { let request = EuphRequest::GetTree {
room: self.room.clone(), room: self.room.clone(),
root: *root, root: *root,
result: tx, result: tx,
@ -174,10 +174,10 @@ impl MsgStore<EuphMsg> for EuphVault {
rx.await.unwrap() rx.await.unwrap()
} }
async fn prev_tree(&self, root: &Snowflake) -> Option<Snowflake> { async fn prev_tree_id(&self, root: &Snowflake) -> Option<Snowflake> {
// TODO vault::Error // TODO vault::Error
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
let request = EuphRequest::PrevTree { let request = EuphRequest::GetPrevTreeId {
room: self.room.clone(), room: self.room.clone(),
root: *root, root: *root,
result: tx, result: tx,
@ -186,10 +186,10 @@ impl MsgStore<EuphMsg> for EuphVault {
rx.await.unwrap() rx.await.unwrap()
} }
async fn next_tree(&self, root: &Snowflake) -> Option<Snowflake> { async fn next_tree_id(&self, root: &Snowflake) -> Option<Snowflake> {
// TODO vault::Error // TODO vault::Error
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
let request = EuphRequest::NextTree { let request = EuphRequest::GetNextTreeId {
room: self.room.clone(), room: self.room.clone(),
root: *root, root: *root,
result: tx, result: tx,
@ -198,10 +198,10 @@ impl MsgStore<EuphMsg> for EuphVault {
rx.await.unwrap() rx.await.unwrap()
} }
async fn first_tree(&self) -> Option<Snowflake> { async fn first_tree_id(&self) -> Option<Snowflake> {
// TODO vault::Error // TODO vault::Error
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
let request = EuphRequest::FirstTree { let request = EuphRequest::GetFirstTreeId {
room: self.room.clone(), room: self.room.clone(),
result: tx, result: tx,
}; };
@ -209,10 +209,10 @@ impl MsgStore<EuphMsg> for EuphVault {
rx.await.unwrap() rx.await.unwrap()
} }
async fn last_tree(&self) -> Option<Snowflake> { async fn last_tree_id(&self) -> Option<Snowflake> {
// TODO vault::Error // TODO vault::Error
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
let request = EuphRequest::LastTree { let request = EuphRequest::GetLastTreeId {
room: self.room.clone(), room: self.room.clone(),
result: tx, result: tx,
}; };
@ -222,7 +222,10 @@ impl MsgStore<EuphMsg> for EuphVault {
} }
pub(super) enum EuphRequest { pub(super) enum EuphRequest {
Rooms { ///////////
// Rooms //
///////////
GetRooms {
result: oneshot::Sender<Vec<String>>, result: oneshot::Sender<Vec<String>>,
}, },
Join { Join {
@ -232,6 +235,10 @@ pub(super) enum EuphRequest {
Delete { Delete {
room: String, room: String,
}, },
//////////////
// Messages //
//////////////
AddMsg { AddMsg {
room: String, room: String,
msg: Message, msg: Message,
@ -242,35 +249,35 @@ pub(super) enum EuphRequest {
msgs: Vec<Message>, msgs: Vec<Message>,
next_msg: Option<Snowflake>, next_msg: Option<Snowflake>,
}, },
LastSpan { GetLastSpan {
room: String, room: String,
result: oneshot::Sender<Option<(Option<Snowflake>, Option<Snowflake>)>>, result: oneshot::Sender<Option<(Option<Snowflake>, Option<Snowflake>)>>,
}, },
Path { GetPath {
room: String, room: String,
id: Snowflake, id: Snowflake,
result: oneshot::Sender<Path<Snowflake>>, result: oneshot::Sender<Path<Snowflake>>,
}, },
Tree { GetTree {
room: String, room: String,
root: Snowflake, root: Snowflake,
result: oneshot::Sender<Tree<EuphMsg>>, result: oneshot::Sender<Tree<EuphMsg>>,
}, },
PrevTree { GetPrevTreeId {
room: String, room: String,
root: Snowflake, root: Snowflake,
result: oneshot::Sender<Option<Snowflake>>, result: oneshot::Sender<Option<Snowflake>>,
}, },
NextTree { GetNextTreeId {
room: String, room: String,
root: Snowflake, root: Snowflake,
result: oneshot::Sender<Option<Snowflake>>, result: oneshot::Sender<Option<Snowflake>>,
}, },
FirstTree { GetFirstTreeId {
room: String, room: String,
result: oneshot::Sender<Option<Snowflake>>, result: oneshot::Sender<Option<Snowflake>>,
}, },
LastTree { GetLastTreeId {
room: String, room: String,
result: oneshot::Sender<Option<Snowflake>>, result: oneshot::Sender<Option<Snowflake>>,
}, },
@ -279,7 +286,7 @@ pub(super) enum EuphRequest {
impl EuphRequest { impl EuphRequest {
pub(super) fn perform(self, conn: &mut Connection) { pub(super) fn perform(self, conn: &mut Connection) {
let result = match self { let result = match self {
EuphRequest::Rooms { result } => Self::rooms(conn, result), EuphRequest::GetRooms { result } => Self::get_rooms(conn, result),
EuphRequest::Join { room, time } => Self::join(conn, room, time), EuphRequest::Join { room, time } => Self::join(conn, room, time),
EuphRequest::Delete { room } => Self::delete(conn, room), EuphRequest::Delete { room } => Self::delete(conn, room),
EuphRequest::AddMsg { EuphRequest::AddMsg {
@ -292,17 +299,21 @@ impl EuphRequest {
msgs, msgs,
next_msg, next_msg,
} => Self::add_msgs(conn, room, msgs, next_msg), } => Self::add_msgs(conn, room, msgs, next_msg),
EuphRequest::LastSpan { room, result } => Self::last_span(conn, room, result), EuphRequest::GetLastSpan { room, result } => Self::get_last_span(conn, room, result),
EuphRequest::Path { room, id, result } => Self::path(conn, room, id, result), EuphRequest::GetPath { room, id, result } => Self::get_path(conn, room, id, result),
EuphRequest::Tree { room, root, result } => Self::tree(conn, room, root, result), EuphRequest::GetTree { room, root, result } => Self::get_tree(conn, room, root, result),
EuphRequest::PrevTree { room, root, result } => { EuphRequest::GetPrevTreeId { room, root, result } => {
Self::prev_tree(conn, room, root, result) Self::get_prev_tree_id(conn, room, root, result)
} }
EuphRequest::NextTree { room, root, result } => { EuphRequest::GetNextTreeId { room, root, result } => {
Self::next_tree(conn, room, root, result) Self::get_next_tree_id(conn, room, root, result)
}
EuphRequest::GetFirstTreeId { room, result } => {
Self::get_first_tree_id(conn, room, result)
}
EuphRequest::GetLastTreeId { room, result } => {
Self::get_last_tree_id(conn, room, result)
} }
EuphRequest::FirstTree { room, result } => Self::first_tree(conn, room, result),
EuphRequest::LastTree { room, result } => Self::last_tree(conn, room, result),
}; };
if let Err(e) = result { if let Err(e) = result {
// If an error occurs here, the rest of the UI will likely panic and // If an error occurs here, the rest of the UI will likely panic and
@ -313,7 +324,10 @@ impl EuphRequest {
} }
} }
fn rooms(conn: &mut Connection, result: oneshot::Sender<Vec<String>>) -> rusqlite::Result<()> { fn get_rooms(
conn: &mut Connection,
result: oneshot::Sender<Vec<String>>,
) -> rusqlite::Result<()> {
let rooms = conn let rooms = conn
.prepare( .prepare(
" "
@ -533,7 +547,7 @@ impl EuphRequest {
Ok(()) Ok(())
} }
fn last_span( fn get_last_span(
conn: &Connection, conn: &Connection,
room: String, room: String,
result: oneshot::Sender<Option<(Option<Snowflake>, Option<Snowflake>)>>, result: oneshot::Sender<Option<(Option<Snowflake>, Option<Snowflake>)>>,
@ -554,7 +568,7 @@ impl EuphRequest {
Ok(()) Ok(())
} }
fn path( fn get_path(
conn: &Connection, conn: &Connection,
room: String, room: String,
id: Snowflake, id: Snowflake,
@ -584,7 +598,7 @@ impl EuphRequest {
Ok(()) Ok(())
} }
fn tree( fn get_tree(
conn: &Connection, conn: &Connection,
room: String, room: String,
root: Snowflake, root: Snowflake,
@ -624,7 +638,7 @@ impl EuphRequest {
Ok(()) Ok(())
} }
fn prev_tree( fn get_prev_tree_id(
conn: &Connection, conn: &Connection,
room: String, room: String,
root: Snowflake, root: Snowflake,
@ -647,7 +661,7 @@ impl EuphRequest {
Ok(()) Ok(())
} }
fn next_tree( fn get_next_tree_id(
conn: &Connection, conn: &Connection,
room: String, room: String,
root: Snowflake, root: Snowflake,
@ -670,7 +684,7 @@ impl EuphRequest {
Ok(()) Ok(())
} }
fn first_tree( fn get_first_tree_id(
conn: &Connection, conn: &Connection,
room: String, room: String,
result: oneshot::Sender<Option<Snowflake>>, result: oneshot::Sender<Option<Snowflake>>,
@ -691,7 +705,7 @@ impl EuphRequest {
Ok(()) Ok(())
} }
fn last_tree( fn get_last_tree_id(
conn: &Connection, conn: &Connection,
room: String, room: String,
result: oneshot::Sender<Option<Snowflake>>, result: oneshot::Sender<Option<Snowflake>>,