depyler-core 3.24.0

Core transpilation engine for the Depyler Python-to-Rust transpiler
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
//! Debugging support for Depyler
//!
//! This module provides debugging features including:
//! - Source map generation
//! - Debug symbol preservation
//! - Debugger integration helpers
//! - Runtime debugging utilities

use crate::hir::{HirFunction, Type};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;

/// Source mapping information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceMap {
    /// Original Python source file
    pub source_file: PathBuf,
    /// Generated Rust file
    pub target_file: PathBuf,
    /// Mapping entries
    pub mappings: Vec<SourceMapping>,
    /// Function mappings
    pub function_map: HashMap<String, FunctionMapping>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceMapping {
    /// Python source location
    pub python_line: usize,
    pub python_column: usize,
    /// Rust target location
    pub rust_line: usize,
    pub rust_column: usize,
    /// Optional symbol name
    pub symbol: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionMapping {
    pub python_name: String,
    pub rust_name: String,
    pub python_start_line: usize,
    pub python_end_line: usize,
    pub rust_start_line: usize,
    pub rust_end_line: usize,
}

/// Debug information generator
pub struct DebugInfoGenerator {
    source_map: SourceMap,
    current_rust_line: usize,
    debug_level: DebugLevel,
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum DebugLevel {
    /// No debug information
    None,
    /// Basic line mappings
    Basic,
    /// Full debug information with symbols
    Full,
}

impl DebugInfoGenerator {
    pub fn new(source_file: PathBuf, target_file: PathBuf, debug_level: DebugLevel) -> Self {
        Self {
            source_map: SourceMap {
                source_file,
                target_file,
                mappings: Vec::new(),
                function_map: HashMap::new(),
            },
            current_rust_line: 1,
            debug_level,
        }
    }

    /// Add a source mapping
    pub fn add_mapping(
        &mut self,
        python_line: usize,
        python_column: usize,
        symbol: Option<String>,
    ) {
        if self.debug_level == DebugLevel::None {
            return;
        }

        self.source_map.mappings.push(SourceMapping {
            python_line,
            python_column,
            rust_line: self.current_rust_line,
            rust_column: 0, // Simplified for now
            symbol,
        });
    }

    /// Add a function mapping
    pub fn add_function_mapping(&mut self, func: &HirFunction, rust_start: usize) {
        if self.debug_level == DebugLevel::None {
            return;
        }

        let rust_end = self.current_rust_line;
        self.source_map.function_map.insert(
            func.name.clone(),
            FunctionMapping {
                python_name: func.name.clone(),
                rust_name: func.name.clone(), // Could be mangled
                python_start_line: 0,         // Would need source location
                python_end_line: 0,
                rust_start_line: rust_start,
                rust_end_line: rust_end,
            },
        );
    }

    /// Increment line counter (for tracking generated code)
    pub fn new_line(&mut self) {
        self.current_rust_line += 1;
    }

    /// Get the source map
    pub fn source_map(&self) -> &SourceMap {
        &self.source_map
    }

    /// Generate debug annotations for a function
    pub fn generate_function_debug(&self, func: &HirFunction) -> String {
        match self.debug_level {
            DebugLevel::None => String::new(),
            DebugLevel::Basic => format!("// Function: {}\n", func.name),
            DebugLevel::Full => {
                format!(
                    "// Function: {} (Python source)\n// Parameters: {:?}\n// Returns: {:?}\n",
                    func.name,
                    func.params.iter().map(|p| &p.name).collect::<Vec<_>>(),
                    func.ret_type
                )
            }
        }
    }

    /// Generate debug print for a variable
    pub fn generate_debug_print(&self, var_name: &str, var_type: &Type) -> String {
        match self.debug_level {
            DebugLevel::None => String::new(),
            DebugLevel::Basic | DebugLevel::Full => match var_type {
                Type::Int | Type::Float | Type::Bool => {
                    format!("eprintln!(\"DEBUG: {} = {{}}\", {});", var_name, var_name)
                }
                Type::String => {
                    format!("eprintln!(\"DEBUG: {} = {{}}\", {});", var_name, var_name)
                }
                _ => {
                    format!("eprintln!(\"DEBUG: {} = {{:?}}\", {});", var_name, var_name)
                }
            },
        }
    }
}

/// Runtime debugging utilities
pub struct DebugRuntime;

impl DebugRuntime {
    /// Generate a breakpoint macro
    pub fn breakpoint() -> &'static str {
        "depyler_breakpoint!()"
    }

    /// Generate an assertion with debug info
    pub fn debug_assert(condition: &str, message: &str) -> String {
        format!("debug_assert!({}, \"{}\");", condition, message)
    }

    /// Generate a trace point
    pub fn trace_point(location: &str) -> String {
        format!("depyler_trace!(\"{}\");", location)
    }
}

/// Debugger integration helpers
pub struct DebuggerIntegration {
    debugger_type: DebuggerType,
}

#[derive(Debug, Clone, Copy)]
pub enum DebuggerType {
    Gdb,
    Lldb,
    RustGdb,
}

impl DebuggerIntegration {
    pub fn new(debugger_type: DebuggerType) -> Self {
        Self { debugger_type }
    }

    /// Generate debugger initialization script
    pub fn generate_init_script(&self, source_map: &SourceMap) -> String {
        match self.debugger_type {
            DebuggerType::Gdb | DebuggerType::RustGdb => self.generate_gdb_script(source_map),
            DebuggerType::Lldb => self.generate_lldb_script(source_map),
        }
    }

    fn generate_gdb_script(&self, source_map: &SourceMap) -> String {
        let mut script = String::new();
        script.push_str("# GDB initialization script for Depyler debugging\n");
        script.push_str("# Source: ");
        script.push_str(&source_map.source_file.display().to_string());
        script.push_str("\n\n");

        // Add source path
        script.push_str("directory .\n");

        // Add function breakpoints
        for mapping in source_map.function_map.values() {
            script.push_str(&format!("break {}\n", mapping.rust_name));
        }

        // Pretty printers for Rust types
        if matches!(self.debugger_type, DebuggerType::RustGdb) {
            script.push_str("\n# Load Rust pretty printers\n");
            script.push_str("python\nimport gdb\n");
            script.push_str("gdb.execute('set print pretty on')\n");
            script.push_str("end\n");
        }

        script
    }

    fn generate_lldb_script(&self, source_map: &SourceMap) -> String {
        let mut script = String::new();
        script.push_str("# LLDB initialization script for Depyler debugging\n");
        script.push_str("# Source: ");
        script.push_str(&source_map.source_file.display().to_string());
        script.push_str("\n\n");

        // Add source mapping
        script.push_str("settings set target.source-map . .\n");

        // Add function breakpoints
        for mapping in source_map.function_map.values() {
            script.push_str(&format!("breakpoint set --name {}\n", mapping.rust_name));
        }

        script
    }
}

/// Debug configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DebugConfig {
    pub debug_level: DebugLevel,
    pub generate_source_map: bool,
    pub preserve_symbols: bool,
    pub debug_prints: bool,
    pub breakpoints: bool,
}

