alef-core 0.7.2

Core types, config schema, and backend trait for the alef polyglot binding generator
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
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;

pub mod build_defaults;
pub mod clean_defaults;
pub mod dto;
pub mod e2e;
pub mod extras;
pub mod languages;
pub mod lint_defaults;
pub mod output;
pub mod publish;
pub mod setup_defaults;
pub mod test_defaults;
pub mod trait_bridge;
pub mod update_defaults;

// Re-exports for backward compatibility — all types were previously flat in config.rs.
pub use dto::{
    CsharpDtoStyle, DtoConfig, ElixirDtoStyle, GoDtoStyle, JavaDtoStyle, NodeDtoStyle, PhpDtoStyle, PythonDtoStyle,
    RDtoStyle, RubyDtoStyle,
};
pub use e2e::E2eConfig;
pub use extras::{AdapterConfig, AdapterParam, AdapterPattern, Language};
pub use languages::{
    CSharpConfig, CustomModulesConfig, CustomRegistration, CustomRegistrationsConfig, ElixirConfig, FfiConfig,
    GoConfig, JavaConfig, NodeConfig, PhpConfig, PythonConfig, RConfig, RubyConfig, StubsConfig, WasmConfig,
};
pub use output::{
    BuildCommandConfig, CleanConfig, ExcludeConfig, IncludeConfig, LintConfig, OutputConfig, ReadmeConfig,
    ScaffoldConfig, SetupConfig, SyncConfig, TestConfig, TextReplacement, UpdateConfig,
};
pub use publish::{PublishConfig, PublishLanguageConfig, VendorMode};
pub use trait_bridge::TraitBridgeConfig;

/// Root configuration from alef.toml.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlefConfig {
    #[serde(rename = "crate")]
    pub crate_config: CrateConfig,
    pub languages: Vec<Language>,
    #[serde(default)]
    pub exclude: ExcludeConfig,
    #[serde(default)]
    pub include: IncludeConfig,
    #[serde(default)]
    pub output: OutputConfig,
    #[serde(default)]
    pub python: Option<PythonConfig>,
    #[serde(default)]
    pub node: Option<NodeConfig>,
    #[serde(default)]
    pub ruby: Option<RubyConfig>,
    #[serde(default)]
    pub php: Option<PhpConfig>,
    #[serde(default)]
    pub elixir: Option<ElixirConfig>,
    #[serde(default)]
    pub wasm: Option<WasmConfig>,
    #[serde(default)]
    pub ffi: Option<FfiConfig>,
    #[serde(default)]
    pub go: Option<GoConfig>,
    #[serde(default)]
    pub java: Option<JavaConfig>,
    #[serde(default)]
    pub csharp: Option<CSharpConfig>,
    #[serde(default)]
    pub r: Option<RConfig>,
    #[serde(default)]
    pub scaffold: Option<ScaffoldConfig>,
    #[serde(default)]
    pub readme: Option<ReadmeConfig>,
    #[serde(default)]
    pub lint: Option<HashMap<String, LintConfig>>,
    #[serde(default)]
    pub update: Option<HashMap<String, UpdateConfig>>,
    #[serde(default)]
    pub test: Option<HashMap<String, TestConfig>>,
    #[serde(default)]
    pub setup: Option<HashMap<String, SetupConfig>>,
    #[serde(default)]
    pub clean: Option<HashMap<String, CleanConfig>>,
    #[serde(default)]
    pub build_commands: Option<HashMap<String, BuildCommandConfig>>,
    /// Publish pipeline configuration (vendoring, packaging, cross-compilation).
    #[serde(default)]
    pub publish: Option<PublishConfig>,
    #[serde(default)]
    pub custom_files: Option<HashMap<String, Vec<PathBuf>>>,
    #[serde(default)]
    pub adapters: Vec<AdapterConfig>,
    #[serde(default)]
    pub custom_modules: CustomModulesConfig,
    #[serde(default)]
    pub custom_registrations: CustomRegistrationsConfig,
    #[serde(default)]
    pub sync: Option<SyncConfig>,
    /// Declare opaque types from external crates that alef can't extract.
    /// Map of type name → Rust path (e.g., "Tree" = "tree_sitter_language_pack::Tree").
    /// These get opaque wrapper structs in all backends.
    #[serde(default)]
    pub opaque_types: HashMap<String, String>,
    /// Controls which generation passes alef runs (all default to true).
    #[serde(default)]
    pub generate: GenerateConfig,
    /// Per-language overrides for generate flags (key = language name, e.g., "python").
    #[serde(default)]
    pub generate_overrides: HashMap<String, GenerateConfig>,
    /// Per-language DTO/type generation style (dataclass vs TypedDict, zod vs interface, etc.).
    #[serde(default)]
    pub dto: DtoConfig,
    /// E2E test generation configuration.
    #[serde(default)]
    pub e2e: Option<E2eConfig>,
    /// Trait bridge configurations — generate FFI bridge code that allows
    /// foreign language objects to implement Rust traits.
    #[serde(default)]
    pub trait_bridges: Vec<TraitBridgeConfig>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrateConfig {
    pub name: String,
    pub sources: Vec<PathBuf>,
    #[serde(default = "default_version_from")]
    pub version_from: String,
    #[serde(default)]
    pub core_import: Option<String>,
    /// Optional workspace root path for resolving `pub use` re-exports from sibling crates.
    #[serde(default)]
    pub workspace_root: Option<PathBuf>,
    /// When true, skip adding `use {core_import};` to generated bindings.
    #[serde(default)]
    pub skip_core_import: bool,
    /// The crate's error type name (e.g., `"KreuzbergError"`).
    /// Used in trait bridge generation for error wrapping.
    /// Defaults to `"Error"` if not set.
    #[serde(default)]
    pub error_type: Option<String>,
    /// Pattern for constructing error values from a String message in trait bridges.
    /// `{msg}` is replaced with the format!(...) expression.
    /// Example: `"KreuzbergError::Plugin { message: {msg}, plugin_name: name.to_string() }"`
    /// Defaults to `"{error_type}::from({msg})"` if not set.
    #[serde(default)]
    pub error_constructor: Option<String>,
    /// Cargo features that are enabled in binding crates.
    /// Fields gated by `#[cfg(feature = "...")]` matching these features
    /// are treated as always-present (cfg stripped from the IR).
    #[serde(default)]
    pub features: Vec<String>,
    /// Maps extracted rust_path prefixes to actual import paths in binding crates.
    /// Example: { "spikard" = "spikard_http" } rewrites "spikard::ServerConfig" to "spikard_http::ServerConfig"
    #[serde(default)]
    pub path_mappings: HashMap<String, String>,
    /// Additional Cargo dependencies added to ALL binding crate Cargo.tomls.
    /// Each entry is a crate name mapping to a TOML dependency spec
    /// (string for version-only, or inline table for path/features/etc.).
    #[serde(default)]
    pub extra_dependencies: HashMap<String, toml::Value>,
    /// When true (default), automatically derive path_mappings from source file locations.
    /// For each source file matching `crates/{name}/src/`, adds a mapping from
    /// `{name}` to the configured `core_import`.
    #[serde(default = "default_true")]
    pub auto_path_mappings: bool,
    /// Multi-crate source groups for workspaces with types spread across crates.
    /// Each entry has a crate `name` and `sources` list. Types extracted from each
    /// group get `rust_path` reflecting the actual defining crate, not the facade.
    /// When non-empty, the top-level `sources` field is ignored.
    #[serde(default)]
    pub source_crates: Vec<SourceCrate>,
}

