ruchy 4.1.1

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
//! Binary compilation support for Ruchy
//!
//! This module provides functionality to compile Ruchy code to standalone binaries
//! via Rust compilation toolchain (rustc).
use crate::utils::common_patterns::ResultContextExt;
use crate::{Parser, Transpiler};
use anyhow::{bail, Context, Result};
use proc_macro2::TokenStream;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::TempDir;
/// Binary compilation options
#[derive(Debug, Clone)]
pub struct CompileOptions {
    /// Output binary path
    pub output: PathBuf,
    /// Optimization level (0-3, or 's' for size)
    pub opt_level: String,
    /// Strip debug symbols
    pub strip: bool,
    /// Static linking
    pub static_link: bool,
    /// Target triple (e.g., x86_64-unknown-linux-gnu)
    pub target: Option<String>,
    /// Additional rustc flags
    pub rustc_flags: Vec<String>,
    /// Model files to embed in the binary (issue #169)
    /// Each path will be embedded via `include_bytes!` for zero-copy loading
    pub embed_models: Vec<PathBuf>,
}
impl Default for CompileOptions {
    fn default() -> Self {
        Self {
            output: PathBuf::from("a.out"),
            opt_level: "2".to_string(),
            strip: false,
            static_link: false,
            target: None,
            rustc_flags: Vec::new(),
            embed_models: Vec::new(),
        }
    }
}
/// Compile a Ruchy source file to a standalone binary
///
/// # Examples
///
/// ```no_run
/// use ruchy::backend::{compile_to_binary, CompileOptions};
/// use std::path::PathBuf;
///
/// let options = CompileOptions {
///     output: PathBuf::from("my_program"),
///     opt_level: "2".to_string(),
///     strip: false,
///     static_link: false,
///     target: None,
///     rustc_flags: Vec::new(),
///     embed_models: Vec::new(),
/// };
///
/// let result = compile_to_binary(&PathBuf::from("program.ruchy"), &options);
/// ```
///
/// # Errors
///
/// Returns an error if:
/// - The source file cannot be read
/// - The source code fails to parse
/// - The transpilation fails
/// - The rustc compilation fails
pub fn compile_to_binary(source_path: &Path, options: &CompileOptions) -> Result<PathBuf> {
    // Read source file
    let source = fs::read_to_string(source_path).file_context("read", source_path)?;
    compile_source_to_binary_with_context(&source, options, Some(source_path))
}
/// Compile Ruchy source code to a standalone binary
///
/// # Examples
///
/// ```no_run
/// use ruchy::backend::{compile_source_to_binary, CompileOptions};
/// use std::path::PathBuf;
///
/// let source = r#"
///     fun main() {
///         println("Hello, World!");
///     }
/// "#;
///
/// let options = CompileOptions::default();
/// let result = compile_source_to_binary(source, &options);
/// ```
///
/// # Errors
///
/// Returns an error if:
/// - The source code fails to parse
/// - The transpilation fails
/// - The working directory cannot be created
/// - The rustc compilation fails
pub fn compile_source_to_binary(source: &str, options: &CompileOptions) -> Result<PathBuf> {
    compile_source_to_binary_with_context(source, options, None)
}

/// Compile Ruchy source code to a standalone binary with file context for module resolution
///
/// # Arguments
/// * `source` - The Ruchy source code
/// * `options` - Compilation options
/// * `source_path` - Optional source file path for module resolution
///
/// # Errors
/// Returns an error if parsing, transpilation, or rustc compilation fails
///
/// # Examples
/// ```no_run
/// use ruchy::backend::{compile_source_to_binary_with_context, CompileOptions};
/// use std::path::Path;
///
/// let source = r#"println!("Hello")"#;
/// let options = CompileOptions::default();
/// let result = compile_source_to_binary_with_context(source, &options, Some(Path::new("app.ruchy")));
/// ```
pub fn compile_source_to_binary_with_context(
    source: &str,
    options: &CompileOptions,
    source_path: Option<&Path>,
) -> Result<PathBuf> {
    // Parse to check for DataFrame, JSON, and HTTP usage
    let mut parser = Parser::new(source);
    let ast = parser.parse().parse_context("Ruchy source")?;
    let needs_polars = uses_dataframes(&ast);
    let needs_json = uses_json(&ast);
    let needs_http = uses_http(&ast);

    // ISSUE-106: Resolve module declarations (mod name;) ONLY if AST contains them
    // This prevents double-resolution with transpiler's existing import handling (ISSUE-103)
    let resolved_ast = if let Some(path) = source_path {
        if contains_module_declaration(&ast) {
            use crate::backend::module_resolver::ModuleResolver;
            let mut resolver = ModuleResolver::new();
            // Add the source file's directory to the module search path
            if let Some(parent_dir) = path.parent() {
                resolver.add_search_path(parent_dir);

                // MODULE-RESOLUTION-001: Also search in standard project layout directories
                // If compiling project/bin/main.ruchy, also search project/src/, project/lib/
                if let Some(project_root) = parent_dir.parent() {
                    resolver.add_search_path(project_root.join("src"));
                    resolver.add_search_path(project_root.join("lib"));
                    resolver.add_search_path(project_root.join("modules"));
                }
            }
            resolver
                .resolve_imports(ast)
                .compile_context("resolve module declarations")?
        } else {
            ast
        }
    } else {
        ast
    };

    // Transpile with file context for module resolution (ISSUE-103)
    let mut transpiler = Transpiler::new();
    let rust_code = transpiler
        .transpile_to_program_with_context(&resolved_ast, source_path)
        .compile_context("transpile to Rust")?;

    if needs_polars || needs_json || needs_http {
        // Use cargo build with Cargo.toml (for external crate access)
        compile_with_cargo(&rust_code, options)
    } else {
        // Use direct rustc (faster for simple programs)
        compile_with_rustc(&rust_code, options)
    }
}
/// Parse Ruchy source and transpile to Rust (complexity: 4)
fn parse_and_transpile(source: &str) -> Result<TokenStream> {
    let mut parser = Parser::new(source);
    let ast = parser.parse().parse_context("Ruchy source")?;
    let mut transpiler = Transpiler::new();
    let rust_code = transpiler
        .transpile_to_program(&ast)
        .compile_context("transpile to Rust")?;
    Ok(rust_code)
}

