22
33use std:: collections:: BTreeMap ;
44
5+ use catalyst_signed_doc:: problem_report:: ProblemReport ;
56use cbork_utils:: { decode_context:: DecodeCtx , map:: Map } ;
67use minicbor:: { Decode , Decoder , Encode , Encoder , encode:: Write } ;
78
@@ -34,13 +35,15 @@ pub struct ContentBallotPayload {
3435 pub voter_choices : Option < EncryptedChoices > ,
3536}
3637
37- impl Decode < ' _ , ( ) > for ContentBallotPayload {
38+ impl Decode < ' _ , ProblemReport > for ContentBallotPayload {
3839 fn decode (
3940 d : & mut Decoder < ' _ > ,
40- ctx : & mut ( ) ,
41+ report : & mut ProblemReport ,
4142 ) -> Result < Self , minicbor:: decode:: Error > {
4243 use minicbor:: data:: Type ;
4344
45+ let context = "Content ballot payload decoding" ;
46+
4447 let map = Map :: decode ( d, & mut DecodeCtx :: Deterministic ) ?;
4548
4649 let mut choices = BTreeMap :: new ( ) ;
@@ -55,36 +58,56 @@ impl Decode<'_, ()> for ContentBallotPayload {
5558 match key_decoder. datatype ( ) ? {
5659 Type :: U8 | Type :: U16 | Type :: U32 | Type :: U64 => {
5760 let key = key_decoder. u64 ( ) ?;
58- let val = Choices :: decode ( & mut value_decoder, ctx) ?;
59- choices. insert ( key, val) ;
61+ match Choices :: decode ( & mut value_decoder, & mut ( ) ) {
62+ Ok ( val) => {
63+ choices. insert ( key, val) ;
64+ } ,
65+ Err ( e) => {
66+ report. other (
67+ & format ! ( "Unable to decode choices for {key} key: {e:?}" ) ,
68+ context,
69+ ) ;
70+ } ,
71+ }
6072 } ,
6173 Type :: String => {
6274 match key_decoder. str ( ) ? {
6375 "column-proof" => {
64- return Err ( minicbor :: decode :: Error :: message (
76+ report . other (
6577 "column-proof is a placeholder and shouldn't be used" ,
66- ) ) ;
78+ context,
79+ ) ;
6780 } ,
6881 "matrix-proof" => {
69- return Err ( minicbor :: decode :: Error :: message (
82+ report . other (
7083 "matrix-proof is a placeholder and shouldn't be used" ,
71- ) ) ;
84+ context,
85+ ) ;
7286 } ,
7387 "voter-choices" => {
74- voter_choices =
75- Some ( EncryptedChoices :: decode ( & mut value_decoder, ctx) ?) ;
88+ match EncryptedChoices :: decode ( & mut value_decoder, & mut ( ) ) {
89+ Ok ( v) => voter_choices = Some ( v) ,
90+ Err ( e) => {
91+ report. other (
92+ & format ! ( "Unable to decode encrypted choices: {e:?}" ) ,
93+ context,
94+ ) ;
95+ } ,
96+ }
7697 } ,
7798 key => {
78- return Err ( minicbor:: decode:: Error :: message ( format ! (
79- "Unexpected content ballot payload key value: {key:?}"
80- ) ) ) ;
99+ report. other (
100+ & format ! ( "Unexpected content ballot payload key value: {key:?}" ) ,
101+ context,
102+ ) ;
81103 } ,
82104 }
83105 } ,
84106 t => {
85- return Err ( minicbor:: decode:: Error :: message ( format ! (
86- "Unexpected content ballot payload key type: {t:?}"
87- ) ) ) ;
107+ report. other (
108+ & format ! ( "Unexpected content ballot payload key type: {t:?}" ) ,
109+ context,
110+ ) ;
88111 } ,
89112 }
90113 }
@@ -160,7 +183,10 @@ mod tests {
160183 original
161184 . encode ( & mut Encoder :: new ( & mut buffer) , & mut ( ) )
162185 . unwrap ( ) ;
163- let decoded = ContentBallotPayload :: decode ( & mut Decoder :: new ( & buffer) , & mut ( ) ) . unwrap ( ) ;
186+ let mut report = ProblemReport :: new ( "test" ) ;
187+ let decoded =
188+ ContentBallotPayload :: decode ( & mut Decoder :: new ( & buffer) , & mut report) . unwrap ( ) ;
164189 assert_eq ! ( original, decoded) ;
190+ assert ! ( !report. is_problematic( ) ) ;
165191 }
166192}
0 commit comments