/// A source crate group for multi-crate extraction.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceCrate {
    /// Crate name (hyphens converted to underscores for rust_path).
    pub name: String,
    /// Source files belonging to this crate.
    pub sources: Vec<PathBuf>,
}

fn default_version_from() -> String {
    "Cargo.toml".to_string()
}

fn default_true() -> bool {
    true
}

/// Controls which generation passes alef runs.
/// All flags default to `true`; set to `false` to skip a pass.
/// Can be overridden per-language via `[generate_overrides.<lang>]`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenerateConfig {
    /// Generate low-level struct wrappers, From impls, module init (default: true)
    #[serde(default = "default_true")]
    pub bindings: bool,
    /// Generate error type hierarchies from thiserror enums (default: true)
    #[serde(default = "default_true")]
    pub errors: bool,
    /// Generate config builder constructors from Default types (default: true)
    #[serde(default = "default_true")]
    pub configs: bool,
    /// Generate async/sync function pairs with runtime management (default: true)
    #[serde(default = "default_true")]
    pub async_wrappers: bool,
    /// Generate recursive type marshaling helpers (default: true)
    #[serde(default = "default_true")]
    pub type_conversions: bool,
    /// Generate package manifests (pyproject.toml, package.json, etc.) (default: true)
    #[serde(default = "default_true")]
    pub package_metadata: bool,
    /// Generate idiomatic public API wrappers (default: true)
    #[serde(default = "default_true")]
    pub public_api: bool,
    /// Generate `From<BindingType> for CoreType` reverse conversions (default: true).
    /// Set to false when the binding layer only returns core types and never accepts them.
    #[serde(default = "default_true")]
    pub reverse_conversions: bool,
}

impl Default for GenerateConfig {
    fn default() -> Self {
        Self {
            bindings: true,
            errors: true,
            configs: true,
            async_wrappers: true,
            type_conversions: true,
            package_metadata: true,
            public_api: true,
            reverse_conversions: true,
        }
    }
}

// ---------------------------------------------------------------------------
// Shared config resolution helpers
// ---------------------------------------------------------------------------

impl AlefConfig {
    /// Get the features to use for a specific language's binding crate.
    /// Checks for a per-language override first, then falls back to `[crate] features`.
    pub fn features_for_language(&self, lang: extras::Language) -> &[String] {
        let override_features = match lang {
            extras::Language::Python => self.python.as_ref().and_then(|c| c.features.as_deref()),
            extras::Language::Node => self.node.as_ref().and_then(|c| c.features.as_deref()),
            extras::Language::Ruby => self.ruby.as_ref().and_then(|c| c.features.as_deref()),
            extras::Language::Php => self.php.as_ref().and_then(|c| c.features.as_deref()),
            extras::Language::Elixir => self.elixir.as_ref().and_then(|c| c.features.as_deref()),
            extras::Language::Wasm => self.wasm.as_ref().and_then(|c| c.features.as_deref()),
            extras::Language::Ffi => self.ffi.as_ref().and_then(|c| c.features.as_deref()),
            extras::Language::Go => self.go.as_ref().and_then(|c| c.features.as_deref()),
            extras::Language::Java => self.java.as_ref().and_then(|c| c.features.as_deref()),
            extras::Language::Csharp => self.csharp.as_ref().and_then(|c| c.features.as_deref()),
            extras::Language::R => self.r.as_ref().and_then(|c| c.features.as_deref()),
            extras::Language::Rust => None, // Rust doesn't have binding-specific features
        };
        override_features.unwrap_or(&self.crate_config.features)
    }

    /// Get the merged extra dependencies for a specific language's binding crate.
    /// Merges crate-level `extra_dependencies` with per-language overrides.
    /// Language-specific entries override crate-level entries with the same key.
    pub fn extra_deps_for_language(&self, lang: extras::Language) -> HashMap<String, toml::Value> {
        let mut deps = self.crate_config.extra_dependencies.clone();
        let lang_deps = match lang {
            extras::Language::Python => self.python.as_ref().map(|c| &c.extra_dependencies),
            extras::Language::Node => self.node.as_ref().map(|c| &c.extra_dependencies),
            extras::Language::Ruby => self.ruby.as_ref().map(|c| &c.extra_dependencies),
            extras::Language::Php => self.php.as_ref().map(|c| &c.extra_dependencies),
            extras::Language::Elixir => self.elixir.as_ref().map(|c| &c.extra_dependencies),
            extras::Language::Wasm => self.wasm.as_ref().map(|c| &c.extra_dependencies),
            _ => None,
        };
        if let Some(lang_deps) = lang_deps {
            deps.extend(lang_deps.iter().map(|(k, v)| (k.clone(), v.clone())));
        }
        deps
    }