/// Check if AST contains `DataFrame` usage (complexity: 3)
///
/// # Examples
///
/// ```
/// use ruchy::frontend::parser::Parser;
/// use ruchy::backend::compiler::uses_dataframes;
///
/// let code = r#"fun main() { let df = df!["x" => [1, 2, 3]]; }"#;
/// let mut parser = Parser::new(code);
/// let ast = parser.parse().unwrap();
/// assert!(uses_dataframes(&ast));
/// ```
pub fn uses_dataframes(ast: &crate::frontend::ast::Expr) -> bool {
    use crate::frontend::ast::ExprKind;

    match &ast.kind {
        // Direct DataFrame usage
        ExprKind::DataFrame { .. } | ExprKind::DataFrameOperation { .. } => true,

        // Recursive checks
        ExprKind::Binary { left, right, .. } => check_binary_for_dataframes(left, right),
        ExprKind::Let { value, body, .. } => check_binary_for_dataframes(value, body),
        ExprKind::MethodCall { receiver, args, .. } => check_method_for_dataframes(receiver, args),
        ExprKind::Call { func, args } => check_call_for_dataframes(func, args),

        // Check inside function bodies
        ExprKind::Function { body, .. } => uses_dataframes(body),
        ExprKind::Block(exprs) => exprs.iter().any(uses_dataframes),

        // Default
        _ => false,
    }
}

/// Check binary expressions for `DataFrames` (complexity: 1)
fn check_binary_for_dataframes(
    left: &crate::frontend::ast::Expr,
    right: &crate::frontend::ast::Expr,
) -> bool {
    uses_dataframes(left) || uses_dataframes(right)
}

/// Check method calls for `DataFrames` (complexity: 1)
fn check_method_for_dataframes(
    receiver: &crate::frontend::ast::Expr,
    args: &[crate::frontend::ast::Expr],
) -> bool {
    uses_dataframes(receiver) || args.iter().any(uses_dataframes)
}

/// Check function calls for `DataFrames` (complexity: 1)
fn check_call_for_dataframes(
    func: &crate::frontend::ast::Expr,
    args: &[crate::frontend::ast::Expr],
) -> bool {
    uses_dataframes(func) || args.iter().any(uses_dataframes)
}

/// Check if AST contains JSON function usage (complexity: 4)
///
/// Detects usage of JSON stdlib functions that require `serde_json` crate.
/// This triggers cargo compilation instead of direct rustc.
///
/// # Examples
///
/// ```
/// use ruchy::frontend::parser::Parser;
/// use ruchy::backend::compiler::uses_json;
///
/// let code = r#"fun main() { let obj = json_parse("{\"x\": 1}"); }"#;
/// let mut parser = Parser::new(code);
/// let ast = parser.parse().unwrap();
/// assert!(uses_json(&ast));
/// ```
pub fn uses_json(ast: &crate::frontend::ast::Expr) -> bool {
    use crate::frontend::ast::ExprKind;

    match &ast.kind {
        ExprKind::Call { func, args } => check_call_for_json(func, args),
        ExprKind::Binary { left, right, .. } => check_binary_for_json(left, right),
        ExprKind::Let { value, body, .. } => check_binary_for_json(value, body),
        ExprKind::MethodCall { receiver, args, .. } => check_method_for_json(receiver, args),
        ExprKind::Function { body, .. } => uses_json(body),
        ExprKind::Block(exprs) => exprs.iter().any(uses_json),
        _ => false,
    }
}

/// Check function calls for JSON functions (complexity: 3)
fn check_call_for_json(
    func: &crate::frontend::ast::Expr,
    args: &[crate::frontend::ast::Expr],
) -> bool {
    use crate::frontend::ast::ExprKind;

    // Check if calling a JSON function
    if let ExprKind::Identifier(name) = &func.kind {
        if is_json_function(name) {
            return true;
        }
    }
    // Recursively check function and arguments
    uses_json(func) || args.iter().any(uses_json)
}

/// Check binary expressions for JSON usage (complexity: 1)
fn check_binary_for_json(
    left: &crate::frontend::ast::Expr,
    right: &crate::frontend::ast::Expr,
) -> bool {
    uses_json(left) || uses_json(right)
}

/// Check method calls for JSON usage (complexity: 1)
fn check_method_for_json(
    receiver: &crate::frontend::ast::Expr,
    args: &[crate::frontend::ast::Expr],
) -> bool {
    uses_json(receiver) || args.iter().any(uses_json)
}

/// Check if function name is a JSON stdlib function (complexity: 1)
fn is_json_function(name: &str) -> bool {
    matches!(
        name,
        "json_parse"
            | "json_stringify"
            | "json_pretty"
            | "json_read"
            | "json_write"
            | "json_validate"
            | "json_type"
            | "json_merge"
            | "json_get"
            | "json_set"
    )
}

// ==============================================================================
// HTTP Detection Functions (STDLIB-PHASE-5)
// ==============================================================================

