|
1 |
| -use cairo_lang_runner::casm_run::format_next_item; |
| 1 | +use starknet_api::execution_utils::format_panic_data; |
2 | 2 | use starknet_types_core::felt::Felt;
|
3 | 3 |
|
4 |
| -/// Helper function to build readable text from a run data. |
| 4 | +/// Helper function to build readable text from run data. |
5 | 5 | #[must_use]
|
6 | 6 | pub fn build_readable_text(data: &[Felt]) -> Option<String> {
|
7 |
| - let mut data_iter = data.iter().copied(); |
8 |
| - let mut items = Vec::new(); |
9 |
| - |
10 |
| - while let Some(item) = format_next_item(&mut data_iter) { |
11 |
| - items.push(item.quote_if_string()); |
12 |
| - } |
13 |
| - |
14 |
| - if items.is_empty() { |
| 7 | + if data.is_empty() { |
15 | 8 | return None;
|
16 | 9 | }
|
17 | 10 |
|
18 |
| - let string = if let [item] = &items[..] { |
19 |
| - item.clone() |
20 |
| - } else { |
21 |
| - format!("({})", items.join(", ")) |
22 |
| - }; |
| 11 | + let string = format_panic_data(data); |
23 | 12 |
|
24 | 13 | let mut result = indent_string(&format!("\n{string}"));
|
25 | 14 | result.push('\n');
|
26 | 15 | Some(result)
|
27 | 16 | }
|
28 | 17 |
|
29 | 18 | fn indent_string(string: &str) -> String {
|
30 |
| - let mut modified_string = string.to_string(); |
31 |
| - let trailing_newline = if string.ends_with('\n') { |
32 |
| - modified_string.pop(); |
33 |
| - true |
34 |
| - } else { |
35 |
| - false |
36 |
| - }; |
| 19 | + let without_trailing = string.strip_suffix('\n').unwrap_or(string); |
| 20 | + let indented = without_trailing.replace('\n', "\n "); |
| 21 | + let should_append_newline = string.ends_with('\n'); |
37 | 22 |
|
38 |
| - modified_string = modified_string.replace('\n', "\n "); |
39 |
| - if trailing_newline { |
40 |
| - modified_string.push('\n'); |
| 23 | + if should_append_newline { |
| 24 | + format!("{indented}\n") |
| 25 | + } else { |
| 26 | + indented |
41 | 27 | }
|
42 |
| - |
43 |
| - modified_string |
44 | 28 | }
|
45 | 29 |
|
46 | 30 | #[cfg(test)]
|
|
0 commit comments