    /// Get the package output directory for a language.
    /// Uses `scaffold_output` from per-language config if set, otherwise defaults.
    ///
    /// Defaults: `packages/python`, `packages/node`, `packages/ruby`, `packages/php`, `packages/elixir`
    pub fn package_dir(&self, lang: extras::Language) -> String {
        let override_path = match lang {
            extras::Language::Python => self.python.as_ref().and_then(|c| c.scaffold_output.as_ref()),
            extras::Language::Node => self.node.as_ref().and_then(|c| c.scaffold_output.as_ref()),
            extras::Language::Ruby => self.ruby.as_ref().and_then(|c| c.scaffold_output.as_ref()),
            extras::Language::Php => self.php.as_ref().and_then(|c| c.scaffold_output.as_ref()),
            extras::Language::Elixir => self.elixir.as_ref().and_then(|c| c.scaffold_output.as_ref()),
            _ => None,
        };
        if let Some(p) = override_path {
            p.to_string_lossy().to_string()
        } else {
            match lang {
                extras::Language::Python => "packages/python".to_string(),
                extras::Language::Node => "packages/node".to_string(),
                extras::Language::Ruby => "packages/ruby".to_string(),
                extras::Language::Php => "packages/php".to_string(),
                extras::Language::Elixir => "packages/elixir".to_string(),
                _ => format!("packages/{lang}"),
            }
        }
    }

    /// Get the effective lint configuration for a language.
    ///
    /// Returns the explicit `[lint.<lang>]` config if present in alef.toml,
    /// otherwise falls back to sensible defaults for the language.
    pub fn lint_config_for_language(&self, lang: extras::Language) -> output::LintConfig {
        if let Some(lint_map) = &self.lint {
            let lang_str = lang.to_string();
            if let Some(explicit) = lint_map.get(&lang_str) {
                return explicit.clone();
            }
        }
        let output_dir = self.package_dir(lang);
        lint_defaults::default_lint_config(lang, &output_dir)
    }

    /// Get the effective update configuration for a language.
    ///
    /// Returns the explicit `[update.<lang>]` config if present in alef.toml,
    /// otherwise falls back to sensible defaults for the language.
    pub fn update_config_for_language(&self, lang: extras::Language) -> output::UpdateConfig {
        if let Some(update_map) = &self.update {
            let lang_str = lang.to_string();
            if let Some(explicit) = update_map.get(&lang_str) {
                return explicit.clone();
            }
        }
        let output_dir = self.package_dir(lang);
        update_defaults::default_update_config(lang, &output_dir)
    }

    /// Get the effective test configuration for a language.
    ///
    /// Returns the explicit `[test.<lang>]` config if present in alef.toml,
    /// otherwise falls back to sensible defaults for the language.
    pub fn test_config_for_language(&self, lang: extras::Language) -> output::TestConfig {
        if let Some(test_map) = &self.test {
            let lang_str = lang.to_string();
            if let Some(explicit) = test_map.get(&lang_str) {
                return explicit.clone();
            }
        }
        let output_dir = self.package_dir(lang);
        test_defaults::default_test_config(lang, &output_dir)
    }

    /// Get the effective setup configuration for a language.
    ///
    /// Returns the explicit `[setup.<lang>]` config if present in alef.toml,
    /// otherwise falls back to sensible defaults for the language.
    pub fn setup_config_for_language(&self, lang: extras::Language) -> output::SetupConfig {
        if let Some(setup_map) = &self.setup {
            let lang_str = lang.to_string();
            if let Some(explicit) = setup_map.get(&lang_str) {
                return explicit.clone();
            }
        }
        let output_dir = self.package_dir(lang);
        setup_defaults::default_setup_config(lang, &output_dir)
    }

    /// Get the effective clean configuration for a language.
    ///
    /// Returns the explicit `[clean.<lang>]` config if present in alef.toml,
    /// otherwise falls back to sensible defaults for the language.
    pub fn clean_config_for_language(&self, lang: extras::Language) -> output::CleanConfig {
        if let Some(clean_map) = &self.clean {
            let lang_str = lang.to_string();
            if let Some(explicit) = clean_map.get(&lang_str) {
                return explicit.clone();
            }
        }
        let output_dir = self.package_dir(lang);
        clean_defaults::default_clean_config(lang, &output_dir)
    }

    /// Get the effective build command configuration for a language.
    ///
    /// Returns the explicit `[build_commands.<lang>]` config if present in alef.toml,
    /// otherwise falls back to sensible defaults for the language.
    pub fn build_command_config_for_language(&self, lang: extras::Language) -> output::BuildCommandConfig {
        if let Some(build_map) = &self.build_commands {
            let lang_str = lang.to_string();
            if let Some(explicit) = build_map.get(&lang_str) {
                return explicit.clone();
            }
        }
        let output_dir = self.package_dir(lang);
        let crate_name = &self.crate_config.name;
        build_defaults::default_build_config(lang, &output_dir, crate_name)
    }

    /// Get the core crate import path (e.g., "liter_llm"). Used by codegen to call into the core crate.
    pub fn core_import(&self) -> String {
        self.crate_config
            .core_import
            .clone()
            .unwrap_or_else(|| self.crate_config.name.replace('-', "_"))
    }

    /// Get the crate error type name (e.g., "KreuzbergError"). Defaults to "Error".
    pub fn error_type(&self) -> String {
        self.crate_config
            .error_type
            .clone()
            .unwrap_or_else(|| "Error".to_string())
    }

    /// Get the error constructor pattern. `{msg}` is replaced with the message expression.
    /// Defaults to `"{core_import}::{error_type}::from({msg})"`.
    pub fn error_constructor(&self) -> String {
        self.crate_config
            .error_constructor
            .clone()
            .unwrap_or_else(|| format!("{}::{}::from({{msg}})", self.core_import(), self.error_type()))
    }

    /// Get the FFI prefix (e.g., "kreuzberg"). Used by FFI, Go, Java, C# backends.
    pub fn ffi_prefix(&self) -> String {
        self.ffi
            .as_ref()
            .and_then(|f| f.prefix.as_ref())
            .cloned()
            .unwrap_or_else(|| self.crate_config.name.replace('-', "_"))
    }