/// Check AST for HTTP stdlib usage (complexity: 2)
pub fn uses_http(ast: &crate::frontend::ast::Expr) -> bool {
    use crate::frontend::ast::ExprKind;

    match &ast.kind {
        ExprKind::Call { func, args } => check_call_for_http(func, args),
        ExprKind::Binary { left, right, .. } => check_binary_for_http(left, right),
        ExprKind::Let { value, body, .. } => check_binary_for_http(value, body),
        ExprKind::MethodCall { receiver, args, .. } => check_method_for_http(receiver, args),
        ExprKind::Function { body, .. } => uses_http(body),
        ExprKind::Block(exprs) => exprs.iter().any(uses_http),
        _ => false,
    }
}

/// Check function calls for HTTP functions (complexity: 3)
fn check_call_for_http(
    func: &crate::frontend::ast::Expr,
    args: &[crate::frontend::ast::Expr],
) -> bool {
    use crate::frontend::ast::ExprKind;

    // Check if calling an HTTP function
    if let ExprKind::Identifier(name) = &func.kind {
        if is_http_function(name) {
            return true;
        }
    }
    // Recursively check function and arguments
    uses_http(func) || args.iter().any(uses_http)
}

/// Check binary expressions for HTTP usage (complexity: 1)
fn check_binary_for_http(
    left: &crate::frontend::ast::Expr,
    right: &crate::frontend::ast::Expr,
) -> bool {
    uses_http(left) || uses_http(right)
}

/// Check method calls for HTTP usage (complexity: 1)
fn check_method_for_http(
    receiver: &crate::frontend::ast::Expr,
    args: &[crate::frontend::ast::Expr],
) -> bool {
    uses_http(receiver) || args.iter().any(uses_http)
}

/// Check if function name is an HTTP stdlib function (complexity: 1)
fn is_http_function(name: &str) -> bool {
    matches!(name, "http_get" | "http_post" | "http_put" | "http_delete")
}

/// Check if AST contains any external module declarations (mod name;) or file imports (use name;)
///
/// ISSUE-106: Used to determine if module resolution is needed in compiler.
/// MODULE-RESOLUTION-001: Also check for Import statements that might be file imports.
/// This prevents double-resolution with transpiler's import handling (ISSUE-103).
fn contains_module_declaration(ast: &crate::frontend::ast::Expr) -> bool {
    use crate::frontend::ast::ExprKind;

    fn check_expr(expr: &crate::frontend::ast::Expr) -> bool {
        match &expr.kind {
            // TRANSPILER-MODULE-001 FIX: Only match actual `mod name;` declarations
            // DO NOT match regular `use module` imports - those are handled by transpiler's
            // resolve_imports_with_context to avoid double-resolution causing duplicate braces
            ExprKind::ModuleDeclaration { .. } => true,
            ExprKind::Block(exprs) => exprs.iter().any(check_expr),
            ExprKind::Function { body, .. } => check_expr(body),
            ExprKind::Let { value, body, .. } => check_expr(value) || check_expr(body),
            _ => false,
        }
    }

    check_expr(ast)
}

/// Generate Cargo.toml with polars dependency (complexity: 2)
fn generate_cargo_toml(binary_name: &str) -> String {
    format!(
        r#"[package]
name = "{binary_name}"
version = "0.1.0"
edition = "2021"

[dependencies]
polars = {{ version = "0.35", features = ["lazy"] }}
serde = {{ version = "1.0", features = ["derive"] }}
serde_json = "1.0"
reqwest = {{ version = "0.12", features = ["blocking"] }}
"#
    )
}

/// Compile with cargo (for `DataFrame` support) (complexity: 7)
fn compile_with_cargo(rust_code: &TokenStream, options: &CompileOptions) -> Result<PathBuf> {
    // Create temporary directory for cargo project
    let temp_dir = TempDir::new().compile_context("create temporary directory")?;
    let project_dir = temp_dir.path();

    // Create src directory
    let src_dir = project_dir.join("src");
    fs::create_dir(&src_dir).context("Failed to create src directory")?;

    // Generate code with embedded models if specified (issue #169)
    let rust_code_str = rust_code.to_string();
    let final_code = if options.embed_models.is_empty() {
        rust_code_str
    } else {
        generate_model_embedding_code(&rust_code_str, &options.embed_models, &src_dir)?
    };

    // Write main.rs
    let main_file = src_dir.join("main.rs");
    fs::write(&main_file, &final_code)?;

    // Write Cargo.toml
    let cargo_toml = project_dir.join("Cargo.toml");
    let cargo_content = generate_cargo_toml("ruchy_binary");
    fs::write(&cargo_toml, cargo_content)?;

    // Run cargo build --release
    let mut cmd = Command::new("cargo");
    cmd.arg("build").arg("--release").current_dir(project_dir);

    let output = cmd.output().context("Failed to execute cargo build")?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        bail!("Cargo build failed:\n{stderr}");
    }

    // Copy binary to output location
    let compiled_binary = project_dir.join("target/release/ruchy_binary");
    if !compiled_binary.exists() {
        bail!("Expected binary not found after cargo build");
    }

    fs::copy(&compiled_binary, &options.output)
        .context("Failed to copy compiled binary to output location")?;

    Ok(options.output.clone())
}

/// Compile with rustc directly (for simple programs) (complexity: 5)
fn compile_with_rustc(rust_code: &TokenStream, options: &CompileOptions) -> Result<PathBuf> {
    let (_temp_dir, rust_file) = prepare_rust_file(rust_code, options)?;
    let cmd = build_rustc_command(&rust_file, options);
    execute_compilation(cmd)?;
    verify_output_exists(&options.output)?;
    Ok(options.output.clone())
}

