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