    /// Get the FFI native library name (for Go cgo, Java Panama, C# P/Invoke).
    ///
    /// Resolution order:
    /// 1. `[ffi] lib_name` explicit override
    /// 2. Directory name of `output.ffi` path with hyphens replaced by underscores
    ///    (e.g. `crates/html-to-markdown-ffi/src/` → `html_to_markdown_ffi`)
    /// 3. `{ffi_prefix}_ffi` fallback
    pub fn ffi_lib_name(&self) -> String {
        // 1. Explicit override in [ffi] section.
        if let Some(name) = self.ffi.as_ref().and_then(|f| f.lib_name.as_ref()) {
            return name.clone();
        }

        // 2. Derive from output.ffi path: take the last meaningful directory component
        //    (skip trailing "src" or similar), then replace hyphens with underscores.
        if let Some(ffi_path) = self.output.ffi.as_ref() {
            let path = std::path::Path::new(ffi_path);
            // Walk components from the end to find the crate directory name.
            // Skip components like "src" that are inside the crate dir.
            let components: Vec<_> = path
                .components()
                .filter_map(|c| {
                    if let std::path::Component::Normal(s) = c {
                        s.to_str()
                    } else {
                        None
                    }
                })
                .collect();
            // The crate name is typically the last component that looks like a crate dir
            // (i.e. not "src", "lib", or similar). Search from the end.
            let crate_dir = components
                .iter()
                .rev()
                .find(|&&s| s != "src" && s != "lib" && s != "include")
                .copied();
            if let Some(dir) = crate_dir {
                return dir.replace('-', "_");
            }
        }

        // 3. Default fallback.
        format!("{}_ffi", self.ffi_prefix())
    }

    /// Get the FFI header name.
    pub fn ffi_header_name(&self) -> String {
        self.ffi
            .as_ref()
            .and_then(|f| f.header_name.as_ref())
            .cloned()
            .unwrap_or_else(|| format!("{}.h", self.ffi_prefix()))
    }

    /// Get the Python module name.
    pub fn python_module_name(&self) -> String {
        self.python
            .as_ref()
            .and_then(|p| p.module_name.as_ref())
            .cloned()
            .unwrap_or_else(|| format!("_{}", self.crate_config.name.replace('-', "_")))
    }

    /// Get the PyPI package name used as `[project] name` in `pyproject.toml`.
    ///
    /// Returns `[python] pip_name` if set, otherwise falls back to the crate name.
    pub fn python_pip_name(&self) -> String {
        self.python
            .as_ref()
            .and_then(|p| p.pip_name.as_ref())
            .cloned()
            .unwrap_or_else(|| self.crate_config.name.clone())
    }

    /// Get the PHP Composer autoload namespace derived from the extension name.
    ///
    /// Converts the extension name (e.g. `html_to_markdown_rs`) into a
    /// PSR-4 namespace string (e.g. `Html\\To\\Markdown\\Rs`).
    pub fn php_autoload_namespace(&self) -> String {
        use heck::ToPascalCase;
        let ext = self.php_extension_name();
        if ext.contains('_') {
            ext.split('_')
                .map(|p| p.to_pascal_case())
                .collect::<Vec<_>>()
                .join("\\")
        } else {
            ext.to_pascal_case()
        }
    }

    /// Get the Node package name.
    pub fn node_package_name(&self) -> String {
        self.node
            .as_ref()
            .and_then(|n| n.package_name.as_ref())
            .cloned()
            .unwrap_or_else(|| self.crate_config.name.clone())
    }

    /// Get the Ruby gem name.
    pub fn ruby_gem_name(&self) -> String {
        self.ruby
            .as_ref()
            .and_then(|r| r.gem_name.as_ref())
            .cloned()
            .unwrap_or_else(|| self.crate_config.name.replace('-', "_"))
    }

    /// Get the PHP extension name.
    pub fn php_extension_name(&self) -> String {
        self.php
            .as_ref()
            .and_then(|p| p.extension_name.as_ref())
            .cloned()
            .unwrap_or_else(|| self.crate_config.name.replace('-', "_"))
    }

    /// Get the Elixir app name.
    pub fn elixir_app_name(&self) -> String {
        self.elixir
            .as_ref()
            .and_then(|e| e.app_name.as_ref())
            .cloned()
            .unwrap_or_else(|| self.crate_config.name.replace('-', "_"))
    }

    /// Get the Go module path.
    pub fn go_module(&self) -> String {
        self.go
            .as_ref()
            .and_then(|g| g.module.as_ref())
            .cloned()
            .unwrap_or_else(|| format!("github.com/kreuzberg-dev/{}", self.crate_config.name))
    }

    /// Get the GitHub repository URL.
    ///
    /// Resolution order:
    /// 1. `[e2e.registry] github_repo`
    /// 2. `[scaffold] repository`
    /// 3. Default: `https://github.com/kreuzberg-dev/{crate.name}`
    pub fn github_repo(&self) -> String {
        if let Some(e2e) = &self.e2e {
            if let Some(url) = &e2e.registry.github_repo {
                return url.clone();
            }
        }
        self.scaffold
            .as_ref()
            .and_then(|s| s.repository.as_ref())
            .cloned()
            .unwrap_or_else(|| format!("https://github.com/kreuzberg-dev/{}", self.crate_config.name))
    }

    /// Get the Java package name.
    pub fn java_package(&self) -> String {
        self.java
            .as_ref()
            .and_then(|j| j.package.as_ref())
            .cloned()
            .unwrap_or_else(|| "dev.kreuzberg".to_string())
    }

    /// Get the Java Maven groupId.
    ///
    /// Uses the full Java package as the groupId, matching Maven convention
    /// where groupId equals the package declaration.
    pub fn java_group_id(&self) -> String {
        self.java_package()
    }

    /// Get the C# namespace.
    pub fn csharp_namespace(&self) -> String {
        self.csharp
            .as_ref()
            .and_then(|c| c.namespace.as_ref())
            .cloned()
            .unwrap_or_else(|| {
                use heck::ToPascalCase;
                self.crate_config.name.to_pascal_case()
            })
    }

    /// Get the directory name of the core crate (derived from sources or falling back to name).
    ///
    /// For example, if `sources` contains `"crates/html-to-markdown/src/lib.rs"`, this returns
    /// `"html-to-markdown"`.  Used by the scaffold to generate correct `path = "../../crates/…"`
    /// references in binding-crate `Cargo.toml` files.
    pub fn core_crate_dir(&self) -> String {
        // Try to derive from first source path: "crates/foo/src/types/config.rs" → "foo"
        // Walk up from the file until we find the "src" directory, then take its parent.
        if let Some(first_source) = self.crate_config.sources.first() {
            let path = std::path::Path::new(first_source);
            let mut current = path.parent();
            while let Some(dir) = current {
                if dir.file_name().is_some_and(|n| n == "src") {
                    if let Some(crate_dir) = dir.parent() {
                        if let Some(dir_name) = crate_dir.file_name() {
                            return dir_name.to_string_lossy().into_owned();
                        }
                    }
                    break;
                }
                current = dir.parent();
            }
        }
        self.crate_config.name.clone()
    }