/// Prepare temporary Rust file for compilation (complexity: 6)
/// Handles model embedding via `include_bytes!` (issue #169)
fn prepare_rust_file(
    rust_code: &TokenStream,
    options: &CompileOptions,
) -> Result<(TempDir, PathBuf)> {
    let temp_dir = TempDir::new().compile_context("create temporary directory")?;
    let rust_file = temp_dir.path().join("main.rs");
    let rust_code_str = rust_code.to_string();

    // Generate model embedding code if models specified (issue #169)
    let final_code = if options.embed_models.is_empty() {
        rust_code_str
    } else {
        generate_model_embedding_code(&rust_code_str, &options.embed_models, temp_dir.path())?
    };

    // Debug: Also write to /tmp/debug_rust_output.rs for inspection
    fs::write("/tmp/debug_rust_output.rs", &final_code)
        .context("Failed to write debug Rust code")?;
    fs::write(&rust_file, &final_code).context("Failed to write Rust code to temporary file")?;
    Ok((temp_dir, rust_file))
}

/// Generate model embedding code with `include_bytes!` (issue #169)
/// Copies model files to temp directory and generates static byte arrays
fn generate_model_embedding_code(
    rust_code: &str,
    embed_models: &[PathBuf],
    temp_dir: &Path,
) -> Result<String> {
    let mut model_statics = String::new();
    let mut model_loader_fn = String::from(
        "\n/// Get embedded model bytes by filename\n\
         #[allow(dead_code)]\n\
         pub fn get_embedded_model(name: &str) -> Option<&'static [u8]> {\n\
         match name {\n",
    );

    for (i, model_path) in embed_models.iter().enumerate() {
        // Validate model file exists
        if !model_path.exists() {
            bail!("Embedded model file not found: {}", model_path.display());
        }

        // Copy model to temp directory for include_bytes! access
        let model_filename = model_path
            .file_name()
            .ok_or_else(|| anyhow::anyhow!("Invalid model path: {}", model_path.display()))?
            .to_string_lossy();
        let temp_model_path = temp_dir.join(&*model_filename);
        fs::copy(model_path, &temp_model_path)
            .with_context(|| format!("Failed to copy model: {}", model_path.display()))?;

        // Generate static variable name from filename (sanitized)
        let var_name = sanitize_model_name(&model_filename);

        // Generate include_bytes! for this model
        model_statics.push_str(&format!(
            "/// Embedded model: {model_filename}\n\
             static MODEL_{var_name}: &[u8] = include_bytes!(\"{model_filename}\");\n\n",
        ));

        // Add to loader function
        model_loader_fn.push_str(&format!(
            "    \"{model_filename}\" => Some(MODEL_{var_name}),\n",
        ));

        // Also add index-based access
        model_loader_fn.push_str(&format!("    \"model_{i}\" => Some(MODEL_{var_name}),\n"));
    }

    model_loader_fn.push_str("    _ => None,\n}\n}\n");

    // Prepend model statics and loader to the Rust code
    Ok(format!(
        "// === Embedded Models (ruchy compile --embed-model) ===\n\
         {model_statics}\n\
         {model_loader_fn}\n\
         // === End Embedded Models ===\n\n\
         {rust_code}",
    ))
}

/// Sanitize model filename to valid Rust identifier (uppercase)
fn sanitize_model_name(filename: &str) -> String {
    filename
        .chars()
        .map(|c| {
            if c.is_alphanumeric() {
                c.to_ascii_uppercase()
            } else {
                '_'
            }
        })
        .collect()
}
/// Build rustc command with options (complexity: 7)
fn build_rustc_command(rust_file: &Path, options: &CompileOptions) -> Command {
    let mut cmd = Command::new("rustc");
    cmd.arg(rust_file).arg("-o").arg(&options.output);
    // Set Rust edition to 2021 for async support
    cmd.arg("--edition").arg("2021");
    // Add optimization level
    cmd.arg("-C")
        .arg(format!("opt-level={}", options.opt_level));
    // Add optional flags
    apply_optional_flags(&mut cmd, options);
    cmd
}
/// Apply optional compilation flags (complexity: 5)
fn apply_optional_flags(cmd: &mut Command, options: &CompileOptions) {
    if options.strip {
        cmd.arg("-C").arg("strip=symbols");
    }
    if options.static_link {
        cmd.arg("-C").arg("target-feature=+crt-static");
    }
    if let Some(target) = &options.target {
        cmd.arg("--target").arg(target);
    }
    for flag in &options.rustc_flags {
        cmd.arg(flag);
    }
}
/// Execute compilation command (complexity: 3)
fn execute_compilation(mut cmd: Command) -> Result<()> {
    let output = cmd.output().context("Failed to execute rustc")?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        bail!("Compilation failed:\n{stderr}");
    }
    Ok(())
}
/// Verify output file exists (complexity: 2)
fn verify_output_exists(output_path: &Path) -> Result<()> {
    if !output_path.exists() {
        bail!(
            "Expected output file not created: {}",
            output_path.display()
        );
    }
    Ok(())
}
/// Check if rustc is available
///
/// Uses multiple strategies to find rustc:
/// 1. Try "rustc" in PATH (standard approach)
/// 2. Try common cargo installation paths as fallback
///
/// This robust approach handles edge cases in test environments where PATH
/// may not be fully propagated to subprocesses (complexity: 5)
///
/// # Examples
///
/// ```
/// use ruchy::backend::compiler::check_rustc_available;
///
/// if check_rustc_available().is_ok() {
///     println!("rustc is available");
/// }
/// ```
///
/// # Errors
///
/// Returns an error if rustc is not installed or cannot be executed
pub fn check_rustc_available() -> Result<()> {
    // Strategy 1: Try rustc in PATH (standard approach)
    if try_rustc_command("rustc").is_ok() {
        return Ok(());
    }

    // Strategy 2: Try common cargo installation paths (robustness for test environments)
    let fallback_paths = [
        format!(
            "{}/.cargo/bin/rustc",
            std::env::var("HOME").unwrap_or_default()
        ),
        "/usr/local/bin/rustc".to_string(),
        "/usr/bin/rustc".to_string(),
    ];

    for path in &fallback_paths {
        if try_rustc_command(path).is_ok() {
            return Ok(());
        }
    }

    bail!("rustc is not available. Please install Rust toolchain.")
}

