Skip to content

Commit 4d2b44f

Browse files
committed
Added toggle for print out structs names
1 parent afa92be commit 4d2b44f

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

crates/types/src/output_preimage.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ use crate::error::Error;
99
pub fn print_output_preimage(
1010
preimage_path: &PathBuf,
1111
compiled_module_path: &PathBuf,
12+
) -> Result<(), Error> {
13+
print_output_preimage_with_options(preimage_path, compiled_module_path, false)
14+
}
15+
16+
/// Deserialize and pretty print output preimage using ABI from contract class
17+
/// with options to control formatting
18+
pub fn print_output_preimage_with_options(
19+
preimage_path: &PathBuf,
20+
compiled_module_path: &PathBuf,
21+
show_struct_names: bool,
1222
) -> Result<(), Error> {
1323
println!("\nOutput Preimage (deserialized via ABI):");
1424
println!("{}", "=".repeat(80));
@@ -50,13 +60,14 @@ pub fn print_output_preimage(
5060

5161
// Deserialize based on ABI
5262
if let Some(output_type) = abi_output_type {
53-
deserialize_with_abi_full(&preimage_data, &output_type, full_abi.as_ref())?;
63+
deserialize_with_abi_full(&preimage_data, &output_type, full_abi.as_ref(), show_struct_names)?;
5464
} else {
5565
// Fallback to basic pretty printing if ABI not found
5666
println!(" Warning: Could not find output type in ABI, using basic format");
5767
print_basic_format(&preimage_data);
5868
}
5969

70+
println!();
6071
println!("{}", "=".repeat(80));
6172
println!();
6273

@@ -130,6 +141,7 @@ fn deserialize_with_abi_full(
130141
preimage_data: &[Felt252],
131142
output_type: &Value,
132143
full_abi: Option<&Value>,
144+
show_struct_names: bool,
133145
) -> Result<(), Error> {
134146
// Get the contract class to find struct definitions
135147
// We need to get the full ABI to resolve struct types
@@ -149,7 +161,7 @@ fn deserialize_with_abi_full(
149161
.to_string();
150162

151163
// Print with custom formatter that preserves order and adds bold formatting
152-
print_formatted_struct(&struct_name, &deserialized, full_abi, 0)?;
164+
print_formatted_struct(&struct_name, &deserialized, full_abi, 0, show_struct_names)?;
153165
return Ok(());
154166
}
155167
Err(e) => {
@@ -412,13 +424,18 @@ fn print_formatted_struct(
412424
value: &Value,
413425
abi: Option<&Value>,
414426
indent: usize,
427+
show_struct_names: bool,
415428
) -> Result<(), Error> {
416429
let indent_str = " ".repeat(indent);
417430

418431
// If it's an object (struct), print with preserved order
419432
if let Value::Object(map) = value {
420-
// Print struct name with the given indent level
421-
print!("{}{}{}{} {{", indent_str, BOLD, struct_name, RESET);
433+
// Print struct name with the given indent level (if enabled)
434+
if show_struct_names {
435+
print!("{}{}{}{} {{", indent_str, BOLD, struct_name, RESET);
436+
} else {
437+
print!("{}{{", indent_str);
438+
}
422439

423440
if map.is_empty() {
424441
print!(" }}");
@@ -446,7 +463,7 @@ fn print_formatted_struct(
446463
print!("{} \"{}\": ", field_indent_str, name);
447464
// Field values should be at the same indent as the field name
448465
// Fields are at indent+1, so pass indent+1 to the formatter
449-
print_formatted_value(field_value, abi, indent + 1)?;
466+
print_formatted_value(field_value, abi, indent + 1, show_struct_names)?;
450467
if idx < members.len() - 1 {
451468
println!(",");
452469
} else {
@@ -469,7 +486,7 @@ fn print_formatted_struct(
469486
for (idx, (key, val)) in entries.iter().enumerate() {
470487
print!("{} \"{}\": ", field_indent_str, key);
471488
// Fields are at indent+1, so pass indent+1 to the formatter
472-
print_formatted_value(val, abi, indent + 1)?;
489+
print_formatted_value(val, abi, indent + 1, show_struct_names)?;
473490
if idx < entries.len() - 1 {
474491
println!(",");
475492
} else {
@@ -479,13 +496,13 @@ fn print_formatted_struct(
479496
print!("{}}}", indent_str);
480497
} else {
481498
// Not a struct, just print the value
482-
print_formatted_value(value, abi, indent)?;
499+
print_formatted_value(value, abi, indent, show_struct_names)?;
483500
}
484501

485502
Ok(())
486503
}
487504

488-
fn print_formatted_value(value: &Value, abi: Option<&Value>, indent: usize) -> Result<(), Error> {
505+
fn print_formatted_value(value: &Value, abi: Option<&Value>, indent: usize, show_struct_names: bool) -> Result<(), Error> {
489506
match value {
490507
Value::Null => print!("{}null{}", BOLD, RESET),
491508
Value::Bool(b) => print!("{}{}{}", BOLD, b, RESET),
@@ -520,7 +537,7 @@ fn print_formatted_value(value: &Value, abi: Option<&Value>, indent: usize) -> R
520537
// Print small numeric arrays on one line
521538
print!("[");
522539
for (idx, item) in arr.iter().enumerate() {
523-
print_formatted_value(item, abi, indent)?;
540+
print_formatted_value(item, abi, indent, show_struct_names)?;
524541
if idx < arr.len() - 1 {
525542
print!(", ");
526543
}
@@ -539,7 +556,7 @@ fn print_formatted_value(value: &Value, abi: Option<&Value>, indent: usize) -> R
539556
print!("{} ", indent_str);
540557
}
541558

542-
print_formatted_value(item, abi, indent)?;
559+
print_formatted_value(item, abi, indent, show_struct_names)?;
543560

544561
if idx < arr.len() - 1 {
545562
if is_line_end {
@@ -562,7 +579,7 @@ fn print_formatted_value(value: &Value, abi: Option<&Value>, indent: usize) -> R
562579
for (idx, item) in arr.iter().enumerate() {
563580
// For array elements, we print them at element_indent level
564581
// The formatter should use this indent level directly
565-
print_formatted_value(item, abi, element_indent)?;
582+
print_formatted_value(item, abi, element_indent, show_struct_names)?;
566583
if idx < arr.len() - 1 {
567584
println!(",");
568585
} else {
@@ -612,7 +629,7 @@ fn print_formatted_value(value: &Value, abi: Option<&Value>, indent: usize) -> R
612629
};
613630

614631
// Use the struct formatter for nested structs
615-
print_formatted_struct(&struct_name, value, abi, indent)?;
632+
print_formatted_struct(&struct_name, value, abi, indent, show_struct_names)?;
616633
}
617634
}
618635
Ok(())

0 commit comments

Comments
 (0)