    /// Get the WASM type name prefix (e.g. "Wasm" produces `WasmConversionOptions`).
    /// Defaults to `"Wasm"`.
    pub fn wasm_type_prefix(&self) -> String {
        self.wasm
            .as_ref()
            .and_then(|w| w.type_prefix.as_ref())
            .cloned()
            .unwrap_or_else(|| "Wasm".to_string())
    }

    /// Get the Node/NAPI type name prefix (e.g. "Js" produces `JsConversionOptions`).
    /// Defaults to `"Js"`.
    pub fn node_type_prefix(&self) -> String {
        self.node
            .as_ref()
            .and_then(|n| n.type_prefix.as_ref())
            .cloned()
            .unwrap_or_else(|| "Js".to_string())
    }

    /// Get the R package name.
    pub fn r_package_name(&self) -> String {
        self.r
            .as_ref()
            .and_then(|r| r.package_name.as_ref())
            .cloned()
            .unwrap_or_else(|| self.crate_config.name.clone())
    }

    /// Attempt to read the resolved version string from the configured `version_from` file.
    /// Returns `None` if the file cannot be read or the version cannot be found.
    pub fn resolved_version(&self) -> Option<String> {
        let content = std::fs::read_to_string(&self.crate_config.version_from).ok()?;
        let value: toml::Value = toml::from_str(&content).ok()?;
        if let Some(v) = value
            .get("workspace")
            .and_then(|w| w.get("package"))
            .and_then(|p| p.get("version"))
            .and_then(|v| v.as_str())
        {
            return Some(v.to_string());
        }
        value
            .get("package")
            .and_then(|p| p.get("version"))
            .and_then(|v| v.as_str())
            .map(|v| v.to_string())
    }

    /// Get the effective serde rename_all strategy for a given language.
    ///
    /// Resolution order:
    /// 1. Per-language config override (`[python] serde_rename_all = "..."`)
    /// 2. Language default:
    ///    - camelCase: node, wasm, java, csharp
    ///    - snake_case: python, ruby, php, go, ffi, elixir, r
    pub fn serde_rename_all_for_language(&self, lang: extras::Language) -> String {
        // 1. Check per-language config override.
        let override_val = match lang {
            extras::Language::Python => self.python.as_ref().and_then(|c| c.serde_rename_all.as_deref()),
            extras::Language::Node => self.node.as_ref().and_then(|c| c.serde_rename_all.as_deref()),
            extras::Language::Ruby => self.ruby.as_ref().and_then(|c| c.serde_rename_all.as_deref()),
            extras::Language::Php => self.php.as_ref().and_then(|c| c.serde_rename_all.as_deref()),
            extras::Language::Elixir => self.elixir.as_ref().and_then(|c| c.serde_rename_all.as_deref()),
            extras::Language::Wasm => self.wasm.as_ref().and_then(|c| c.serde_rename_all.as_deref()),
            extras::Language::Ffi => self.ffi.as_ref().and_then(|c| c.serde_rename_all.as_deref()),
            extras::Language::Go => self.go.as_ref().and_then(|c| c.serde_rename_all.as_deref()),
            extras::Language::Java => self.java.as_ref().and_then(|c| c.serde_rename_all.as_deref()),
            extras::Language::Csharp => self.csharp.as_ref().and_then(|c| c.serde_rename_all.as_deref()),
            extras::Language::R => self.r.as_ref().and_then(|c| c.serde_rename_all.as_deref()),
            extras::Language::Rust => None, // Rust uses native naming (snake_case)
        };

        if let Some(val) = override_val {
            return val.to_string();
        }

        // 2. Language defaults.
        match lang {
            extras::Language::Node | extras::Language::Wasm | extras::Language::Java | extras::Language::Csharp => {
                "camelCase".to_string()
            }
            extras::Language::Python
            | extras::Language::Ruby
            | extras::Language::Php
            | extras::Language::Go
            | extras::Language::Ffi
            | extras::Language::Elixir
            | extras::Language::R
            | extras::Language::Rust => "snake_case".to_string(),
        }
    }

    /// Rewrite a rust_path using path_mappings.
    /// Matches the longest prefix first.
    pub fn rewrite_path(&self, rust_path: &str) -> String {
        // Sort mappings by key length descending (longest prefix first)
        let mut mappings: Vec<_> = self.crate_config.path_mappings.iter().collect();
        mappings.sort_by_key(|b| std::cmp::Reverse(b.0.len()));

        for (from, to) in &mappings {
            if rust_path.starts_with(from.as_str()) {
                return format!("{}{}", to, &rust_path[from.len()..]);
            }
        }
        rust_path.to_string()
    }

    /// Return the effective path mappings for this config.
    ///
    /// When `auto_path_mappings` is true, automatically derives a mapping from each source
    /// crate to the configured `core_import` facade.  For each source file whose path contains
    /// `crates/{crate-name}/src/`, a mapping `{crate_name}` → `{core_import}` is added
    /// (hyphens in the crate name are converted to underscores).  Source crates that already
    /// equal `core_import` are skipped.
    ///
    /// Explicit entries in `path_mappings` always override auto-derived ones.
    pub fn effective_path_mappings(&self) -> HashMap<String, String> {
        let mut mappings = HashMap::new();

        if self.crate_config.auto_path_mappings {
            let core_import = self.core_import();

            for source in &self.crate_config.sources {
                let source_str = source.to_string_lossy();
                // Match `crates/{name}/src/` pattern in the path.
                if let Some(after_crates) = find_after_crates_prefix(&source_str) {
                    // Extract the crate directory name (everything before the next `/`).
                    if let Some(slash_pos) = after_crates.find('/') {
                        let crate_dir = &after_crates[..slash_pos];
                        let crate_ident = crate_dir.replace('-', "_");
                        // Only add a mapping when the source crate differs from the facade.
                        if crate_ident != core_import && !mappings.contains_key(&crate_ident) {
                            mappings.insert(crate_ident, core_import.clone());
                        }
                    }
                }
            }
        }

        // Explicit path_mappings always win — insert last so they overwrite auto entries.
        for (from, to) in &self.crate_config.path_mappings {
            mappings.insert(from.clone(), to.clone());
        }

        mappings
    }
}

