Skip to main content

provekit_common/utils/
print_abi.rs

1use {
2    noirc_abi::{Abi, AbiType, AbiVisibility, Sign},
3    std::fmt::{Display, Formatter, Result},
4};
5
6pub struct PrintAbi<'a>(pub &'a Abi);
7
8pub struct PrintType<'a>(pub &'a AbiType);
9
10impl Display for PrintAbi<'_> {
11    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
12        write!(f, "(")?;
13        for (i, param) in self.0.parameters.iter().enumerate() {
14            write!(f, "{}: ", param.name)?;
15            match param.visibility {
16                AbiVisibility::Public => write!(f, "pub ")?,
17                AbiVisibility::Private => {}
18                AbiVisibility::DataBus => write!(f, "data_bus ")?,
19            }
20            write!(f, "{}", PrintType(&param.typ))?;
21            if i < self.0.parameters.len() - 1 {
22                write!(f, ", ")?;
23            }
24        }
25        write!(f, ")")
26    }
27}
28
29impl Display for PrintType<'_> {
30    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
31        match self.0 {
32            AbiType::Field => write!(f, "Field"),
33            AbiType::Boolean => write!(f, "bool"),
34            AbiType::Integer { sign, width } => match sign {
35                Sign::Signed => write!(f, "i{width}"),
36                Sign::Unsigned => write!(f, "u{width}"),
37            },
38            AbiType::String { length } => write!(f, "str<{length}>"),
39            AbiType::Array { length, typ } => write!(f, "[{}; {length}]", PrintType(typ)),
40            AbiType::Tuple { fields } => {
41                write!(f, "(")?;
42                for (idx, typ) in fields.iter().enumerate() {
43                    write!(f, "{}", PrintType(typ))?;
44                    if idx < fields.len() - 1 {
45                        write!(f, ", ")?;
46                    }
47                }
48                write!(f, ")")
49            }
50            AbiType::Struct { path, fields } => {
51                write!(f, "{path} {{")?;
52                for (idx, (name, typ)) in fields.iter().enumerate() {
53                    write!(f, "{}: {}", name, PrintType(typ))?;
54                    if idx < fields.len() - 1 {
55                        write!(f, ", ")?;
56                    }
57                }
58                write!(f, "}}")
59            }
60        }
61    }
62}