impl Default for DebugConfig {
    fn default() -> Self {
        Self {
            debug_level: DebugLevel::Basic,
            generate_source_map: true,
            preserve_symbols: true,
            debug_prints: false,
            breakpoints: false,
        }
    }
}

/// Helper macros for generated code
pub fn generate_debug_macros() -> String {
    r#"
// Depyler debugging macros
#[macro_export]
macro_rules! depyler_breakpoint {
    () => {
        #[cfg(debug_assertions)]
        {
            eprintln!("BREAKPOINT at {}:{}", file!(), line!());
            // Uncomment to actually break in debugger
            // std::intrinsics::breakpoint();
        }
    };
}

#[macro_export]
macro_rules! depyler_trace {
    ($msg:expr) => {
        #[cfg(debug_assertions)]
        eprintln!("[TRACE] {} at {}:{}", $msg, file!(), line!());
    };
}
"#
    .to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hir::{FunctionProperties, HirParam};
    use smallvec::smallvec;

    // === SourceMap tests ===

    #[test]
    fn test_source_map_new() {
        let sm = SourceMap {
            source_file: PathBuf::from("input.py"),
            target_file: PathBuf::from("output.rs"),
            mappings: vec![],
            function_map: HashMap::new(),
        };
        assert_eq!(sm.source_file, PathBuf::from("input.py"));
        assert_eq!(sm.target_file, PathBuf::from("output.rs"));
        assert!(sm.mappings.is_empty());
    }

    #[test]
    fn test_source_map_clone() {
        let sm = SourceMap {
            source_file: PathBuf::from("a.py"),
            target_file: PathBuf::from("a.rs"),
            mappings: vec![SourceMapping {
                python_line: 1,
                python_column: 0,
                rust_line: 1,
                rust_column: 0,
                symbol: None,
            }],
            function_map: HashMap::new(),
        };
        let cloned = sm.clone();
        assert_eq!(cloned.mappings.len(), 1);
    }

    #[test]
    fn test_source_map_serialize() {
        let sm = SourceMap {
            source_file: PathBuf::from("test.py"),
            target_file: PathBuf::from("test.rs"),
            mappings: vec![],
            function_map: HashMap::new(),
        };
        let json = serde_json::to_string(&sm).unwrap();
        assert!(json.contains("test.py"));
        let deserialized: SourceMap = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.source_file, sm.source_file);
    }

    // === SourceMapping tests ===

    #[test]
    fn test_source_mapping_new() {
        let mapping = SourceMapping {
            python_line: 42,
            python_column: 8,
            rust_line: 100,
            rust_column: 4,
            symbol: Some("my_func".to_string()),
        };
        assert_eq!(mapping.python_line, 42);
        assert_eq!(mapping.rust_line, 100);
        assert_eq!(mapping.symbol, Some("my_func".to_string()));
    }

    #[test]
    fn test_source_mapping_clone() {
        let mapping = SourceMapping {
            python_line: 1,
            python_column: 0,
            rust_line: 5,
            rust_column: 0,
            symbol: None,
        };
        let cloned = mapping.clone();
        assert_eq!(cloned.python_line, mapping.python_line);
    }

    #[test]
    fn test_source_mapping_serialize() {
        let mapping = SourceMapping {
            python_line: 10,
            python_column: 2,
            rust_line: 20,
            rust_column: 4,
            symbol: Some("var".to_string()),
        };
        let json = serde_json::to_string(&mapping).unwrap();
        assert!(json.contains("10"));
        assert!(json.contains("var"));
    }

    // === FunctionMapping tests ===

    #[test]
    fn test_function_mapping_new() {
        let fm = FunctionMapping {
            python_name: "py_func".to_string(),
            rust_name: "rust_func".to_string(),
            python_start_line: 5,
            python_end_line: 15,
            rust_start_line: 10,
            rust_end_line: 30,
        };
        assert_eq!(fm.python_name, "py_func");
        assert_eq!(fm.rust_name, "rust_func");
    }

    #[test]
    fn test_function_mapping_clone() {
        let fm = FunctionMapping {
            python_name: "f".to_string(),
            rust_name: "f".to_string(),
            python_start_line: 1,
            python_end_line: 2,
            rust_start_line: 3,
            rust_end_line: 4,
        };
        let cloned = fm.clone();
        assert_eq!(cloned.python_name, fm.python_name);
    }

    // === DebugLevel tests ===

    #[test]
    fn test_debug_level_none() {
        assert_eq!(DebugLevel::None, DebugLevel::None);
        assert_ne!(DebugLevel::None, DebugLevel::Basic);
    }

    #[test]
    fn test_debug_level_basic() {
        assert_eq!(DebugLevel::Basic, DebugLevel::Basic);
    }

    #[test]
    fn test_debug_level_full() {
        let level = DebugLevel::Full;
        let cloned = level;
        assert_eq!(cloned, DebugLevel::Full);
    }

    #[test]
    fn test_debug_level_serialize() {
        let level = DebugLevel::Full;
        let json = serde_json::to_string(&level).unwrap();
        let deserialized: DebugLevel = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, level);
    }

    // === DebugInfoGenerator tests ===

    #[test]
    fn test_debug_info_generator_new() {
        let gen = DebugInfoGenerator::new(
            PathBuf::from("src.py"),
            PathBuf::from("src.rs"),
            DebugLevel::Basic,
        );
        assert_eq!(gen.current_rust_line, 1);
        assert_eq!(gen.debug_level, DebugLevel::Basic);
    }

    #[test]
    fn test_debug_info_generator_add_mapping_none_level() {
        let mut gen = DebugInfoGenerator::new(
            PathBuf::from("a.py"),
            PathBuf::from("a.rs"),
            DebugLevel::None,
        );
        gen.add_mapping(10, 0, Some("test".to_string()));
        // Should not add mappings when debug level is None
        assert!(gen.source_map().mappings.is_empty());
    }

    #[test]
    fn test_debug_info_generator_new_line() {
        let mut gen = DebugInfoGenerator::new(
            PathBuf::from("a.py"),
            PathBuf::from("a.rs"),
            DebugLevel::Basic,
        );
        assert_eq!(gen.current_rust_line, 1);
        gen.new_line();
        assert_eq!(gen.current_rust_line, 2);
        gen.new_line();
        gen.new_line();
        assert_eq!(gen.current_rust_line, 4);
    }

    #[test]
    fn test_debug_info_generator_source_map_getter() {
        let gen = DebugInfoGenerator::new(
            PathBuf::from("test.py"),
            PathBuf::from("test.rs"),
            DebugLevel::Full,
        );
        let sm = gen.source_map();
        assert_eq!(sm.source_file, PathBuf::from("test.py"));
    }

    #[test]
    fn test_generate_function_debug_none_level() {
        let gen = DebugInfoGenerator::new(
            PathBuf::from("a.py"),
            PathBuf::from("a.rs"),
            DebugLevel::None,
        );
        let func = HirFunction {
            name: "test".to_string(),
            params: smallvec![],
            ret_type: Type::None,
            body: vec![],
            properties: FunctionProperties::default(),
            annotations: depyler_annotations::TranspilationAnnotations::default(),
            docstring: None,
        };
        let debug = gen.generate_function_debug(&func);
        assert!(debug.is_empty());
    }

    #[test]
    fn test_generate_function_debug_basic_level() {
        let gen = DebugInfoGenerator::new(
            PathBuf::from("a.py"),
            PathBuf::from("a.rs"),
            DebugLevel::Basic,
        );
        let func = HirFunction {
            name: "my_func".to_string(),
            params: smallvec![],
            ret_type: Type::Int,
            body: vec![],
            properties: FunctionProperties::default(),
            annotations: depyler_annotations::TranspilationAnnotations::default(),
            docstring: None,
        };
        let debug = gen.generate_function_debug(&func);
        assert!(debug.contains("// Function: my_func"));
        assert!(!debug.contains("Parameters")); // Basic doesn't include params
    }

    #[test]
    fn test_generate_function_debug_full_level() {
        let gen = DebugInfoGenerator::new(
            PathBuf::from("a.py"),
            PathBuf::from("a.rs"),
            DebugLevel::Full,
        );
        let func = HirFunction {
            name: "calc".to_string(),
            params: smallvec![HirParam::new("x".to_string(), Type::Int)],
            ret_type: Type::Int,
            body: vec![],
            properties: FunctionProperties::default(),
            annotations: depyler_annotations::TranspilationAnnotations::default(),
            docstring: None,
        };
        let debug = gen.generate_function_debug(&func);
        assert!(debug.contains("// Function: calc"));
        assert!(debug.contains("Parameters"));
        assert!(debug.contains("Returns"));
    }

    #[test]
    fn test_generate_debug_print_none_level() {
        let gen = DebugInfoGenerator::new(
            PathBuf::from("a.py"),
            PathBuf::from("a.rs"),
            DebugLevel::None,
        );
        let debug = gen.generate_debug_print("x", &Type::Int);
        assert!(debug.is_empty());
    }

    #[test]
    fn test_generate_debug_print_float() {
        let gen = DebugInfoGenerator::new(
            PathBuf::from("a.py"),
            PathBuf::from("a.rs"),
            DebugLevel::Basic,
        );
        let debug = gen.generate_debug_print("value", &Type::Float);
        assert!(debug.contains("eprintln!"));
        assert!(debug.contains("value = {}"));
    }

    #[test]
    fn test_generate_debug_print_bool() {
        let gen = DebugInfoGenerator::new(
            PathBuf::from("a.py"),
            PathBuf::from("a.rs"),
            DebugLevel::Full,
        );
        let debug = gen.generate_debug_print("flag", &Type::Bool);
        assert!(debug.contains("flag = {}"));
    }

    #[test]
    fn test_generate_debug_print_string() {
        let gen = DebugInfoGenerator::new(
            PathBuf::from("a.py"),
            PathBuf::from("a.rs"),
            DebugLevel::Basic,
        );
        let debug = gen.generate_debug_print("name", &Type::String);
        assert!(debug.contains("name = {}"));
    }

    #[test]
    fn test_add_function_mapping_none_level() {
        let mut gen = DebugInfoGenerator::new(
            PathBuf::from("a.py"),
            PathBuf::from("a.rs"),
            DebugLevel::None,
        );
        let func = HirFunction {
            name: "test".to_string(),
            params: smallvec![],
            ret_type: Type::None,
            body: vec![],
            properties: FunctionProperties::default(),
            annotations: depyler_annotations::TranspilationAnnotations::default(),
            docstring: None,
        };
        gen.add_function_mapping(&func, 1);
        assert!(gen.source_map().function_map.is_empty());
    }

    #[test]
    fn test_add_function_mapping_full_level() {
        let mut gen = DebugInfoGenerator::new(
            PathBuf::from("a.py"),
            PathBuf::from("a.rs"),
            DebugLevel::Full,
        );
        gen.new_line(); // line 2
        gen.new_line(); // line 3
        let func = HirFunction {
            name: "my_func".to_string(),
            params: smallvec![],
            ret_type: Type::Int,
            body: vec![],
            properties: FunctionProperties::default(),
            annotations: depyler_annotations::TranspilationAnnotations::default(),
            docstring: None,
        };
        gen.add_function_mapping(&func, 1);
        assert!(gen.source_map().function_map.contains_key("my_func"));
        let fm = gen.source_map().function_map.get("my_func").unwrap();
        assert_eq!(fm.rust_start_line, 1);
        assert_eq!(fm.rust_end_line, 3);
    }

    // === DebugRuntime tests ===

    #[test]
    fn test_debug_runtime_breakpoint() {
        let bp = DebugRuntime::breakpoint();
        assert_eq!(bp, "depyler_breakpoint!()");
    }

    #[test]
    fn test_debug_runtime_debug_assert() {
        let assertion = DebugRuntime::debug_assert("x > 0", "x must be positive");
        assert!(assertion.contains("debug_assert!"));
        assert!(assertion.contains("x > 0"));
        assert!(assertion.contains("x must be positive"));
    }

    #[test]
    fn test_debug_runtime_trace_point() {
        let trace = DebugRuntime::trace_point("entering loop");
        assert!(trace.contains("depyler_trace!"));
        assert!(trace.contains("entering loop"));
    }

    // === DebuggerType tests ===

    #[test]
    fn test_debugger_type_gdb() {
        let dt = DebuggerType::Gdb;
        let debug = format!("{:?}", dt);
        assert!(debug.contains("Gdb"));
    }

    #[test]
    fn test_debugger_type_lldb() {
        let dt = DebuggerType::Lldb;
        let cloned = dt;
        assert!(matches!(cloned, DebuggerType::Lldb));
    }

    #[test]
    fn test_debugger_type_rust_gdb() {
        let dt = DebuggerType::RustGdb;
        let debug = format!("{:?}", dt);
        assert!(debug.contains("RustGdb"));
    }

    // === DebuggerIntegration tests ===

    #[test]
    fn test_debugger_integration_new() {
        let di = DebuggerIntegration::new(DebuggerType::Gdb);
        assert!(matches!(di.debugger_type, DebuggerType::Gdb));
    }

    #[test]
    fn test_generate_rust_gdb_script() {
        let source_map = SourceMap {
            source_file: PathBuf::from("test.py"),
            target_file: PathBuf::from("test.rs"),
            mappings: vec![],
            function_map: HashMap::new(),
        };
        let di = DebuggerIntegration::new(DebuggerType::RustGdb);
        let script = di.generate_init_script(&source_map);
        assert!(script.contains("GDB initialization"));
        assert!(script.contains("Rust pretty printers"));
        assert!(script.contains("python"));
    }

    // === DebugConfig tests ===

    #[test]
    fn test_debug_config_default() {
        let config = DebugConfig::default();
        assert_eq!(config.debug_level, DebugLevel::Basic);
        assert!(config.generate_source_map);
        assert!(config.preserve_symbols);
        assert!(!config.debug_prints);
        assert!(!config.breakpoints);
    }

    #[test]
    fn test_debug_config_clone() {
        let config = DebugConfig {
            debug_level: DebugLevel::Full,
            generate_source_map: false,
            preserve_symbols: false,
            debug_prints: true,
            breakpoints: true,
        };
        let cloned = config.clone();
        assert_eq!(cloned.debug_level, DebugLevel::Full);
        assert!(cloned.debug_prints);
    }

    #[test]
    fn test_debug_config_serialize() {
        let config = DebugConfig::default();
        let json = serde_json::to_string(&config).unwrap();
        assert!(json.contains("Basic"));
        let deserialized: DebugConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.debug_level, config.debug_level);
    }

    // === generate_debug_macros tests ===

    #[test]
    fn test_generate_debug_macros() {
        let macros = generate_debug_macros();
        assert!(macros.contains("depyler_breakpoint"));
        assert!(macros.contains("depyler_trace"));
        assert!(macros.contains("#[macro_export]"));
        assert!(macros.contains("debug_assertions"));
    }

    // === Original tests ===

    #[test]
    fn test_source_mapping() {
        let mut generator = DebugInfoGenerator::new(
            PathBuf::from("test.py"),
            PathBuf::from("test.rs"),
            DebugLevel::Full,
        );

        generator.add_mapping(10, 0, Some("test_func".to_string()));
        generator.new_line();
        generator.add_mapping(11, 4, None);

        assert_eq!(generator.source_map().mappings.len(), 2);
        assert_eq!(generator.source_map().mappings[0].python_line, 10);
        assert_eq!(generator.source_map().mappings[0].rust_line, 1);
        assert_eq!(generator.source_map().mappings[1].rust_line, 2);
    }

    #[test]
    fn test_debug_print_generation() {
        let generator = DebugInfoGenerator::new(
            PathBuf::from("test.py"),
            PathBuf::from("test.rs"),
            DebugLevel::Full,
        );

        let int_debug = generator.generate_debug_print("x", &Type::Int);
        assert!(int_debug.contains("eprintln!"));
        assert!(int_debug.contains("x = {}"));

        let vec_debug = generator.generate_debug_print("items", &Type::List(Box::new(Type::Int)));
        assert!(vec_debug.contains("{:?}"));
    }

    #[test]
    fn test_debugger_scripts() {
        let source_map = SourceMap {
            source_file: PathBuf::from("test.py"),
            target_file: PathBuf::from("test.rs"),
            mappings: vec![],
            function_map: vec![(
                "test_func".to_string(),
                FunctionMapping {
                    python_name: "test_func".to_string(),
                    rust_name: "test_func".to_string(),
                    python_start_line: 1,
                    python_end_line: 5,
                    rust_start_line: 10,
                    rust_end_line: 20,
                },
            )]
            .into_iter()
            .collect(),
        };

        let gdb_integration = DebuggerIntegration::new(DebuggerType::Gdb);
        let gdb_script = gdb_integration.generate_init_script(&source_map);
        assert!(gdb_script.contains("break test_func"));

        let lldb_integration = DebuggerIntegration::new(DebuggerType::Lldb);
        let lldb_script = lldb_integration.generate_init_script(&source_map);
        assert!(lldb_script.contains("breakpoint set --name test_func"));
    }
}