Rename tree_id to root_id
This commit is contained in:
parent
da2c3d86f5
commit
cb1fdb41b8
6 changed files with 69 additions and 70 deletions
|
|
@ -20,11 +20,11 @@ pub async fn export_to_file(
|
|||
) -> anyhow::Result<()> {
|
||||
let mut exported_trees = 0;
|
||||
let mut exported_msgs = 0;
|
||||
let mut tree_id = vault.first_tree_id().await;
|
||||
while let Some(some_tree_id) = tree_id {
|
||||
let tree = vault.tree(some_tree_id).await;
|
||||
write_tree(file, &tree, some_tree_id, 0)?;
|
||||
tree_id = vault.next_tree_id(some_tree_id).await;
|
||||
let mut root_id = vault.first_root_id().await;
|
||||
while let Some(some_root_id) = root_id {
|
||||
let tree = vault.tree(some_root_id).await;
|
||||
write_tree(file, &tree, some_root_id, 0)?;
|
||||
root_id = vault.next_root_id(some_root_id).await;
|
||||
|
||||
exported_trees += 1;
|
||||
exported_msgs += tree.len();
|
||||
|
|
|
|||
|
|
@ -83,48 +83,48 @@ impl MsgStore<LogMsg> for Logger {
|
|||
self.messages.lock().get(*id).cloned()
|
||||
}
|
||||
|
||||
async fn tree(&self, tree_id: &usize) -> Tree<LogMsg> {
|
||||
async fn tree(&self, root_id: &usize) -> Tree<LogMsg> {
|
||||
let msgs = self
|
||||
.messages
|
||||
.lock()
|
||||
.get(*tree_id)
|
||||
.get(*root_id)
|
||||
.map(|msg| vec![msg.clone()])
|
||||
.unwrap_or_default();
|
||||
Tree::new(*tree_id, msgs)
|
||||
Tree::new(*root_id, msgs)
|
||||
}
|
||||
|
||||
async fn first_tree_id(&self) -> Option<usize> {
|
||||
async fn first_root_id(&self) -> Option<usize> {
|
||||
let empty = self.messages.lock().is_empty();
|
||||
Some(0).filter(|_| !empty)
|
||||
}
|
||||
|
||||
async fn last_tree_id(&self) -> Option<usize> {
|
||||
async fn last_root_id(&self) -> Option<usize> {
|
||||
self.messages.lock().len().checked_sub(1)
|
||||
}
|
||||
|
||||
async fn prev_tree_id(&self, tree_id: &usize) -> Option<usize> {
|
||||
tree_id.checked_sub(1)
|
||||
async fn prev_root_id(&self, root_id: &usize) -> Option<usize> {
|
||||
root_id.checked_sub(1)
|
||||
}
|
||||
|
||||
async fn next_tree_id(&self, tree_id: &usize) -> Option<usize> {
|
||||
async fn next_root_id(&self, root_id: &usize) -> Option<usize> {
|
||||
let len = self.messages.lock().len();
|
||||
tree_id.checked_add(1).filter(|t| *t < len)
|
||||
root_id.checked_add(1).filter(|t| *t < len)
|
||||
}
|
||||
|
||||
async fn oldest_msg_id(&self) -> Option<usize> {
|
||||
self.first_tree_id().await
|
||||
self.first_root_id().await
|
||||
}
|
||||
|
||||
async fn newest_msg_id(&self) -> Option<usize> {
|
||||
self.last_tree_id().await
|
||||
self.last_root_id().await
|
||||
}
|
||||
|
||||
async fn older_msg_id(&self, id: &usize) -> Option<usize> {
|
||||
self.prev_tree_id(id).await
|
||||
self.prev_root_id(id).await
|
||||
}
|
||||
|
||||
async fn newer_msg_id(&self, id: &usize) -> Option<usize> {
|
||||
self.next_tree_id(id).await
|
||||
self.next_root_id(id).await
|
||||
}
|
||||
|
||||
async fn oldest_unseen_msg_id(&self) -> Option<usize> {
|
||||
|
|
|
|||
10
src/store.rs
10
src/store.rs
|
|
@ -134,11 +134,11 @@ impl<M: Msg> Tree<M> {
|
|||
pub trait MsgStore<M: Msg> {
|
||||
async fn path(&self, id: &M::Id) -> Path<M::Id>;
|
||||
async fn msg(&self, id: &M::Id) -> Option<M>;
|
||||
async fn tree(&self, tree_id: &M::Id) -> Tree<M>;
|
||||
async fn first_tree_id(&self) -> Option<M::Id>;
|
||||
async fn last_tree_id(&self) -> Option<M::Id>;
|
||||
async fn prev_tree_id(&self, tree_id: &M::Id) -> Option<M::Id>;
|
||||
async fn next_tree_id(&self, tree_id: &M::Id) -> Option<M::Id>;
|
||||
async fn tree(&self, root_id: &M::Id) -> Tree<M>;
|
||||
async fn first_root_id(&self) -> Option<M::Id>;
|
||||
async fn last_root_id(&self) -> Option<M::Id>;
|
||||
async fn prev_root_id(&self, root_id: &M::Id) -> Option<M::Id>;
|
||||
async fn next_root_id(&self, root_id: &M::Id) -> Option<M::Id>;
|
||||
async fn oldest_msg_id(&self) -> Option<M::Id>;
|
||||
async fn newest_msg_id(&self) -> Option<M::Id>;
|
||||
async fn older_msg_id(&self, id: &M::Id) -> Option<M::Id>;
|
||||
|
|
|
|||
|
|
@ -101,9 +101,9 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
|||
} else if tree.parent(id).is_none() {
|
||||
// We're at the root of our tree, so we need to move to the root of
|
||||
// the previous tree.
|
||||
if let Some(prev_tree_id) = store.prev_tree_id(tree.root()).await {
|
||||
*tree = store.tree(&prev_tree_id).await;
|
||||
*id = prev_tree_id;
|
||||
if let Some(prev_root_id) = store.prev_root_id(tree.root()).await {
|
||||
*tree = store.tree(&prev_root_id).await;
|
||||
*id = prev_root_id;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
|
|
@ -123,9 +123,9 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
|||
} else if tree.parent(id).is_none() {
|
||||
// We're at the root of our tree, so we need to move to the root of
|
||||
// the next tree.
|
||||
if let Some(next_tree_id) = store.next_tree_id(tree.root()).await {
|
||||
*tree = store.tree(&next_tree_id).await;
|
||||
*id = next_tree_id;
|
||||
if let Some(next_root_id) = store.next_root_id(tree.root()).await {
|
||||
*tree = store.tree(&next_root_id).await;
|
||||
*id = next_root_id;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
|
|
@ -183,9 +183,9 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
|||
pub async fn move_cursor_up(&mut self) {
|
||||
match &mut self.cursor {
|
||||
Cursor::Bottom | Cursor::Pseudo { parent: None, .. } => {
|
||||
if let Some(last_tree_id) = self.store.last_tree_id().await {
|
||||
let tree = self.store.tree(&last_tree_id).await;
|
||||
let mut id = last_tree_id;
|
||||
if let Some(last_root_id) = self.store.last_root_id().await {
|
||||
let tree = self.store.tree(&last_root_id).await;
|
||||
let mut id = last_root_id;
|
||||
while Self::find_last_child(&self.folded, &tree, &mut id) {}
|
||||
self.cursor = Cursor::Msg(id);
|
||||
}
|
||||
|
|
@ -243,8 +243,8 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
|||
pub async fn move_cursor_up_sibling(&mut self) {
|
||||
match &mut self.cursor {
|
||||
Cursor::Bottom | Cursor::Pseudo { parent: None, .. } => {
|
||||
if let Some(last_tree_id) = self.store.last_tree_id().await {
|
||||
self.cursor = Cursor::Msg(last_tree_id);
|
||||
if let Some(last_root_id) = self.store.last_root_id().await {
|
||||
self.cursor = Cursor::Msg(last_root_id);
|
||||
}
|
||||
}
|
||||
Cursor::Msg(msg) => {
|
||||
|
|
@ -392,8 +392,8 @@ impl<M: Msg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
|||
}
|
||||
|
||||
pub async fn move_cursor_to_top(&mut self) {
|
||||
if let Some(first_tree_id) = self.store.first_tree_id().await {
|
||||
self.cursor = Cursor::Msg(first_tree_id);
|
||||
if let Some(first_root_id) = self.store.first_root_id().await {
|
||||
self.cursor = Cursor::Msg(first_root_id);
|
||||
self.correction = Some(Correction::MakeCursorVisible);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -181,15 +181,15 @@ impl<M: Msg + ChatMsg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
|||
|
||||
while blocks.blocks().top_line > top_line {
|
||||
let top_root = blocks.top_root();
|
||||
let prev_tree_id = match top_root {
|
||||
Root::Bottom => self.store.last_tree_id().await,
|
||||
Root::Tree(tree_id) => self.store.prev_tree_id(tree_id).await,
|
||||
let prev_root_id = match top_root {
|
||||
Root::Bottom => self.store.last_root_id().await,
|
||||
Root::Tree(root_id) => self.store.prev_root_id(root_id).await,
|
||||
};
|
||||
let prev_tree_id = match prev_tree_id {
|
||||
Some(tree_id) => tree_id,
|
||||
let prev_root_id = match prev_root_id {
|
||||
Some(id) => id,
|
||||
None => break,
|
||||
};
|
||||
let prev_tree = self.store.tree(&prev_tree_id).await;
|
||||
let prev_tree = self.store.tree(&prev_root_id).await;
|
||||
blocks.prepend(self.layout_tree(nick, frame, prev_tree));
|
||||
}
|
||||
}
|
||||
|
|
@ -204,12 +204,12 @@ impl<M: Msg + ChatMsg, S: MsgStore<M>> InnerTreeViewState<M, S> {
|
|||
|
||||
while blocks.blocks().bottom_line < bottom_line {
|
||||
let bottom_root = blocks.bottom_root();
|
||||
let next_tree_id = match bottom_root {
|
||||
let next_root_id = match bottom_root {
|
||||
Root::Bottom => break,
|
||||
Root::Tree(tree_id) => self.store.next_tree_id(tree_id).await,
|
||||
Root::Tree(root_id) => self.store.next_root_id(root_id).await,
|
||||
};
|
||||
if let Some(next_tree_id) = next_tree_id {
|
||||
let next_tree = self.store.tree(&next_tree_id).await;
|
||||
if let Some(next_root_id) = next_root_id {
|
||||
let next_tree = self.store.tree(&next_root_id).await;
|
||||
blocks.append(self.layout_tree(nick, frame, next_tree));
|
||||
} else {
|
||||
blocks.append(self.layout_bottom(nick, frame));
|
||||
|
|
|
|||
|
|
@ -100,24 +100,24 @@ impl MsgStore<SmallMessage> for EuphRoomVault {
|
|||
self.msg(*id).await
|
||||
}
|
||||
|
||||
async fn tree(&self, tree_id: &Snowflake) -> Tree<SmallMessage> {
|
||||
self.tree(*tree_id).await
|
||||
async fn tree(&self, root_id: &Snowflake) -> Tree<SmallMessage> {
|
||||
self.tree(*root_id).await
|
||||
}
|
||||
|
||||
async fn first_tree_id(&self) -> Option<Snowflake> {
|
||||
self.first_tree_id().await
|
||||
async fn first_root_id(&self) -> Option<Snowflake> {
|
||||
self.first_root_id().await
|
||||
}
|
||||
|
||||
async fn last_tree_id(&self) -> Option<Snowflake> {
|
||||
self.last_tree_id().await
|
||||
async fn last_root_id(&self) -> Option<Snowflake> {
|
||||
self.last_root_id().await
|
||||
}
|
||||
|
||||
async fn prev_tree_id(&self, tree_id: &Snowflake) -> Option<Snowflake> {
|
||||
self.prev_tree_id(*tree_id).await
|
||||
async fn prev_root_id(&self, root_id: &Snowflake) -> Option<Snowflake> {
|
||||
self.prev_root_id(*root_id).await
|
||||
}
|
||||
|
||||
async fn next_tree_id(&self, tree_id: &Snowflake) -> Option<Snowflake> {
|
||||
self.next_tree_id(*tree_id).await
|
||||
async fn next_root_id(&self, root_id: &Snowflake) -> Option<Snowflake> {
|
||||
self.next_root_id(*root_id).await
|
||||
}
|
||||
|
||||
async fn oldest_msg_id(&self) -> Option<Snowflake> {
|
||||
|
|
@ -241,7 +241,6 @@ macro_rules! requests {
|
|||
};
|
||||
}
|
||||
|
||||
// TODO Rename `root` to `root_id` or `tree_id`
|
||||
requests! {
|
||||
// Cookies
|
||||
GetCookies : cookies() -> CookieJar;
|
||||
|
|
@ -259,11 +258,11 @@ requests! {
|
|||
GetPath : path(room: String, id: Snowflake) -> Path<Snowflake>;
|
||||
GetMsg : msg(room: String, id: Snowflake) -> Option<SmallMessage>;
|
||||
GetFullMsg : full_msg(room: String, id: Snowflake) -> Option<Message>;
|
||||
GetTree : tree(room: String, root: Snowflake) -> Tree<SmallMessage>;
|
||||
GetFirstTreeId : first_tree_id(room: String) -> Option<Snowflake>;
|
||||
GetLastTreeId : last_tree_id(room: String) -> Option<Snowflake>;
|
||||
GetPrevTreeId : prev_tree_id(room: String, root: Snowflake) -> Option<Snowflake>;
|
||||
GetNextTreeId : next_tree_id(room: String, root: Snowflake) -> Option<Snowflake>;
|
||||
GetTree : tree(room: String, root_id: Snowflake) -> Tree<SmallMessage>;
|
||||
GetFirstRootId : first_root_id(room: String) -> Option<Snowflake>;
|
||||
GetLastRootId : last_root_id(room: String) -> Option<Snowflake>;
|
||||
GetPrevRootId : prev_root_id(room: String, root_id: Snowflake) -> Option<Snowflake>;
|
||||
GetNextRootId : next_root_id(room: String, root_id: Snowflake) -> Option<Snowflake>;
|
||||
GetOldestMsgId : oldest_msg_id(room: String) -> Option<Snowflake>;
|
||||
GetNewestMsgId : newest_msg_id(room: String) -> Option<Snowflake>;
|
||||
GetOlderMsgId : older_msg_id(room: String, id: Snowflake) -> Option<Snowflake>;
|
||||
|
|
@ -707,7 +706,7 @@ impl Request for GetTree {
|
|||
ORDER BY id ASC
|
||||
",
|
||||
)?
|
||||
.query_map(params![self.room, WSnowflake(self.root)], |row| {
|
||||
.query_map(params![self.room, WSnowflake(self.root_id)], |row| {
|
||||
Ok(SmallMessage {
|
||||
id: row.get::<_, WSnowflake>(0)?.0,
|
||||
parent: row.get::<_, Option<WSnowflake>>(1)?.map(|s| s.0),
|
||||
|
|
@ -718,13 +717,13 @@ impl Request for GetTree {
|
|||
})
|
||||
})?
|
||||
.collect::<rusqlite::Result<_>>()?;
|
||||
let tree = Tree::new(self.root, msgs);
|
||||
let tree = Tree::new(self.root_id, msgs);
|
||||
let _ = self.result.send(tree);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Request for GetFirstTreeId {
|
||||
impl Request for GetFirstRootId {
|
||||
fn perform(self, conn: &mut Connection) -> rusqlite::Result<()> {
|
||||
let tree = conn
|
||||
.prepare(
|
||||
|
|
@ -743,7 +742,7 @@ impl Request for GetFirstTreeId {
|
|||
}
|
||||
}
|
||||
|
||||
impl Request for GetLastTreeId {
|
||||
impl Request for GetLastRootId {
|
||||
fn perform(self, conn: &mut Connection) -> rusqlite::Result<()> {
|
||||
let tree = conn
|
||||
.prepare(
|
||||
|
|
@ -762,7 +761,7 @@ impl Request for GetLastTreeId {
|
|||
}
|
||||
}
|
||||
|
||||
impl Request for GetPrevTreeId {
|
||||
impl Request for GetPrevRootId {
|
||||
fn perform(self, conn: &mut Connection) -> rusqlite::Result<()> {
|
||||
let tree = conn
|
||||
.prepare(
|
||||
|
|
@ -775,7 +774,7 @@ impl Request for GetPrevTreeId {
|
|||
LIMIT 1
|
||||
",
|
||||
)?
|
||||
.query_row(params![self.room, WSnowflake(self.root)], |row| {
|
||||
.query_row(params![self.room, WSnowflake(self.root_id)], |row| {
|
||||
row.get::<_, WSnowflake>(0).map(|s| s.0)
|
||||
})
|
||||
.optional()?;
|
||||
|
|
@ -784,7 +783,7 @@ impl Request for GetPrevTreeId {
|
|||
}
|
||||
}
|
||||
|
||||
impl Request for GetNextTreeId {
|
||||
impl Request for GetNextRootId {
|
||||
fn perform(self, conn: &mut Connection) -> rusqlite::Result<()> {
|
||||
let tree = conn
|
||||
.prepare(
|
||||
|
|
@ -797,7 +796,7 @@ impl Request for GetNextTreeId {
|
|||
LIMIT 1
|
||||
",
|
||||
)?
|
||||
.query_row(params![self.room, WSnowflake(self.root)], |row| {
|
||||
.query_row(params![self.room, WSnowflake(self.root_id)], |row| {
|
||||
row.get::<_, WSnowflake>(0).map(|s| s.0)
|
||||
})
|
||||
.optional()?;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue