diff --git a/bin/commands/issue.rs b/bin/commands/issue.rs index 895e55b5..3eb6c401 100644 --- a/bin/commands/issue.rs +++ b/bin/commands/issue.rs @@ -11,10 +11,12 @@ use std::ffi::OsString; use anyhow::anyhow; +use radicle::issue; + use radicle::identity::RepoId; use radicle_tui as tui; -use tui::common::cob::issue::{self, State}; +use tui::common::cob; use tui::common::log; use crate::terminal; @@ -59,7 +61,7 @@ pub enum OperationName { #[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct SelectOptions { mode: common::Mode, - filter: issue::Filter, + filter: cob::issue::Filter, } impl Args for Options { @@ -92,13 +94,19 @@ impl Args for Options { select_opts.filter = select_opts.filter.with_state(None); } Long("open") if op == Some(OperationName::Select) => { - select_opts.filter = select_opts.filter.with_state(Some(State::Open)); + select_opts.filter = select_opts.filter.with_state(Some(issue::State::Open)); } Long("solved") if op == Some(OperationName::Select) => { - select_opts.filter = select_opts.filter.with_state(Some(State::Solved)); + select_opts.filter = + select_opts.filter.with_state(Some(issue::State::Closed { + reason: issue::CloseReason::Solved, + })); } Long("closed") if op == Some(OperationName::Select) => { - select_opts.filter = select_opts.filter.with_state(Some(State::Closed)); + select_opts.filter = + select_opts.filter.with_state(Some(issue::State::Closed { + reason: issue::CloseReason::Other, + })); } Long("assigned") if op == Some(OperationName::Select) => { if let Ok(val) = parser.value() { diff --git a/src/common/cob/issue.rs b/src/common/cob/issue.rs index 6fe464a2..b59b3cf5 100644 --- a/src/common/cob/issue.rs +++ b/src/common/cob/issue.rs @@ -1,34 +1,12 @@ -use std::fmt::Display; - use anyhow::Result; + use radicle::cob::issue::{Issue, IssueId}; use radicle::cob::Label; use radicle::issue::cache::Issues; -use radicle::issue::CloseReason; +use radicle::issue::State; use radicle::prelude::{Did, Signer}; use radicle::storage::git::Repository; -use radicle::{issue, Profile}; - -use super::format; - -#[derive(Clone, Default, Debug, Eq, PartialEq)] -pub enum State { - #[default] - Open, - Solved, - Closed, -} - -impl Display for State { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let state = match self { - State::Open => "open", - State::Solved => "solved", - State::Closed => "closed", - }; - f.write_str(state) - } -} +use radicle::Profile; #[derive(Clone, Debug, Eq, PartialEq)] pub struct Filter { @@ -62,44 +40,6 @@ impl Filter { self.assignees.push(assignee); self } - - pub fn state(&self) -> Option { - self.state.clone() - } - - pub fn matches(&self, profile: &Profile, issue: &Issue) -> bool { - let matches_state = match self.state { - Some(State::Open) => matches!(issue.state(), issue::State::Open), - Some(State::Solved) => matches!( - issue.state(), - issue::State::Closed { - reason: CloseReason::Solved - } - ), - Some(State::Closed) => matches!(issue.state(), issue::State::Closed { .. }), - None => true, - }; - - let matches_assgined = self - .assigned - .then(|| { - issue - .assignees() - .collect::>() - .contains(&&profile.did()) - }) - .unwrap_or(true); - - let matches_assignees = (!self.assignees.is_empty()) - .then(|| { - self.assignees - .iter() - .any(|other| issue.assignees().collect::>().contains(&other)) - }) - .unwrap_or(true); - - matches_state && matches_assgined && matches_assignees - } } impl ToString for Filter { @@ -120,7 +60,7 @@ impl ToString for Filter { let mut assignees = self.assignees.iter().peekable(); while let Some(assignee) = assignees.next() { - filter.push_str(&format::did(assignee)); + filter.push_str(&assignee.encode()); if assignees.peek().is_some() { filter.push(','); @@ -159,3 +99,51 @@ pub fn create( Ok(*issue.id()) } + +#[cfg(test)] +mod tests { + use std::str::FromStr; + + use anyhow::Result; + use radicle::issue; + + use super::*; + + #[test] + fn issue_filter_display_with_state_should_succeed() -> Result<()> { + let actual = Filter::default().with_state(Some(issue::State::Open)); + + assert_eq!(String::from("is:open "), actual.to_string()); + + Ok(()) + } + + #[test] + fn issue_filter_display_with_state_and_assigned_should_succeed() -> Result<()> { + let actual = Filter::default() + .with_state(Some(issue::State::Open)) + .with_assgined(true); + + assert_eq!(String::from("is:open is:assigned "), actual.to_string()); + + Ok(()) + } + + #[test] + fn issue_filter_display_with_status_and_author_should_succeed() -> Result<()> { + let actual = Filter::default() + .with_state(Some(issue::State::Open)) + .with_assginee(Did::from_str( + "did:key:z6MkswQE8gwZw924amKatxnNCXA55BMupMmRg7LvJuim2C1V", + )?); + + assert_eq!( + String::from( + "is:open assignees:[did:key:z6MkswQE8gwZw924amKatxnNCXA55BMupMmRg7LvJuim2C1V]" + ), + actual.to_string() + ); + + Ok(()) + } +} diff --git a/src/common/cob/patch.rs b/src/common/cob/patch.rs index f234a4d5..e8b3324d 100644 --- a/src/common/cob/patch.rs +++ b/src/common/cob/patch.rs @@ -59,7 +59,7 @@ impl ToString for Filter { let mut authors = self.authors.iter().peekable(); while let Some(author) = authors.next() { - filter.push_str(&author.to_string()); + filter.push_str(&author.encode()); if authors.peek().is_some() { filter.push(','); @@ -122,7 +122,9 @@ mod tests { )?); assert_eq!( - String::from("is:open authors:[z6MkswQE8gwZw924amKatxnNCXA55BMupMmRg7LvJuim2C1V]"), + String::from( + "is:open authors:[did:key:z6MkswQE8gwZw924amKatxnNCXA55BMupMmRg7LvJuim2C1V]" + ), actual.to_string() );