1- use std:: { borrow:: Cow , ops:: RangeBounds } ;
1+ use std:: { borrow:: Cow , fmt :: Display , ops:: RangeBounds } ;
22
33use backend_api:: ApiError ;
44use candid:: { CandidType , Decode , Deserialize , Encode } ;
@@ -11,13 +11,40 @@ use super::{CommitSha, DateTime, ProposalReviewId, UserId, Uuid};
1111
1212pub type ProposalReviewCommitId = Uuid ;
1313
14+ #[ derive( Debug , CandidType , Deserialize , Clone , PartialEq , Eq ) ]
15+ pub struct ReviewedCommitState {
16+ pub matches_description : Option < bool > ,
17+ pub comment : Option < String > ,
18+ pub highlights : Vec < String > ,
19+ }
20+
21+ impl Display for ReviewedCommitState {
22+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
23+ let mut commit_details = format ! (
24+ "Matches description: {}" ,
25+ self . matches_description
26+ . map( |b| b. to_string( ) )
27+ . unwrap_or_else( || "Unanswered" . to_string( ) ) ,
28+ ) ;
29+ if let Some ( comment) = self . comment . as_ref ( ) {
30+ if !comment. is_empty ( ) {
31+ commit_details = format ! ( "{}\n Comment: {}" , commit_details, comment) ;
32+ }
33+ }
34+ if !self . highlights . is_empty ( ) {
35+ commit_details = format ! (
36+ "{}\n Highlights: {}" ,
37+ commit_details,
38+ self . highlights. join( ", " )
39+ ) ;
40+ }
41+ write ! ( f, "{}" , commit_details)
42+ }
43+ }
44+
1445#[ derive( Debug , CandidType , Deserialize , Clone , PartialEq , Eq ) ]
1546pub enum ReviewCommitState {
16- Reviewed {
17- matches_description : Option < bool > ,
18- comment : Option < String > ,
19- highlights : Vec < String > ,
20- } ,
47+ Reviewed ( ReviewedCommitState ) ,
2148 NotReviewed ,
2249}
2350
@@ -39,6 +66,14 @@ impl ProposalReviewCommit {
3966 pub fn is_not_reviewed ( & self ) -> bool {
4067 matches ! ( & self . state, ReviewCommitState :: NotReviewed )
4168 }
69+
70+ pub fn reviewed_state ( & self ) -> Option < & ReviewedCommitState > {
71+ if let ReviewCommitState :: Reviewed ( state) = & self . state {
72+ Some ( state)
73+ } else {
74+ None
75+ }
76+ }
4277}
4378
4479impl Storable for ProposalReviewCommit {
@@ -174,4 +209,42 @@ mod tests {
174209
175210 assert_eq ! ( key, deserialized_key) ;
176211 }
212+
213+ #[ rstest]
214+ fn reviewed_commit_state_display_impl ( ) {
215+ let mut state = ReviewedCommitState {
216+ matches_description : None ,
217+ comment : None ,
218+ highlights : vec ! [ ] ,
219+ } ;
220+
221+ assert_eq ! ( state. to_string( ) , "Matches description: Unanswered" ) ;
222+
223+ state. matches_description = Some ( false ) ;
224+ assert_eq ! ( state. to_string( ) , "Matches description: false" ) ;
225+
226+ state. matches_description = Some ( true ) ;
227+ assert_eq ! ( state. to_string( ) , "Matches description: true" ) ;
228+
229+ state. comment = Some ( "" . to_string ( ) ) ;
230+ assert_eq ! ( state. to_string( ) , "Matches description: true" ) ;
231+
232+ state. comment = Some ( "test" . to_string ( ) ) ;
233+ assert_eq ! (
234+ state. to_string( ) ,
235+ "Matches description: true\n Comment: test"
236+ ) ;
237+
238+ state. highlights = vec ! [ "test" . to_string( ) ] ;
239+ assert_eq ! (
240+ state. to_string( ) ,
241+ "Matches description: true\n Comment: test\n Highlights: test"
242+ ) ;
243+
244+ state. highlights = vec ! [ "test1" . to_string( ) , "test2" . to_string( ) ] ;
245+ assert_eq ! (
246+ state. to_string( ) ,
247+ "Matches description: true\n Comment: test\n Highlights: test1, test2"
248+ ) ;
249+ }
177250}
0 commit comments