/// Try executing rustc command with given path (complexity: 4)
fn try_rustc_command(rustc_path: &str) -> Result<()> {
    let output = Command::new(rustc_path).arg("--version").output();

    match output {
        Ok(output) if output.status.success() => Ok(()),
        Ok(output) => {
            // Command executed but returned non-zero exit code
            bail!(
                "rustc at '{}' failed with exit code {:?}",
                rustc_path,
                output.status.code()
            )
        }
        Err(e) => {
            // Command could not be executed (binary not found, permission denied, etc.)
            bail!("Could not execute rustc at '{rustc_path}': {e}")
        }
    }
}
/// Get rustc version information
///
/// # Examples
///
/// ```
/// use ruchy::backend::compiler::get_rustc_version;
///
/// if let Ok(version) = get_rustc_version() {
///     println!("rustc version: {}", version);
/// }
/// ```
///
/// # Errors
///
/// Returns an error if rustc is not available or version cannot be retrieved
pub fn get_rustc_version() -> Result<String> {
    let output = Command::new("rustc")
        .arg("--version")
        .output()
        .context("Failed to execute rustc")?;
    if !output.status.success() {
        bail!("Failed to get rustc version");
    }
    Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_check_rustc_available() {
        // This should pass in any environment with Rust installed
        assert!(check_rustc_available().is_ok());
    }
    #[test]
    fn test_get_rustc_version() {
        let version = get_rustc_version().unwrap_or_else(|_| "unknown".to_string());
        assert!(version.contains("rustc"));
    }
    #[test]
    fn test_compile_simple_program() {
        let source = r#"
            fun main() {
                println("Hello from compiled Ruchy!");
            }
        "#;
        let options = CompileOptions {
            output: PathBuf::from("/tmp/test_ruchy_binary"),
            ..Default::default()
        };
        // This might fail if the transpiler doesn't support the syntax yet
        // but the infrastructure should work
        let _ = compile_source_to_binary(source, &options);
    }

    #[test]
    fn test_compile_options_default() {
        let options = CompileOptions::default();
        assert_eq!(options.output, PathBuf::from("a.out"));
        assert_eq!(options.opt_level, "2");
        assert!(!options.strip);
        assert!(!options.static_link);
        assert!(options.target.is_none());
        assert!(options.rustc_flags.is_empty());
        assert!(options.embed_models.is_empty());
    }

    #[test]
    fn test_compile_options_custom() {
        let options = CompileOptions {
            output: PathBuf::from("my_binary"),
            opt_level: "3".to_string(),
            strip: true,
            static_link: true,
            target: Some("x86_64-unknown-linux-musl".to_string()),
            rustc_flags: vec!["-C".to_string(), "lto=fat".to_string()],
            embed_models: Vec::new(),
        };

        assert_eq!(options.output, PathBuf::from("my_binary"));
        assert_eq!(options.opt_level, "3");
        assert!(options.strip);
        assert!(options.static_link);
        assert_eq!(
            options.target,
            Some("x86_64-unknown-linux-musl".to_string())
        );
        assert_eq!(options.rustc_flags.len(), 2);
    }

    #[test]
    fn test_sanitize_model_name() {
        assert_eq!(
            sanitize_model_name("model.safetensors"),
            "MODEL_SAFETENSORS"
        );
        assert_eq!(sanitize_model_name("my-model_v2.gguf"), "MY_MODEL_V2_GGUF");
        assert_eq!(sanitize_model_name("123_numbers.bin"), "123_NUMBERS_BIN");
    }

    #[test]
    fn test_generate_model_embedding_code() {
        use tempfile::TempDir;

        // Create a temp directory with a dummy model file
        let temp_dir = TempDir::new().expect("create temp dir");
        let model_path = temp_dir.path().join("test_model.bin");
        fs::write(&model_path, b"fake model data").expect("write model");

        let rust_code = "fn main() { println!(\"hello\"); }";
        let result = generate_model_embedding_code(rust_code, &[model_path], temp_dir.path());

        assert!(result.is_ok());
        let code = result.expect("model embedding code");

        // Check that include_bytes! was generated
        assert!(code.contains("include_bytes!"));
        assert!(code.contains("MODEL_TEST_MODEL_BIN"));
        assert!(code.contains("get_embedded_model"));
        assert!(code.contains("test_model.bin"));
        // Original code should be at the end
        assert!(code.contains("fn main()"));
    }

    #[test]
    fn test_generate_model_embedding_multiple_models() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().expect("create temp dir");
        let model1 = temp_dir.path().join("model_a.bin");
        let model2 = temp_dir.path().join("model_b.bin");
        fs::write(&model1, b"model a data").expect("write model a");
        fs::write(&model2, b"model b data").expect("write model b");

        let rust_code = "fn main() {}";
        let result = generate_model_embedding_code(rust_code, &[model1, model2], temp_dir.path());

        assert!(result.is_ok());
        let code = result.expect("model embedding code");

        // Check both models are embedded
        assert!(code.contains("MODEL_MODEL_A_BIN"));
        assert!(code.contains("MODEL_MODEL_B_BIN"));
        assert!(code.contains("\"model_a.bin\""));
        assert!(code.contains("\"model_b.bin\""));
        // Index-based access
        assert!(code.contains("\"model_0\""));
        assert!(code.contains("\"model_1\""));
    }

    #[test]
    fn test_generate_model_embedding_missing_file() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().expect("create temp dir");
        let missing_model = temp_dir.path().join("nonexistent.bin");

        let result =
            generate_model_embedding_code("fn main() {}", &[missing_model], temp_dir.path());

        assert!(result.is_err());
        let err = result.expect_err("should fail");
        assert!(err.to_string().contains("not found"));
    }

    #[test]
    fn test_build_rustc_command() {
        let rust_file = Path::new("/tmp/test.rs");
        let options = CompileOptions {
            opt_level: "2".to_string(),
            strip: true,
            ..Default::default()
        };

        let _cmd = build_rustc_command(rust_file, &options);

        // Can't easily test Command internals, but verify it doesn't panic
        // The function returns a Command which we can't easily inspect
        // Test passes without panic; // Just verify no panic
    }

    #[test]
    fn test_apply_optional_flags() {
        let mut cmd = Command::new("rustc");
        let options = CompileOptions {
            strip: true,
            static_link: true,
            target: Some("x86_64-unknown-linux-musl".to_string()),
            rustc_flags: vec!["-C".to_string(), "lto=fat".to_string()],
            ..Default::default()
        };

        apply_optional_flags(&mut cmd, &options);
        // Can't easily inspect Command internals, but verify it doesn't panic
        // Test passes without panic;
    }

    #[test]
    fn test_prepare_rust_file() {
        let rust_code = TokenStream::new();
        let options = CompileOptions::default();
        let result = prepare_rust_file(&rust_code, &options);
        assert!(result.is_ok());

        if let Ok((_temp_dir, rust_file)) = result {
            assert!(rust_file.exists());
            assert!(rust_file.extension() == Some(std::ffi::OsStr::new("rs")));
        }
    }

    #[test]
    fn test_parse_and_transpile() {
        // Test with valid Ruchy code
        let source = "fun main() { println(\"Hello\"); }";
        let result = parse_and_transpile(source);
        // This might fail if parser doesn't support this syntax yet
        let _ = result; // Just check it doesn't panic
    }

    #[test]
    fn test_execute_compilation() {
        // Test with a command that will fail (non-existent file)
        let mut cmd = Command::new("rustc");
        cmd.arg("/non/existent/file.rs");

        let result = execute_compilation(cmd);
        assert!(result.is_err());
    }

    #[test]
    fn test_verify_output_exists() {
        // Test with non-existent file
        let result = verify_output_exists(Path::new("/non/existent/binary"));
        assert!(result.is_err());

        // Test with existing file (use temp file)
        let temp_file = tempfile::NamedTempFile::new().expect("operation should succeed in test");
        let result = verify_output_exists(temp_file.path());
        assert!(result.is_ok());
    }

    #[test]
    fn test_compile_invalid_source() {
        let source = "this is not valid Ruchy code!@#$%";
        let options = CompileOptions::default();

        let result = compile_source_to_binary(source, &options);
        assert!(result.is_err());
    }

    #[test]
    fn test_compile_empty_source() {
        let source = "";
        let options = CompileOptions::default();

        let result = compile_source_to_binary(source, &options);
        // Empty source might be valid or not depending on parser
        let _ = result; // Just check it doesn't panic
    }

    #[test]
    fn test_compile_whitespace_only() {
        let source = "   \n\t\n   ";
        let options = CompileOptions::default();

        let result = compile_source_to_binary(source, &options);
        // Whitespace might be valid or not depending on parser
        let _ = result; // Just check it doesn't panic
    }

    // Test 14: CompileOptions builder pattern functionality
    #[test]
    fn test_compile_options_builder_pattern() {
        let mut options = CompileOptions::default();
        options.output = PathBuf::from("custom_binary");
        options.opt_level = "3".to_string();
        options.strip = true;
        options.rustc_flags.push("--verbose".to_string());

        assert_eq!(options.output, PathBuf::from("custom_binary"));
        assert_eq!(options.opt_level, "3");
        assert!(options.strip);
        assert_eq!(options.rustc_flags.len(), 1);
        assert_eq!(options.rustc_flags[0], "--verbose");
    }

    // Test 15: All optimization level variations
    #[test]
    fn test_all_optimization_levels() {
        let valid_levels = vec!["0", "1", "2", "3", "s", "z"];

        for level in valid_levels {
            let options = CompileOptions {
                opt_level: level.to_string(),
                ..Default::default()
            };
            assert_eq!(options.opt_level, level);

            // Test command building doesn't panic with any opt level
            let rust_file = Path::new("/tmp/test.rs");
            let cmd = build_rustc_command(rust_file, &options);
            // Verify command was created successfully
            assert_eq!(cmd.get_program(), "rustc");
        }
    }

    // Test 16: Target triple validation
    #[test]
    fn test_target_triple_combinations() {
        let targets = vec![
            "x86_64-unknown-linux-gnu",
            "x86_64-unknown-linux-musl",
            "x86_64-pc-windows-msvc",
            "x86_64-apple-darwin",
            "aarch64-unknown-linux-gnu",
            "wasm32-unknown-unknown",
        ];

        for target in targets {
            let options = CompileOptions {
                target: Some(target.to_string()),
                ..Default::default()
            };

            assert_eq!(options.target, Some(target.to_string()));

            // Test command building with target
            let rust_file = Path::new("/tmp/test.rs");
            let cmd = build_rustc_command(rust_file, &options);
            assert_eq!(cmd.get_program(), "rustc");
        }
    }

    // Test 17: Multiple rustc flags handling
    #[test]
    fn test_multiple_rustc_flags() {
        let flags = vec![
            "-C".to_string(),
            "lto=fat".to_string(),
            "--verbose".to_string(),
            "-Z".to_string(),
            "print-type-sizes".to_string(),
        ];

        let options = CompileOptions {
            rustc_flags: flags.clone(),
            ..Default::default()
        };

        assert_eq!(options.rustc_flags.len(), 5);
        assert_eq!(options.rustc_flags, flags);

        // Test flag application
        let mut cmd = Command::new("rustc");
        apply_optional_flags(&mut cmd, &options);
        // Function shouldn't panic with multiple flags
        // Test passes without panic;
    }

    // Test 18: Strip and static link combinations
    #[test]
    fn test_strip_and_static_combinations() {
        let combinations = vec![(false, false), (true, false), (false, true), (true, true)];

        for (strip, static_link) in combinations {
            let options = CompileOptions {
                strip,
                static_link,
                ..Default::default()
            };

            assert_eq!(options.strip, strip);
            assert_eq!(options.static_link, static_link);

            let mut cmd = Command::new("rustc");
            apply_optional_flags(&mut cmd, &options);
            // Should handle all combinations without panic
            // Test passes without panic;
        }
    }

    // Test 19: Path handling with different file extensions
    #[test]
    fn test_path_handling_extensions() {
        let paths = vec![
            PathBuf::from("binary"),
            PathBuf::from("program.exe"),
            PathBuf::from("/tmp/output"),
            PathBuf::from("./relative/path"),
            PathBuf::from("../parent/binary"),
        ];

        for path in paths {
            let options = CompileOptions {
                output: path.clone(),
                ..Default::default()
            };

            assert_eq!(options.output, path);

            let rust_file = Path::new("/tmp/test.rs");
            let cmd = build_rustc_command(rust_file, &options);
            assert_eq!(cmd.get_program(), "rustc");
        }
    }

    // Test 20: Temporary file creation and cleanup
    #[test]
    fn test_temp_file_creation_cleanup() {
        let rust_code = TokenStream::new();
        let options = CompileOptions::default();

        // Test multiple temp file creations
        for _i in 0..5 {
            let result = prepare_rust_file(&rust_code, &options);
            assert!(result.is_ok());

            if let Ok((_temp_dir, rust_file)) = result {
                // Verify file was created
                assert!(rust_file.exists());
                assert!(
                    rust_file
                        .file_name()
                        .expect("operation should succeed in test")
                        == "main.rs"
                );

                // Verify parent directory exists
                assert!(rust_file
                    .parent()
                    .expect("operation should succeed in test")
                    .exists());

                // When _temp_dir goes out of scope, cleanup should happen automatically
                // This tests the RAII behavior of TempDir
            }
        }
    }

    // Test 21: Error message handling in execute_compilation
    #[test]
    fn test_execute_compilation_error_messages() {
        // Create command that will fail with specific error
        let mut cmd = Command::new("rustc");
        cmd.arg("--invalid-flag-that-does-not-exist");
        cmd.arg("/non/existent/file.rs");

        let result = execute_compilation(cmd);
        assert!(result.is_err());

        let error_msg = format!("{}", result.expect_err("operation should fail in test"));
        assert!(error_msg.contains("Compilation failed"));
    }

    // Test 22: Complex source code patterns
    #[test]
    fn test_complex_source_patterns() {
        let complex_sources = vec![
            // Unicode content
            "fun main() { println(\"Hello 世界! 🚀\"); }",
            // Long identifier names
            "fun this_is_a_very_long_function_name_that_might_cause_issues() { }",
            // Nested structures
            "fun main() { if (true) { if (false) { println(\"nested\"); } } }",
            // Comments and whitespace
            "// Comment\nfun main() {\n  // Another comment\n  println(\"test\");\n}",
            // String with escape sequences
            "fun main() { println(\"Line 1\\nLine 2\\tTabbed\"); }",
        ];

        for source in complex_sources {
            let options = CompileOptions {
                output: PathBuf::from("/tmp/complex_test"),
                ..Default::default()
            };

            // These may fail due to parser limitations, but shouldn't panic
            let result = compile_source_to_binary(source, &options);
            if let Ok(_) = result {
                // Success is good
            }
            // Expected failure is also fine
        }
    }

    // Test 23: Parse and transpile error handling
    #[test]
    fn test_parse_and_transpile_error_handling() {
        let invalid_sources = vec![
            "{{{[[[@#$%",            // Invalid syntax
            "fun(",                  // Incomplete function
            "\"unterminated string", // Unterminated string
        ];

        for source in invalid_sources {
            let result = compile_source_to_binary(source, &CompileOptions::default());
            // Should return error, not panic
            assert!(result.is_err(), "Expected error for source: '{source}'");
        }
    }

    // Test 24: File I/O edge cases
    #[test]
    fn test_file_io_edge_cases() {
        // Test with empty token stream
        let empty_tokens = TokenStream::new();
        let options = CompileOptions::default();
        let result = prepare_rust_file(&empty_tokens, &options);
        assert!(result.is_ok());

        if let Ok((_temp_dir, rust_file)) = result {
            // Verify empty file was created
            assert!(rust_file.exists());
            let contents =
                std::fs::read_to_string(&rust_file).expect("operation should succeed in test");
            assert!(contents.is_empty());
        }
    }

    // Test 25: Verify output with different scenarios
    #[test]
    fn test_verify_output_scenarios() {
        // Test with temporary file that exists
        let temp_file = tempfile::NamedTempFile::new().expect("operation should succeed in test");
        let result = verify_output_exists(temp_file.path());
        assert!(result.is_ok());

        // Test with directory instead of file
        let temp_dir = tempfile::TempDir::new().expect("operation should succeed in test");
        let result = verify_output_exists(temp_dir.path());
        assert!(result.is_ok()); // Directory exists, so should pass

        // Test with file in nested directory that doesn't exist
        let nested_path = Path::new("/non/existent/directory/binary");
        let result = verify_output_exists(nested_path);
        assert!(result.is_err());
    }

    // Test 26: Command building with extreme cases
    #[test]
    fn test_command_building_extreme_cases() {
        let options = CompileOptions {
            output: PathBuf::from("/very/long/path/with/many/segments/binary"),
            opt_level: "z".to_string(), // Size optimization
            strip: true,
            static_link: true,
            target: Some("wasm32-unknown-unknown".to_string()),
            rustc_flags: vec![
                "-C".to_string(),
                "lto=fat".to_string(),
                "-C".to_string(),
                "codegen-units=1".to_string(),
                "-C".to_string(),
                "panic=abort".to_string(),
            ],
            embed_models: Vec::new(),
        };

        let rust_file = Path::new("/tmp/test.rs");
        let cmd = build_rustc_command(rust_file, &options);

        // Verify command was built successfully
        assert_eq!(cmd.get_program(), "rustc");
    }

    // Test 27: CompileOptions clone and debug functionality
    #[test]
    fn test_compile_options_traits() {
        let options = CompileOptions {
            output: PathBuf::from("test_binary"),
            opt_level: "2".to_string(),
            strip: true,
            static_link: false,
            target: Some("x86_64-unknown-linux-gnu".to_string()),
            rustc_flags: vec!["--verbose".to_string()],
            embed_models: Vec::new(),
        };

        // Test Clone trait
        let cloned_options = options.clone();
        assert_eq!(options.output, cloned_options.output);
        assert_eq!(options.opt_level, cloned_options.opt_level);
        assert_eq!(options.strip, cloned_options.strip);
        assert_eq!(options.static_link, cloned_options.static_link);
        assert_eq!(options.target, cloned_options.target);
        assert_eq!(options.rustc_flags, cloned_options.rustc_flags);

        // Test Debug trait
        let debug_str = format!("{options:?}");
        assert!(debug_str.contains("CompileOptions"));
        assert!(debug_str.contains("test_binary"));
        assert!(debug_str.contains('2'));
    }

    // Test 28: Integration with file system operations
    #[test]
    fn test_filesystem_integration() {
        use std::fs;

        // Create a temporary directory for our tests
        let temp_dir = tempfile::TempDir::new().expect("operation should succeed in test");
        let source_file = temp_dir.path().join("test_program.ruchy");

        // Write a simple test program
        let source_content = "fun main() { println(\"Integration test\"); }";
        fs::write(&source_file, source_content).expect("operation should succeed in test");

        // Verify file was created and can be read
        assert!(source_file.exists());
        let read_content =
            fs::read_to_string(&source_file).expect("operation should succeed in test");
        assert_eq!(read_content, source_content);

        // Test compile_to_binary with actual file
        let output_path = temp_dir.path().join("test_output");
        let options = CompileOptions {
            output: output_path,
            ..Default::default()
        };

        // This may fail due to parser/transpiler limitations, but should not panic
        let result = compile_to_binary(&source_file, &options);
        if let Ok(_) = result {
            // Success is good
        }
        // Expected failure due to incomplete implementation
    }

    // Test 29: rustc version parsing
    #[test]
    fn test_rustc_version_parsing() {
        if let Ok(version) = get_rustc_version() {
            // Should contain rustc and version number
            assert!(version.contains("rustc"));

            // Should contain a version number pattern (X.Y.Z)
            let has_version_pattern = version.split_whitespace().any(|part| {
                part.split('.').count() >= 2 && part.chars().any(|c| c.is_ascii_digit())
            });
            assert!(has_version_pattern);
        }
    }

    // Test 30: Error context propagation
    #[test]
    fn test_error_context_propagation() {
        // Test parse context in parse_and_transpile
        let invalid_source = "syntax error @#$%";
        let result = parse_and_transpile(invalid_source);

        if let Err(error) = result {
            let error_str = format!("{error}");
            // Should contain context information
            assert!(!error_str.is_empty()); // At least some error message
        }

        // Test compile context for invalid paths
        let invalid_path = Path::new("/root/no_permission/file.ruchy");
        let options = CompileOptions::default();

        if invalid_path.exists() {
            // Only test if file actually exists and we can't read it
            let result = compile_to_binary(invalid_path, &options);
            assert!(result.is_err());
        }
    }
}
#[cfg(test)]
mod property_tests_compiler {
    use super::*;
    use proptest::proptest;

    proptest! {
        /// Property: compile_source_to_binary never panics on any string input
        #[test]
        fn test_compile_source_to_binary_never_panics(input: String) {
            // Limit input size to avoid timeout
            let _input = if input.len() > 100 { &input[..100] } else { &input[..] };
            // Function should not panic on any input, even invalid syntax
            let result = std::panic::catch_unwind(|| {
                let options = CompileOptions::default();
                let _ = compile_source_to_binary(&input, &options);
            });
            // Assert that no panic occurred (Result can be Ok or Err, but no panic)
            assert!(result.is_ok(), "compile_source_to_binary panicked on input: {input:?}");
        }
    }
}