/// Find the path segment that comes after a `crates/` component.
///
/// Handles both absolute paths (e.g., `/workspace/repo/crates/foo/src/lib.rs`)
/// and relative paths (e.g., `crates/foo/src/lib.rs`).  Returns the slice
/// starting immediately after the `crates/` prefix, or `None` if the path
/// does not contain such a component.
fn find_after_crates_prefix(path: &str) -> Option<&str> {
    // Normalise to forward slashes for cross-platform matching.
    // We search for `/crates/` (with leading slash) first, then fall back to
    // a leading `crates/` for relative paths that start with that component.
    if let Some(pos) = path.find("/crates/") {
        return Some(&path[pos + "/crates/".len()..]);
    }
    if let Some(stripped) = path.strip_prefix("crates/") {
        return Some(stripped);
    }
    None
}

/// Helper function to resolve output directory path from config.
/// Replaces {name} placeholder with the crate name.
pub fn resolve_output_dir(config_path: Option<&PathBuf>, crate_name: &str, default: &str) -> String {
    config_path
        .map(|p| p.to_string_lossy().replace("{name}", crate_name))
        .unwrap_or_else(|| default.replace("{name}", crate_name))
}

/// Detect whether `serde` and `serde_json` are available in a binding crate's Cargo.toml.
///
/// `output_dir` is the generated source directory (e.g., `crates/spikard-py/src/`).
/// The function walks up to find the crate's Cargo.toml and checks its `[dependencies]`
/// for both `serde` and `serde_json`.
pub fn detect_serde_available(output_dir: &str) -> bool {
    let src_path = std::path::Path::new(output_dir);
    // Walk up from the output dir to find Cargo.toml (usually output_dir is `crates/foo/src/`)
    let mut dir = src_path;
    loop {
        let cargo_toml = dir.join("Cargo.toml");
        if cargo_toml.exists() {
            return cargo_toml_has_serde(&cargo_toml);
        }
        match dir.parent() {
            Some(parent) if !parent.as_os_str().is_empty() => dir = parent,
            _ => break,
        }
    }
    false
}

/// Check if a Cargo.toml has both `serde` (with derive feature) and `serde_json` in its dependencies.
///
/// The `serde::Serialize` derive macro requires `serde` as a direct dependency with the `derive`
/// feature enabled. Having only `serde_json` is not sufficient since it only pulls in `serde`
/// transitively without the derive proc-macro.
fn cargo_toml_has_serde(path: &std::path::Path) -> bool {
    let content = match std::fs::read_to_string(path) {
        Ok(c) => c,
        Err(_) => return false,
    };

    let has_serde_json = content.contains("serde_json");
    // Check for `serde` as a direct dependency (not just serde_json).
    // Must match "serde" as a TOML key, not as a substring of "serde_json".
    // Valid patterns: `serde = `, `serde.`, `[dependencies.serde]`
    let has_serde_dep = content.lines().any(|line| {
        let trimmed = line.trim();
        // Match `serde = ...` or `serde.workspace = true` etc., but not `serde_json`
        trimmed.starts_with("serde ")
            || trimmed.starts_with("serde=")
            || trimmed.starts_with("serde.")
            || trimmed == "[dependencies.serde]"
    });

    has_serde_json && has_serde_dep
}

#[cfg(test)]
mod tests {
    use super::*;

    fn minimal_config() -> AlefConfig {
        toml::from_str(
            r#"
languages = ["python", "node", "rust"]

[crate]
name = "test-lib"
sources = ["src/lib.rs"]
"#,
        )
        .unwrap()
    }

    #[test]
    fn lint_config_falls_back_to_defaults() {
        let config = minimal_config();
        assert!(config.lint.is_none());

        let py = config.lint_config_for_language(Language::Python);
        assert!(py.format.is_some());
        assert!(py.check.is_some());
        assert!(py.typecheck.is_some());

        let node = config.lint_config_for_language(Language::Node);
        assert!(node.format.is_some());
        assert!(node.check.is_some());
    }

    #[test]
    fn lint_config_explicit_overrides_default() {
        let config: AlefConfig = toml::from_str(
            r#"
languages = ["python"]

[crate]
name = "test-lib"
sources = ["src/lib.rs"]

[lint.python]
format = "custom-formatter"
check = "custom-checker"
"#,
        )
        .unwrap();

        let py = config.lint_config_for_language(Language::Python);
        assert_eq!(py.format.unwrap().commands(), vec!["custom-formatter"]);
        assert_eq!(py.check.unwrap().commands(), vec!["custom-checker"]);
        assert!(py.typecheck.is_none()); // explicit config had no typecheck
    }

    #[test]
    fn lint_config_partial_override_does_not_merge() {
        let config: AlefConfig = toml::from_str(
            r#"
languages = ["python"]

[crate]
name = "test-lib"
sources = ["src/lib.rs"]

[lint.python]
format = "only-format"
"#,
        )
        .unwrap();

        let py = config.lint_config_for_language(Language::Python);
        assert_eq!(py.format.unwrap().commands(), vec!["only-format"]);
        // Explicit config replaces entirely, no fallback for missing fields
        assert!(py.check.is_none());
        assert!(py.typecheck.is_none());
    }

    #[test]
    fn lint_config_unconfigured_language_uses_defaults() {
        let config: AlefConfig = toml::from_str(
            r#"
languages = ["python", "node"]

[crate]
name = "test-lib"
sources = ["src/lib.rs"]

[lint.python]
format = "custom"
"#,
        )
        .unwrap();

        // Python uses explicit config
        let py = config.lint_config_for_language(Language::Python);
        assert_eq!(py.format.unwrap().commands(), vec!["custom"]);

        // Node falls back to defaults since not in [lint]
        let node = config.lint_config_for_language(Language::Node);
        let fmt = node.format.unwrap().commands().join(" ");
        assert!(fmt.contains("oxfmt"));
    }

    #[test]
    fn update_config_falls_back_to_defaults() {
        let config = minimal_config();
        assert!(config.update.is_none());

        let py = config.update_config_for_language(Language::Python);
        assert!(py.update.is_some());
        assert!(py.upgrade.is_some());

        let rust = config.update_config_for_language(Language::Rust);
        let update = rust.update.unwrap().commands().join(" ");
        assert!(update.contains("cargo update"));
    }

