dusk_cdf/zkdb/
breakpoint.rs1use std::collections::HashMap;
2
3use crate::Constraint;
4
5#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
7pub struct Breakpoint {
8 pub source: String,
10 pub line: Option<u64>,
13}
14
15impl Breakpoint {
16 pub fn matches(&self, source: &str, line: u64) -> bool {
18 source.contains(&self.source)
19 && match self.line {
20 Some(l) => l == line,
21 None => true,
22 }
23 }
24}
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct Breakpoints {
28 next_id: usize,
29 breakpoints: HashMap<Breakpoint, usize>,
30}
31
32impl Default for Breakpoints {
33 fn default() -> Self {
34 Self {
35 next_id: 1,
36 breakpoints: HashMap::default(),
37 }
38 }
39}
40
41impl Breakpoints {
42 pub fn add(&mut self, source: String, line: Option<u64>) -> usize {
43 let breakpoint = Breakpoint { source, line };
44
45 let id = *self.breakpoints.entry(breakpoint).or_insert(self.next_id);
46
47 if id >= self.next_id {
48 self.next_id += 1;
49 }
50
51 id
52 }
53
54 pub fn remove(&mut self, id: usize) -> Option<Breakpoint> {
55 let removed = self
56 .breakpoints
57 .iter()
58 .find_map(|(breakpoint, idx)| (idx == &id).then_some(breakpoint))
59 .cloned();
60
61 if let Some(b) = &removed {
62 self.breakpoints.remove(b);
63 }
64
65 removed
66 }
67
68 pub fn find_breakpoint<'a>(&self, constraint: &Constraint<'a>) -> Option<usize> {
69 let source = constraint.name();
70 let line = constraint.line();
71
72 self.breakpoints
73 .keys()
74 .find(|b| b.matches(source, line))
75 .and_then(|b| self.breakpoints.get(b).copied())
76 }
77
78 pub fn find_breakpoint_from_id(&self, id: usize) -> Option<&Breakpoint> {
79 self.breakpoints
80 .iter()
81 .find_map(|(b, idx)| (id == *idx).then_some(b))
82 }
83}