foundry_common_fmt/
eof.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use comfy_table::{ContentArrangement, Table};
use revm_primitives::{
    eof::{EofBody, EofHeader},
    Eof,
};
use std::fmt::{self, Write};

pub fn pretty_eof(eof: &Eof) -> Result<String, fmt::Error> {
    let Eof {
        header:
            EofHeader {
                types_size,
                code_sizes,
                container_sizes,
                data_size,
                sum_code_sizes: _,
                sum_container_sizes: _,
            },
        body:
            EofBody { types_section, code_section, container_section, data_section, is_data_filled: _ },
        raw: _,
    } = eof;

    let mut result = String::new();

    let mut table = Table::new();
    table.add_row(vec!["type_size", &types_size.to_string()]);
    table.add_row(vec!["num_code_sections", &code_sizes.len().to_string()]);
    if !code_sizes.is_empty() {
        table.add_row(vec!["code_sizes", &format!("{code_sizes:?}")]);
    }
    table.add_row(vec!["num_container_sections", &container_sizes.len().to_string()]);
    if !container_sizes.is_empty() {
        table.add_row(vec!["container_sizes", &format!("{container_sizes:?}")]);
    }
    table.add_row(vec!["data_size", &data_size.to_string()]);

    write!(result, "Header:\n{table}")?;

    if !code_section.is_empty() {
        let mut table = Table::new();
        table.set_content_arrangement(ContentArrangement::Dynamic);
        table.set_header(vec!["", "Inputs", "Outputs", "Max stack height", "Code"]);
        for (idx, (code, type_section)) in code_section.iter().zip(types_section).enumerate() {
            table.add_row(vec![
                &idx.to_string(),
                &type_section.inputs.to_string(),
                &type_section.outputs.to_string(),
                &type_section.max_stack_size.to_string(),
                &code.to_string(),
            ]);
        }

        write!(result, "\n\nCode sections:\n{table}")?;
    }

    if !container_section.is_empty() {
        let mut table = Table::new();
        table.set_content_arrangement(ContentArrangement::Dynamic);
        for (idx, container) in container_section.iter().enumerate() {
            table.add_row(vec![&idx.to_string(), &container.to_string()]);
        }

        write!(result, "\n\nContainer sections:\n{table}")?;
    }

    if !data_section.is_empty() {
        let mut table = Table::new();
        table.set_content_arrangement(ContentArrangement::Dynamic);
        table.add_row(vec![&data_section.to_string()]);
        write!(result, "\n\nData section:\n{table}")?;
    }

    Ok(result)
}