    #[test]
    fn update_config_explicit_overrides_default() {
        let config: AlefConfig = toml::from_str(
            r#"
languages = ["rust"]

[crate]
name = "test-lib"
sources = ["src/lib.rs"]

[update.rust]
update = "my-custom-update"
upgrade = ["step1", "step2"]
"#,
        )
        .unwrap();

        let rust = config.update_config_for_language(Language::Rust);
        assert_eq!(rust.update.unwrap().commands(), vec!["my-custom-update"]);
        assert_eq!(rust.upgrade.unwrap().commands(), vec!["step1", "step2"]);
    }

    #[test]
    fn test_config_falls_back_to_defaults() {
        let config = minimal_config();
        assert!(config.test.is_none());

        let py = config.test_config_for_language(Language::Python);
        assert!(py.command.is_some());
        assert!(py.coverage.is_some());
        assert!(py.e2e.is_none());

        let rust = config.test_config_for_language(Language::Rust);
        let cmd = rust.command.unwrap().commands().join(" ");
        assert!(cmd.contains("cargo test"));
    }

    #[test]
    fn test_config_explicit_overrides_default() {
        let config: AlefConfig = toml::from_str(
            r#"
languages = ["python"]

[crate]
name = "test-lib"
sources = ["src/lib.rs"]

[test.python]
command = "my-custom-test"
"#,
        )
        .unwrap();

        let py = config.test_config_for_language(Language::Python);
        assert_eq!(py.command.unwrap().commands(), vec!["my-custom-test"]);
        assert!(py.coverage.is_none()); // explicit config had no coverage
    }

    #[test]
    fn setup_config_falls_back_to_defaults() {
        let config = minimal_config();
        assert!(config.setup.is_none());

        let py = config.setup_config_for_language(Language::Python);
        assert!(py.install.is_some());
        let install = py.install.unwrap().commands().join(" ");
        assert!(install.contains("uv sync"));

        let rust = config.setup_config_for_language(Language::Rust);
        let install = rust.install.unwrap().commands().join(" ");
        assert!(install.contains("rustup update"));
    }

    #[test]
    fn setup_config_explicit_overrides_default() {
        let config: AlefConfig = toml::from_str(
            r#"
languages = ["python"]

[crate]
name = "test-lib"
sources = ["src/lib.rs"]

[setup.python]
install = "my-custom-install"
"#,
        )
        .unwrap();

        let py = config.setup_config_for_language(Language::Python);
        assert_eq!(py.install.unwrap().commands(), vec!["my-custom-install"]);
    }

    #[test]
    fn clean_config_falls_back_to_defaults() {
        let config = minimal_config();
        assert!(config.clean.is_none());

        let py = config.clean_config_for_language(Language::Python);
        assert!(py.clean.is_some());
        let clean = py.clean.unwrap().commands().join(" ");
        assert!(clean.contains("__pycache__"));

        let rust = config.clean_config_for_language(Language::Rust);
        let clean = rust.clean.unwrap().commands().join(" ");
        assert!(clean.contains("cargo clean"));
    }

    #[test]
    fn clean_config_explicit_overrides_default() {
        let config: AlefConfig = toml::from_str(
            r#"
languages = ["rust"]

[crate]
name = "test-lib"
sources = ["src/lib.rs"]

[clean.rust]
clean = "my-custom-clean"
"#,
        )
        .unwrap();

        let rust = config.clean_config_for_language(Language::Rust);
        assert_eq!(rust.clean.unwrap().commands(), vec!["my-custom-clean"]);
    }

    #[test]
    fn build_command_config_falls_back_to_defaults() {
        let config = minimal_config();
        assert!(config.build_commands.is_none());

        let py = config.build_command_config_for_language(Language::Python);
        assert!(py.build.is_some());
        assert!(py.build_release.is_some());
        let build = py.build.unwrap().commands().join(" ");
        assert!(build.contains("maturin develop"));

        let rust = config.build_command_config_for_language(Language::Rust);
        let build = rust.build.unwrap().commands().join(" ");
        assert!(build.contains("cargo build --workspace"));
    }

    #[test]
    fn build_command_config_explicit_overrides_default() {
        let config: AlefConfig = toml::from_str(
            r#"
languages = ["rust"]

[crate]
name = "test-lib"
sources = ["src/lib.rs"]

[build_commands.rust]
build = "my-custom-build"
build_release = "my-custom-build --release"
"#,
        )
        .unwrap();

        let rust = config.build_command_config_for_language(Language::Rust);
        assert_eq!(rust.build.unwrap().commands(), vec!["my-custom-build"]);
        assert_eq!(
            rust.build_release.unwrap().commands(),
            vec!["my-custom-build --release"]
        );
    }

    #[test]
    fn build_command_config_uses_crate_name() {
        let config = minimal_config();
        let py = config.build_command_config_for_language(Language::Python);
        let build = py.build.unwrap().commands().join(" ");
        assert!(
            build.contains("test-lib-py"),
            "Python build should reference crate name, got: {build}"
        );
    }

    #[test]
    fn package_dir_defaults_are_correct() {
        let config = minimal_config();
        assert_eq!(config.package_dir(Language::Python), "packages/python");
        assert_eq!(config.package_dir(Language::Node), "packages/node");
        assert_eq!(config.package_dir(Language::Ruby), "packages/ruby");
        assert_eq!(config.package_dir(Language::Go), "packages/go");
        assert_eq!(config.package_dir(Language::Java), "packages/java");
    }

    #[test]
    fn explicit_lint_config_preserves_precondition_and_before() {
        let config: AlefConfig = toml::from_str(
            r#"
languages = ["go"]

[crate]
name = "test"
sources = ["src/lib.rs"]

[lint.go]
precondition = "test -f target/release/libtest_ffi.so"
before = "cargo build --release -p test-ffi"
format = "gofmt -w packages/go"
check = "golangci-lint run ./..."
"#,
        )
        .unwrap();

        let lint = config.lint_config_for_language(Language::Go);
        assert_eq!(
            lint.precondition.as_deref(),
            Some("test -f target/release/libtest_ffi.so"),
            "precondition should be preserved from explicit config"
        );
        assert_eq!(
            lint.before.unwrap().commands(),
            vec!["cargo build --release -p test-ffi"],
            "before should be preserved from explicit config"
        );
    }

