aether/debugger/
breakpoint.rs1#[derive(Debug, Clone, PartialEq)]
6pub enum BreakpointType {
7 Line { file: String, line: usize },
9 Function { name: String },
11 Conditional {
13 file: String,
14 line: usize,
15 condition: String,
16 },
17}
18
19#[derive(Debug, Clone, PartialEq)]
21pub struct Breakpoint {
22 pub id: usize,
23 pub bp_type: BreakpointType,
24 pub enabled: bool,
25 pub hit_count: usize,
26 pub ignore_count: usize, }
28
29impl Breakpoint {
30 pub fn new(id: usize, bp_type: BreakpointType) -> Self {
32 Breakpoint {
33 id,
34 bp_type,
35 enabled: true,
36 hit_count: 0,
37 ignore_count: 0,
38 }
39 }
40
41 pub fn should_trigger(&mut self, file: &str, line: usize) -> bool {
43 if !self.enabled {
44 return false;
45 }
46
47 fn files_match(a: &str, b: &str) -> bool {
49 if a == b {
50 return true;
51 }
52 fn file_name(p: &str) -> &str {
53 p.rsplit(['/', '\\']).next().unwrap_or(p)
54 }
55 let (na, nb) = (file_name(a), file_name(b));
56 !na.is_empty() && na == nb
57 }
58
59 let matches = match &self.bp_type {
60 BreakpointType::Line { file: f, line: l } => files_match(f, file) && *l == line,
61 BreakpointType::Conditional {
62 file: f, line: l, ..
63 } => files_match(f, file) && *l == line,
64 BreakpointType::Function { .. } => false, };
66
67 if matches && self.hit_count >= self.ignore_count {
68 self.hit_count += 1;
69 true
70 } else if matches {
71 self.hit_count += 1;
72 false
73 } else {
74 false
75 }
76 }
77
78 pub fn is_function_breakpoint(&self, func_name: &str) -> bool {
80 match &self.bp_type {
81 BreakpointType::Function { name } => name == func_name,
82 _ => false,
83 }
84 }
85
86 pub fn get_condition(&self) -> Option<&str> {
88 match &self.bp_type {
89 BreakpointType::Conditional { condition, .. } => Some(condition.as_str()),
90 _ => None,
91 }
92 }
93
94 pub fn location_string(&self) -> String {
96 match &self.bp_type {
97 BreakpointType::Line { file, line } => {
98 format!("{}:{}", file, line)
99 }
100 BreakpointType::Function { name } => {
101 format!("function '{}'", name)
102 }
103 BreakpointType::Conditional {
104 file,
105 line,
106 condition,
107 } => {
108 format!("{}:{} if {}", file, line, condition)
109 }
110 }
111 }
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 #[test]
119 fn test_breakpoint_creation() {
120 let bp = Breakpoint::new(
121 1,
122 BreakpointType::Line {
123 file: "test.aether".to_string(),
124 line: 10,
125 },
126 );
127
128 assert_eq!(bp.id, 1);
129 assert!(bp.enabled);
130 assert_eq!(bp.hit_count, 0);
131 }
132
133 #[test]
134 fn test_breakpoint_trigger() {
135 let mut bp = Breakpoint::new(
136 1,
137 BreakpointType::Line {
138 file: "test.aether".to_string(),
139 line: 10,
140 },
141 );
142
143 assert!(bp.should_trigger("test.aether", 10));
144 assert!(!bp.should_trigger("test.aether", 11));
145 assert!(!bp.should_trigger("other.aether", 10));
146 }
147
148 #[test]
149 fn test_disabled_breakpoint() {
150 let mut bp = Breakpoint::new(
151 1,
152 BreakpointType::Line {
153 file: "test.aether".to_string(),
154 line: 10,
155 },
156 );
157 bp.enabled = false;
158
159 assert!(!bp.should_trigger("test.aether", 10));
160 }
161
162 #[test]
163 fn test_ignore_count() {
164 let mut bp = Breakpoint::new(
165 1,
166 BreakpointType::Line {
167 file: "test.aether".to_string(),
168 line: 10,
169 },
170 );
171 bp.ignore_count = 2;
172
173 assert!(!bp.should_trigger("test.aether", 10)); assert!(!bp.should_trigger("test.aether", 10)); assert!(bp.should_trigger("test.aether", 10)); }
177
178 #[test]
179 fn test_function_breakpoint() {
180 let bp = Breakpoint::new(
181 1,
182 BreakpointType::Function {
183 name: "myFunc".to_string(),
184 },
185 );
186
187 assert!(bp.is_function_breakpoint("myFunc"));
188 assert!(!bp.is_function_breakpoint("otherFunc"));
189 }
190}