From bb3787258a22f6830daf9b9fea242eb16018db65 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 26 Jan 2023 16:20:22 +0100 Subject: [PATCH] cob: Refactor `Thread` methods --- radicle-cli/src/commands/comment.rs | 3 ++- radicle/src/cob/patch.rs | 2 +- radicle/src/cob/thread.rs | 23 ++++++++++------------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/radicle-cli/src/commands/comment.rs b/radicle-cli/src/commands/comment.rs index ac2cd9d6..c38d1331 100644 --- a/radicle-cli/src/commands/comment.rs +++ b/radicle-cli/src/commands/comment.rs @@ -97,9 +97,10 @@ fn comment( store::Error::NotFound(_, _) => anyhow::anyhow!("Could not find issue {}", options.id), _ => e.into(), })?; - let (comment_id, _) = issue.root().expect("root comment always exists"); + let (comment_id, _) = issue.comments().next().expect("root comment always exists"); issue.comment(message, *comment_id, &signer)?; + Ok(()) } diff --git a/radicle/src/cob/patch.rs b/radicle/src/cob/patch.rs index 7c60f3f1..4261ae6f 100644 --- a/radicle/src/cob/patch.rs +++ b/radicle/src/cob/patch.rs @@ -353,7 +353,7 @@ impl Revision { } pub fn description(&self) -> Option<&str> { - let (_, comment) = self.discussion.root()?; + let (_, comment) = self.discussion.first()?; Some(comment.body()) } } diff --git a/radicle/src/cob/thread.rs b/radicle/src/cob/thread.rs index 181e5e82..8ddc1416 100644 --- a/radicle/src/cob/thread.rs +++ b/radicle/src/cob/thread.rs @@ -203,11 +203,12 @@ impl Thread { } } - pub fn root(&self) -> Option<(&CommentId, &Comment)> { - self.comments - .iter() - .filter_map(|(id, r)| r.get().map(|comment| (id, comment))) - .next() + pub fn first(&self) -> Option<(&CommentId, &Comment)> { + self.comments().next() + } + + pub fn last(&self) -> Option<(&CommentId, &Comment)> { + self.comments().next_back() } pub fn replies<'a>( @@ -235,14 +236,10 @@ impl Thread { .map(|(a, r)| (a, r)) } - pub fn comments(&self) -> impl Iterator + '_ { - self.comments.iter().filter_map(|(id, comment)| { - if let Redactable::Present(c) = comment { - Some((id, c)) - } else { - None - } - }) + pub fn comments(&self) -> impl DoubleEndedIterator + '_ { + self.comments + .iter() + .filter_map(|(id, comment)| comment.get().map(|comment| (id, comment))) } }