    #[test]
    fn explicit_lint_config_with_before_list_preserves_all_commands() {
        let config: AlefConfig = toml::from_str(
            r#"
languages = ["go"]

[crate]
name = "test"
sources = ["src/lib.rs"]

[lint.go]
before = ["cargo build --release -p test-ffi", "cp target/release/libtest_ffi.so packages/go/"]
check = "golangci-lint run ./..."
"#,
        )
        .unwrap();

        let lint = config.lint_config_for_language(Language::Go);
        assert!(lint.precondition.is_none(), "precondition should be None when not set");
        assert_eq!(
            lint.before.unwrap().commands(),
            vec![
                "cargo build --release -p test-ffi",
                "cp target/release/libtest_ffi.so packages/go/"
            ],
            "before list should be preserved from explicit config"
        );
    }

    #[test]
    fn default_lint_config_has_no_precondition_or_before() {
        let config = minimal_config();
        let py = config.lint_config_for_language(Language::Python);
        assert!(
            py.precondition.is_none(),
            "default lint config should have no precondition"
        );
        assert!(py.before.is_none(), "default lint config should have no before");

        let go = config.lint_config_for_language(Language::Go);
        assert!(
            go.precondition.is_none(),
            "default Go lint config should have no precondition"
        );
        assert!(go.before.is_none(), "default Go lint config should have no before");
    }

    #[test]
    fn explicit_test_config_preserves_precondition_and_before() {
        let config: AlefConfig = toml::from_str(
            r#"
languages = ["python"]

[crate]
name = "test"
sources = ["src/lib.rs"]

[test.python]
precondition = "test -f target/release/libtest.so"
before = "maturin develop"
command = "pytest"
"#,
        )
        .unwrap();

        let test = config.test_config_for_language(Language::Python);
        assert_eq!(
            test.precondition.as_deref(),
            Some("test -f target/release/libtest.so"),
            "test precondition should be preserved"
        );
        assert_eq!(
            test.before.unwrap().commands(),
            vec!["maturin develop"],
            "test before should be preserved"
        );
    }

    #[test]
    fn default_test_config_has_no_precondition_or_before() {
        let config = minimal_config();
        let py = config.test_config_for_language(Language::Python);
        assert!(
            py.precondition.is_none(),
            "default test config should have no precondition"
        );
        assert!(py.before.is_none(), "default test config should have no before");
    }

    #[test]
    fn explicit_setup_config_preserves_precondition_and_before() {
        let config: AlefConfig = toml::from_str(
            r#"
languages = ["python"]

[crate]
name = "test"
sources = ["src/lib.rs"]

[setup.python]
precondition = "which uv"
before = "pip install uv"
install = "uv sync"
"#,
        )
        .unwrap();

        let setup = config.setup_config_for_language(Language::Python);
        assert_eq!(
            setup.precondition.as_deref(),
            Some("which uv"),
            "setup precondition should be preserved"
        );
        assert_eq!(
            setup.before.unwrap().commands(),
            vec!["pip install uv"],
            "setup before should be preserved"
        );
    }

    #[test]
    fn default_setup_config_has_no_precondition_or_before() {
        let config = minimal_config();
        let py = config.setup_config_for_language(Language::Python);
        assert!(
            py.precondition.is_none(),
            "default setup config should have no precondition"
        );
        assert!(py.before.is_none(), "default setup config should have no before");
    }

    #[test]
    fn explicit_update_config_preserves_precondition_and_before() {
        let config: AlefConfig = toml::from_str(
            r#"
languages = ["rust"]

[crate]
name = "test"
sources = ["src/lib.rs"]

[update.rust]
precondition = "test -f Cargo.lock"
before = "cargo fetch"
update = "cargo update"
"#,
        )
        .unwrap();

        let update = config.update_config_for_language(Language::Rust);
        assert_eq!(
            update.precondition.as_deref(),
            Some("test -f Cargo.lock"),
            "update precondition should be preserved"
        );
        assert_eq!(
            update.before.unwrap().commands(),
            vec!["cargo fetch"],
            "update before should be preserved"
        );
    }

    #[test]
    fn default_update_config_has_no_precondition_or_before() {
        let config = minimal_config();
        let rust = config.update_config_for_language(Language::Rust);
        assert!(
            rust.precondition.is_none(),
            "default update config should have no precondition"
        );
        assert!(rust.before.is_none(), "default update config should have no before");
    }

    #[test]
    fn explicit_clean_config_preserves_precondition_and_before() {
        let config: AlefConfig = toml::from_str(
            r#"
languages = ["rust"]

[crate]
name = "test"
sources = ["src/lib.rs"]

[clean.rust]
precondition = "test -d target"
before = "echo cleaning"
clean = "cargo clean"
"#,
        )
        .unwrap();

        let clean = config.clean_config_for_language(Language::Rust);
        assert_eq!(
            clean.precondition.as_deref(),
            Some("test -d target"),
            "clean precondition should be preserved"
        );
        assert_eq!(
            clean.before.unwrap().commands(),
            vec!["echo cleaning"],
            "clean before should be preserved"
        );
    }

    #[test]
    fn default_clean_config_has_no_precondition_or_before() {
        let config = minimal_config();
        let rust = config.clean_config_for_language(Language::Rust);
        assert!(
            rust.precondition.is_none(),
            "default clean config should have no precondition"
        );
        assert!(rust.before.is_none(), "default clean config should have no before");
    }

    #[test]
    fn explicit_build_command_config_preserves_precondition_and_before() {
        let config: AlefConfig = toml::from_str(
            r#"
languages = ["go"]

[crate]
name = "test"
sources = ["src/lib.rs"]

[build_commands.go]
precondition = "which go"
before = "cargo build --release -p test-ffi"
build = "cd packages/go && go build ./..."
build_release = "cd packages/go && go build -ldflags='-s -w' ./..."
"#,
        )
        .unwrap();

        let build = config.build_command_config_for_language(Language::Go);
        assert_eq!(
            build.precondition.as_deref(),
            Some("which go"),
            "build precondition should be preserved"
        );
        assert_eq!(
            build.before.unwrap().commands(),
            vec!["cargo build --release -p test-ffi"],
            "build before should be preserved"
        );
    }

    #[test]
    fn default_build_command_config_has_no_precondition_or_before() {
        let config = minimal_config();
        let rust = config.build_command_config_for_language(Language::Rust);
        assert!(
            rust.precondition.is_none(),
            "default build command config should have no precondition"
        );
        assert!(
            rust.before.is_none(),
            "default build command config should have no before"
        );
    }
}