@@ -30,43 +30,42 @@ bool ui_expert_mode = false;
3030
3131#define NONE_STRING "None"
3232
33- parser_error_t schema_display_integer (parser_context_t * ctx , primitive_integer_t * primitive ) {
33+ parser_error_t schema_display_integer (parser_context_t * ctx , primitive_integer_t * primitive , parser_context_t * ctx_to_push ) {
3434 CHECK_INPUT (ctx );
3535 CHECK_INPUT (primitive );
36+ CHECK_INPUT (ctx_to_push );
3637
37- parser_context_t ctx_bytes = {0 };
38- uint16_t len = 0 ;
39-
40- ctx_bytes .buffer .ptr = ctx -> buffer .ptr + ctx -> offset ;
38+ uint16_t len_to_push = 0 ;
39+ const uint8_t * ctx_mem = ctx -> buffer .ptr + ctx -> offset ;
4140 switch (primitive -> type ) {
4241 case INTEGER_I8 :
4342 case INTEGER_U8 : {
44- len = OFFSET_U8 ;
45- CTX_CHECK_AND_ADVANCE (ctx , len );
43+ len_to_push = OFFSET_U8 ;
44+ CTX_CHECK_AND_ADVANCE (ctx , len_to_push );
4645 break ;
4746 }
4847 case INTEGER_I16 :
4948 case INTEGER_U16 : {
50- len = OFFSET_U16 ;
51- CTX_CHECK_AND_ADVANCE (ctx , len );
49+ len_to_push = OFFSET_U16 ;
50+ CTX_CHECK_AND_ADVANCE (ctx , len_to_push );
5251 break ;
5352 }
5453 case INTEGER_I32 :
5554 case INTEGER_U32 : {
56- len = OFFSET_U32 ;
57- CTX_CHECK_AND_ADVANCE (ctx , len );
55+ len_to_push = OFFSET_U32 ;
56+ CTX_CHECK_AND_ADVANCE (ctx , len_to_push );
5857 break ;
5958 }
6059 case INTEGER_I64 :
6160 case INTEGER_U64 : {
62- len = OFFSET_U64 ;
63- CTX_CHECK_AND_ADVANCE (ctx , len );
61+ len_to_push = OFFSET_U64 ;
62+ CTX_CHECK_AND_ADVANCE (ctx , len_to_push );
6463 break ;
6564 }
6665 case INTEGER_I128 :
6766 case INTEGER_U128 : {
68- len = OFFSET_U64 * 2 ;
69- CTX_CHECK_AND_ADVANCE (ctx , len );
67+ len_to_push = OFFSET_U64 * 2 ;
68+ CTX_CHECK_AND_ADVANCE (ctx , len_to_push );
7069 break ;
7170 }
7271 default :
@@ -92,7 +91,7 @@ parser_error_t schema_display_integer(parser_context_t *ctx, primitive_integer_t
9291 if (ctx -> offset + primitive -> display .fixed_point .from_sibling_field .byte_offset >= ctx -> buffer .len ) {
9392 return parser_unexpected_buffer_end ;
9493 }
95- len += primitive -> display .fixed_point .from_sibling_field .byte_offset + 1 ;
94+ len_to_push += primitive -> display .fixed_point .from_sibling_field .byte_offset + 1 ;
9695 break ;
9796 default :
9897 return parser_unexpected_type ;
@@ -101,49 +100,48 @@ parser_error_t schema_display_integer(parser_context_t *ctx, primitive_integer_t
101100 default :
102101 return parser_unexpected_type ;
103102 }
104- ctx_bytes .buffer .len = len ;
105-
106- set_data_context (& ctx_bytes );
103+ ctx_to_push -> buffer .ptr = ctx_mem ;
104+ ctx_to_push -> buffer .len = len_to_push ;
107105
108106 return parser_ok ;
109107}
110108
111- parser_error_t schema_display_byte_array (parser_context_t * ctx , primitive_byte_array_t * byte_array ) {
109+ parser_error_t schema_display_byte_array (parser_context_t * ctx , primitive_byte_array_t * byte_array , parser_context_t * ctx_to_push ) {
112110 CHECK_INPUT (ctx );
113111 CHECK_INPUT (byte_array );
112+ CHECK_INPUT (ctx_to_push );
114113
115- parser_context_t ctx_bytes = {0 };
116- ctx_bytes .buffer .len = byte_array -> len ;
117- ctx_bytes .buffer .ptr = ctx -> buffer .ptr + ctx -> offset ;
118- CTX_CHECK_AND_ADVANCE (ctx , byte_array -> len )
114+ const uint8_t * ctx_mem = ctx -> buffer .ptr + ctx -> offset ;
115+ uint16_t len_to_push = byte_array -> len ;
116+ CTX_CHECK_AND_ADVANCE (ctx , len_to_push );
119117
120118 if (byte_array -> len > MAX_INPUT_CHUNK ) {
121119 return parser_ui_buffer_too_small ;
122120 }
123121
124- set_data_context (& ctx_bytes );
122+ ctx_to_push -> buffer .ptr = ctx_mem ;
123+ ctx_to_push -> buffer .len = len_to_push ;
125124
126125 return parser_ok ;
127126}
128127
129- parser_error_t schema_display_primitive (parser_context_t * ctx , primitive_t * primitive ) {
128+ parser_error_t schema_display_primitive (parser_context_t * ctx , parser_tx_t * txObj , primitive_t * primitive ) {
130129 CHECK_INPUT (ctx );
131130
132131 uint16_t primitive_size = sizeof (primitive );
133132 print_u16 ("Primitive size: " , primitive_size );
134133
135- set_primitive (primitive );
136-
134+ parser_context_t ctx_to_push = {0 };
137135 switch (primitive -> type ) {
138136 case PRIMITIVE_INTEGER :
139137 primitive_size = sizeof (primitive -> integer );
140138 print_u16 ("Primitive size integer_0: " , primitive_size );
141- CHECK_ERROR (schema_display_integer (ctx , & primitive -> integer ));
139+ CHECK_ERROR (schema_display_integer (ctx , & primitive -> integer , & ctx_to_push ));
142140 break ;
143141 case PRIMITIVE_BYTE_ARRAY :
144142 primitive_size = sizeof (primitive -> byte_array );
145143 print_u16 ("Primitive size byte_array: " , primitive_size );
146- CHECK_ERROR (schema_display_byte_array (ctx , & primitive -> byte_array ));
144+ CHECK_ERROR (schema_display_byte_array (ctx , & primitive -> byte_array , & ctx_to_push ));
147145 break ;
148146 case PRIMITIVE_BYTE_VEC :
149147 // TODO: Implement me
@@ -170,6 +168,9 @@ parser_error_t schema_display_primitive(parser_context_t *ctx, primitive_t *prim
170168 return parser_unexpected_type ;
171169 }
172170
171+ if (is_enable_push_item ()) {
172+ CHECK_ERROR (push_item (txObj , primitive , & ctx_to_push ));
173+ }
173174 return parser_ok ;
174175}
175176
@@ -209,8 +210,9 @@ parser_error_t schema_display_enum(parser_context_t *ctx, parser_tx_t *txObj) {
209210 ctx_bytes .buffer = variant .name ;
210211 primitive_t primitive = {0 };
211212 primitive .type = PRIMITIVE_STRING ;
212- set_primitive (& primitive );
213- set_data_context (& ctx_bytes );
213+ if (is_enable_push_item ()) {
214+ CHECK_ERROR (push_item_string (txObj , variant .name .ptr , variant .name .len ));
215+ }
214216 }
215217
216218 return parser_ok ;
@@ -229,7 +231,7 @@ parser_error_t schema_display_struct(parser_context_t *ctx, parser_tx_t *txObj)
229231 MEMZERO (& named_field , sizeof (named_field_t ));
230232 CHECK_ERROR (read_named_field (& struct_type .named_fields , & named_field ));
231233
232- bool try_push = false;
234+ bool remove_title = false;
233235 set_enable_push_item (true);
234236 if (!named_field .silent || !is_link_skip (& named_field .value )) {
235237 if (!named_field .is_expert || ui_expert_mode ) {
@@ -239,7 +241,7 @@ parser_error_t schema_display_struct(parser_context_t *ctx, parser_tx_t *txObj)
239241 uint16_t len = strlen (structured_show_as );
240242 if (len > 0 ) {
241243 CHECK_ERROR (append_item_title (structured_show_as , len ));
242- try_push = true;
244+ remove_title = true;
243245 }
244246 } else {
245247 set_enable_push_item (false);
@@ -251,36 +253,27 @@ parser_error_t schema_display_struct(parser_context_t *ctx, parser_tx_t *txObj)
251253 CHECK_ERROR (schema_display_generic_by_index (ctx , txObj , named_field .value .data .by_index ));
252254 break ;
253255 case LINK_IMMEDIATE :
254- CHECK_ERROR (schema_display_primitive (ctx , & named_field .value .data .immediate ));
256+ CHECK_ERROR (schema_display_primitive (ctx , txObj , & named_field .value .data .immediate ));
255257 break ;
256258 default :
257259 print_string ("schema_display_struct IMPLEMENT ME 1" );
258260 return parser_unexpected_type ;
259261 }
260-
261- if (try_push ) {
262- if (!is_data_context_empty ()) {
263- CHECK_ERROR (push_item (txObj ));
264- CHECK_ERROR (remove_last_item_title ());
265- }
266- } else {
267- // TODO: check if this is correct
262+ if (remove_title ) {
263+ CHECK_ERROR (remove_last_item_title ());
268264 }
269265 }
270266 } else {
271267 for (uint32_t i = 0 ; i < struct_type .fields_qty ; i ++ ) {
272268 MEMZERO (& named_field , sizeof (named_field_t ));
273269 CHECK_ERROR (read_named_field (& struct_type .named_fields , & named_field ));
274- bool remove_variant = false;
270+ bool remove_title = false;
275271 if ((!named_field .silent && !is_link_skip (& named_field .value ) && !named_field .is_expert ) || ui_expert_mode ) {
276272 CHECK_ERROR (append_item_title ((char * )named_field .display_name .ptr , named_field .display_name .len ));
277- remove_variant = true;
273+ remove_title = true;
278274 }
279275 CHECK_ERROR (schema_display_generic_by_index (ctx , txObj , named_field .value .data .by_index ));
280- if (remove_variant ) {
281- if (!is_data_context_empty ()) {
282- CHECK_ERROR (push_item (txObj ));
283- }
276+ if (remove_title ) {
284277 CHECK_ERROR (remove_last_item_title ());
285278 }
286279 }
@@ -315,29 +308,27 @@ parser_error_t schema_display_tuple(parser_context_t *ctx, parser_tx_t *txObj) {
315308 for (uint32_t i = 0 ; i < tuple_type .fields_qty ; i ++ ) {
316309 MEMZERO (& unnamed_field , sizeof (unnamed_field_t ));
317310 CHECK_ERROR (read_unnamed_field (& tuple_type .unnamed_fields , & unnamed_field ));
311+
312+ bool remove_title = false;
313+ if (tuple_type .fields_qty > 1 ) {
314+ if (!unnamed_field .is_expert || ui_expert_mode ) {
315+ CHECK_ERROR (append_item_title_index (i ));
316+ remove_title = true;
317+ }
318+ }
318319 switch (unnamed_field .value .tag ) {
319320 case LINK_BY_INDEX :
320321 CHECK_ERROR (schema_display_generic_by_index (ctx , txObj , unnamed_field .value .data .by_index ));
321322 break ;
322323 case LINK_IMMEDIATE :
323- CHECK_ERROR (schema_display_primitive (ctx , & unnamed_field .value .data .immediate ));
324+ CHECK_ERROR (schema_display_primitive (ctx , txObj , & unnamed_field .value .data .immediate ));
324325 break ;
325326 default :
326327 print_string ("Tuple field is not a link by index\n" );
327328 return parser_unexpected_type ;
328329 }
329- if (tuple_type .fields_qty == 1 ) {
330- return parser_ok ;
331- }
332-
333- if (!unnamed_field .is_expert || ui_expert_mode ) {
334- char index_str [12 ] = {0 };
335- snprintf (index_str , sizeof (index_str ), "%u" , i );
336- CHECK_ERROR (append_item_title (index_str , strlen (index_str )));
337- if (!is_data_context_empty ()) {
338- CHECK_ERROR (push_item (txObj ));
339- CHECK_ERROR (remove_last_item_title ());
340- }
330+ if (remove_title ) {
331+ CHECK_ERROR (remove_last_item_title ());
341332 }
342333 }
343334 }
@@ -352,13 +343,9 @@ parser_error_t schema_display_option(parser_context_t *ctx, parser_tx_t *txObj)
352343 uint8_t discriminant = 0 ;
353344 CHECK_ERROR (read_u8 (ctx , & discriminant ));
354345 if (discriminant == 0 ) {
355- parser_context_t ctx_bytes = {0 };
356- ctx_bytes .buffer .ptr = (uint8_t * )NONE_STRING ;
357- ctx_bytes .buffer .len = strlen (NONE_STRING );
358- primitive_t primitive = {0 };
359- primitive .type = PRIMITIVE_STRING ;
360- set_primitive (& primitive );
361- set_data_context (& ctx_bytes );
346+ if (is_enable_push_item ()) {
347+ CHECK_ERROR (push_item_string (txObj , NONE_STRING , strlen (NONE_STRING )));
348+ }
362349 CHECK_ERROR (schema_reset_leaf_offset (& txObj -> merkle_proofs .leaves ));
363350 return parser_ok ;
364351 }
@@ -372,7 +359,7 @@ parser_error_t schema_display_option(parser_context_t *ctx, parser_tx_t *txObj)
372359 CHECK_ERROR (schema_display_generic_by_index (ctx , txObj , option_type .value .data .by_index ));
373360 break ;
374361 case LINK_IMMEDIATE :
375- CHECK_ERROR (schema_display_primitive (ctx , & option_type .value .data .immediate ));
362+ CHECK_ERROR (schema_display_primitive (ctx , txObj , & option_type .value .data .immediate ));
376363 break ;
377364 default :
378365 print_string ("Option field is not a link by index\n" );
@@ -391,32 +378,21 @@ parser_error_t schema_display_array(parser_context_t *ctx, parser_tx_t *txObj) {
391378 CHECK_ERROR (schema_reset_leaf_offset (& txObj -> merkle_proofs .leaves ));
392379
393380 for (uint32_t i = 0 ; i < array_type .len ; i ++ ) {
394- char index_str [12 ];
395- snprintf (index_str , sizeof (index_str ), "%d" , i );
396- CHECK_ERROR (append_item_title (index_str , strlen (index_str )));
381+ CHECK_ERROR (append_item_title_index (i ));
397382 switch (array_type .value .tag ) {
398383 case LINK_BY_INDEX :
399384 CHECK_ERROR (schema_display_generic_by_index (ctx , txObj , array_type .value .data .by_index ));
400- if (!is_data_context_empty ()) {
401- CHECK_ERROR (push_item (txObj ));
402- CHECK_ERROR (remove_last_item_title ());
403- }
404385 break ;
405386 case LINK_IMMEDIATE : {
406- CHECK_ERROR (schema_display_primitive (ctx , & array_type .value .data .immediate ));
407- if (!is_data_context_empty ()) {
408- CHECK_ERROR (push_item (txObj ));
409- CHECK_ERROR (remove_last_item_title ());
410- if (i == array_type .len - 1 ) {
411- CHECK_ERROR (remove_last_item_title ());
412- }
413- }
387+ CHECK_ERROR (schema_display_primitive (ctx , txObj , & array_type .value .data .immediate ));
414388 break ;
415389 }
416390 default :
417391 print_string ("Array field is not a link by index\n" );
418392 return parser_unexpected_type ;
419393 }
394+
395+ CHECK_ERROR (remove_last_item_title ())
420396 }
421397
422398 return parser_ok ;
@@ -441,17 +417,7 @@ parser_error_t schema_display_vec(parser_context_t *ctx, parser_tx_t *txObj) {
441417 CHECK_ERROR (schema_display_generic_by_index (ctx , txObj , vec_type .value .data .by_index ));
442418 break ;
443419 case LINK_IMMEDIATE : {
444- char index_str [12 ];
445- snprintf (index_str , sizeof (index_str ), "%d" , i );
446- CHECK_ERROR (append_item_title (index_str , strlen (index_str )));
447- CHECK_ERROR (schema_display_primitive (ctx , & vec_type .value .data .immediate ));
448- if (!is_data_context_empty ()) {
449- CHECK_ERROR (push_item (txObj ));
450- CHECK_ERROR (remove_last_item_title ());
451- if (i == vec_len - 1 ) {
452- CHECK_ERROR (remove_last_item_title ());
453- }
454- }
420+ CHECK_ERROR (schema_display_primitive (ctx , txObj , & vec_type .value .data .immediate ));
455421 break ;
456422 }
457423 default :
0 commit comments