foundry_debugger/
builder.rs1use crate::{DebugNode, Debugger, node::flatten_call_trace};
4use alloy_primitives::{Address, map::AddressHashMap};
5use foundry_common::get_contract_name;
6use foundry_evm_core::Breakpoints;
7use foundry_evm_traces::{CallTraceArena, CallTraceDecoder, Traces, debug::ContractSources};
8
9#[derive(Debug, Default)]
11#[must_use = "builders do nothing unless you call `build` on them"]
12pub struct DebuggerBuilder {
13 debug_arena: Vec<DebugNode>,
15 identified_contracts: AddressHashMap<String>,
17 sources: ContractSources,
19 breakpoints: Breakpoints,
21}
22
23impl DebuggerBuilder {
24 #[inline]
26 pub fn new() -> Self {
27 Self::default()
28 }
29
30 #[inline]
32 pub fn traces(mut self, traces: Traces) -> Self {
33 for (_, arena) in traces {
34 self = self.trace_arena(arena.arena);
35 }
36 self
37 }
38
39 #[inline]
41 pub fn trace_arena(mut self, arena: CallTraceArena) -> Self {
42 flatten_call_trace(arena, &mut self.debug_arena);
43 self
44 }
45
46 #[inline]
48 pub fn decoders(mut self, decoders: &[CallTraceDecoder]) -> Self {
49 for decoder in decoders {
50 self = self.decoder(decoder);
51 }
52 self
53 }
54
55 #[inline]
57 pub fn decoder(self, decoder: &CallTraceDecoder) -> Self {
58 let c = decoder.contracts.iter().map(|(k, v)| (*k, get_contract_name(v).to_string()));
59 self.identified_contracts(c)
60 }
61
62 #[inline]
64 pub fn identified_contracts(
65 mut self,
66 identified_contracts: impl IntoIterator<Item = (Address, String)>,
67 ) -> Self {
68 self.identified_contracts.extend(identified_contracts);
69 self
70 }
71
72 #[inline]
74 pub fn sources(mut self, sources: ContractSources) -> Self {
75 self.sources = sources;
76 self
77 }
78
79 #[inline]
81 pub fn breakpoints(mut self, breakpoints: Breakpoints) -> Self {
82 self.breakpoints = breakpoints;
83 self
84 }
85
86 #[inline]
88 pub fn build(self) -> Debugger {
89 let Self { debug_arena, identified_contracts, sources, breakpoints } = self;
90 Debugger::new(debug_arena, identified_contracts, sources, breakpoints)
91 }
92}