Skip to main content

alef_core/config/
languages.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use std::path::PathBuf;
4
5use super::extras::Language;
6
7/// Configuration for a single capsule type entry in `PythonConfig::capsule_types`.
8///
9/// Supports two TOML forms via `#[serde(untagged)]`:
10///
11/// - String: `Language = "tree_sitter.Language"` → capsule round-trip via `into_raw()`
12/// - Struct: `Parser = { python_type = "tree_sitter.Parser", construct_from = "Language" }` → Python-side construction
13#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
14#[serde(untagged)]
15pub enum CapsuleTypeConfig {
16    /// Capsule round-trip: the Rust type exposes `into_raw()` returning a raw pointer.
17    /// The generated code calls `PyCapsule_New(value.into_raw(), capsule_name, None)` on return,
18    /// and `PyCapsule_GetPointer` + `from_raw()` on input.
19    ///
20    /// Value is the fully-qualified Python capsule name (e.g. `"tree_sitter.Language"`).
21    Capsule(String),
22    /// Python-side construction: the type does not have a direct `into_raw()`.
23    /// Instead, the generated code constructs the Python type by calling a Python factory
24    /// (e.g. `tree_sitter.Parser(language)`) where `language` is a bound capsule argument.
25    ConstructFrom {
26        /// The fully-qualified Python type to import and call (e.g. `"tree_sitter.Parser"`).
27        python_type: String,
28        /// The capsule-type argument name to pass to the Python constructor.
29        /// Must be one of the other capsule-type entries (e.g. `"Language"`).
30        construct_from: String,
31    },
32}
33
34impl CapsuleTypeConfig {
35    /// Returns the Python type string (dotted path) for this config entry.
36    pub fn python_type(&self) -> &str {
37        match self {
38            Self::Capsule(name) => name,
39            Self::ConstructFrom { python_type, .. } => python_type,
40        }
41    }
42
43    /// Returns the `construct_from` dependency type name, if this is a `ConstructFrom` entry.
44    pub fn construct_from(&self) -> Option<&str> {
45        match self {
46            Self::ConstructFrom { construct_from, .. } => Some(construct_from.as_str()),
47            Self::Capsule(_) => None,
48        }
49    }
50
51    /// Returns true when this entry represents a raw capsule round-trip (not Python-side construction).
52    pub fn is_capsule_roundtrip(&self) -> bool {
53        matches!(self, Self::Capsule(_))
54    }
55}
56
57#[derive(Debug, Clone, Serialize, Deserialize)]
58pub struct PythonConfig {
59    pub module_name: Option<String>,
60    pub async_runtime: Option<String>,
61    pub stubs: Option<StubsConfig>,
62    /// PyPI package name (e.g. `"html-to-markdown"`). Used as the `[project] name` in
63    /// `pyproject.toml` and to derive the `python-packages` list for maturin.
64    /// Defaults to the crate name.
65    #[serde(default)]
66    pub pip_name: Option<String>,
67    /// Per-language feature override. When set, these features are used instead of
68    /// `[crate] features` for this language's binding crate.
69    #[serde(default)]
70    pub features: Option<Vec<String>>,
71    /// Override the serde rename_all strategy for JSON field names (e.g. "camelCase", "snake_case").
72    /// When set, this takes priority over the IR type-level serde_rename_all.
73    #[serde(default)]
74    pub serde_rename_all: Option<String>,
75    /// Map of type name -> capsule config for PyCapsule pass-through.
76    /// Types listed here are emitted as PyCapsule_New / PyCapsule_GetPointer instead of
77    /// opaque `#[pyclass]` wrappers. Use `CapsuleTypeConfig::Capsule` for raw capsule
78    /// round-trips and `CapsuleTypeConfig::ConstructFrom` for Python-side construction.
79    #[serde(default)]
80    pub capsule_types: HashMap<String, CapsuleTypeConfig>,
81    /// When true, wrap blocking function bodies in py.allow_threads() to release the GIL.
82    // TODO: Wire into gen_bindings.rs to emit py.allow_threads(|| { ... }) for non-async functions.
83    #[serde(default)]
84    pub release_gil: bool,
85    /// Functions to exclude from Python binding generation.
86    #[serde(default)]
87    pub exclude_functions: Vec<String>,
88    /// Types to exclude from Python binding generation.
89    #[serde(default)]
90    pub exclude_types: Vec<String>,
91    /// Additional Cargo dependencies for this language's binding crate only.
92    #[serde(default)]
93    pub extra_dependencies: HashMap<String, toml::Value>,
94    /// Override the scaffold output directory for this language's Cargo.toml and package files.
95    #[serde(default)]
96    pub scaffold_output: Option<PathBuf>,
97    /// Per-field name remapping for this language. Key is `TypeName.field_name` (e.g.
98    /// `"LayoutDetection.class"`), value is the desired binding field name. Applied after
99    /// automatic keyword escaping, so an explicit entry takes priority.
100    #[serde(default)]
101    pub rename_fields: HashMap<String, String>,
102    /// Prefix wrapper for default tool invocations. When set, prepends this string to default
103    /// commands across all pipelines (lint, test, build, etc.).
104    /// E.g., `run_wrapper = "uv run --no-sync"` turns `ruff format packages/python` into
105    /// `uv run --no-sync ruff format packages/python`.
106    #[serde(default)]
107    pub run_wrapper: Option<String>,
108    /// Extra paths to append to default lint commands (format, check, typecheck).
109    /// Space-separated paths are appended to the command.
110    #[serde(default)]
111    pub extra_lint_paths: Vec<String>,
112    /// Additional `from <module> import <symbol>` lines to emit in the generated `__init__.py`.
113    /// Key is the relative or absolute Python module path (e.g. `"._supported_languages"`),
114    /// value is the list of symbols to import. The symbols are also added to `__all__`.
115    ///
116    /// Use this to re-export hand-written sibling modules (e.g. generated by a project's own
117    /// build script) without alef's cleanup culling them. The hand-written file must NOT contain
118    /// the substrings `"DO NOT EDIT"`, `"auto-generated by alef"`, or `"AUTO-GENERATED by alef"`
119    /// in its first 5 lines, or alef's cleanup pipeline will treat it as a stale alef artifact.
120    #[serde(default)]
121    pub extra_init_imports: std::collections::BTreeMap<String, Vec<String>>,
122}
123
124#[derive(Debug, Clone, Serialize, Deserialize)]
125pub struct StubsConfig {
126    pub output: PathBuf,
127}
128
129/// Configuration for a single capsule type entry in `NodeConfig::capsule_types`.
130///
131/// When set, the named Rust type is NOT emitted as a `#[napi]` opaque wrapper.
132/// Instead, functions returning this type produce a `JsObject` carrying the raw
133/// pointer in a configurable `Napi::External<T>` property — the layout consumed
134/// by the `tree-sitter` npm package's `Parser.setLanguage()`.
135///
136/// TOML form:
137/// ```toml
138/// [crates.node.capsule_types.Language]
139/// type = "Language"
140/// from_module = "tree-sitter"
141/// property_name = "language"
142/// type_tag = { lower = "0x8AF2E5212AD58ABF", upper = "0xD5006CAD83ABBA16" }
143/// ```
144#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
145pub struct NodeCapsuleTypeConfig {
146    /// User-facing class name in the ecosystem library (e.g. `"Language"`).
147    /// Emitted as the return-type annotation in the generated `index.d.ts`.
148    #[serde(rename = "type")]
149    pub type_name: String,
150    /// npm package to import the type from (e.g. `"tree-sitter"`).
151    /// Emitted as the `from` clause in the generated `import type` line.
152    pub from_module: String,
153    /// Codegen strategy. Currently only `"external_pointer"` is supported.
154    /// Defaults to `"external_pointer"`.
155    #[serde(default = "default_node_capsule_construct")]
156    pub construct: String,
157    /// JS property name to set on the returned object. `node-tree-sitter`
158    /// reads `value["language"]`; other consumers may use different names.
159    /// Defaults to `"__parser"` for back-compat with existing configs.
160    #[serde(default = "default_node_capsule_property_name")]
161    pub property_name: String,
162    /// Optional N-API type tag to apply via `napi_type_tag_object`. Required
163    /// when the consumer library (e.g. `node-tree-sitter`) calls
164    /// `napi_check_object_type_tag` to validate the External before using it.
165    #[serde(default)]
166    pub type_tag: Option<NapiTypeTagConfig>,
167}
168
169/// An N-API `napi_type_tag` value, expressed as two 64-bit hex strings.
170#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
171pub struct NapiTypeTagConfig {
172    /// Lower 64 bits of the tag, hex (e.g. `"0x8AF2E5212AD58ABF"`).
173    pub lower: String,
174    /// Upper 64 bits of the tag, hex (e.g. `"0xD5006CAD83ABBA16"`).
175    pub upper: String,
176}
177
178fn default_node_capsule_construct() -> String {
179    "external_pointer".to_string()
180}
181
182fn default_node_capsule_property_name() -> String {
183    "__parser".to_string()
184}
185
186#[derive(Debug, Clone, Serialize, Deserialize)]
187pub struct NodeConfig {
188    pub package_name: Option<String>,
189    /// Per-language feature override. When set, these features are used instead of
190    /// `[crate] features` for this language's binding crate.
191    #[serde(default)]
192    pub features: Option<Vec<String>>,
193    /// Override the serde rename_all strategy for JSON field names (e.g. "camelCase", "snake_case").
194    /// When set, this takes priority over the IR type-level serde_rename_all.
195    #[serde(default)]
196    pub serde_rename_all: Option<String>,
197    /// Prefix for generated type names (e.g. "Js" produces `JsConversionOptions`).
198    /// Defaults to `"Js"`.
199    #[serde(default)]
200    pub type_prefix: Option<String>,
201    /// Map of Rust type name -> capsule config for raw-pointer passthrough.
202    /// Types listed here skip the default `#[napi]` opaque-wrapper emission;
203    /// functions returning them produce a `JsObject` with a `__parser`
204    /// `Napi::External<T>` property instead. See [`NodeCapsuleTypeConfig`].
205    #[serde(default)]
206    pub capsule_types: HashMap<String, NodeCapsuleTypeConfig>,
207    /// Functions to exclude from Node binding generation.
208    #[serde(default)]
209    pub exclude_functions: Vec<String>,
210    /// Types to exclude from Node binding generation.
211    #[serde(default)]
212    pub exclude_types: Vec<String>,
213    /// Additional Cargo dependencies for this language's binding crate only.
214    #[serde(default)]
215    pub extra_dependencies: HashMap<String, toml::Value>,
216    /// Override the scaffold output directory for this language's Cargo.toml and package files.
217    #[serde(default)]
218    pub scaffold_output: Option<PathBuf>,
219    /// Per-field name remapping for this language. Key is `TypeName.field_name`, value is the
220    /// desired binding field name. Applied after automatic keyword escaping.
221    #[serde(default)]
222    pub rename_fields: HashMap<String, String>,
223    /// Prefix wrapper for default tool invocations. When set, prepends this string to default
224    /// commands across all pipelines (lint, test, build, etc.).
225    #[serde(default)]
226    pub run_wrapper: Option<String>,
227    /// Extra paths to append to default lint commands (format, check, typecheck).
228    #[serde(default)]
229    pub extra_lint_paths: Vec<String>,
230}
231
232#[derive(Debug, Clone, Serialize, Deserialize)]
233pub struct RubyConfig {
234    pub gem_name: Option<String>,
235    pub stubs: Option<StubsConfig>,
236    /// Per-language feature override. When set, these features are used instead of
237    /// `[crate] features` for this language's binding crate.
238    #[serde(default)]
239    pub features: Option<Vec<String>>,
240    /// Override the serde rename_all strategy for JSON field names (e.g. "camelCase", "snake_case").
241    /// When set, this takes priority over the IR type-level serde_rename_all.
242    #[serde(default)]
243    pub serde_rename_all: Option<String>,
244    /// Functions to exclude from Ruby binding generation.
245    #[serde(default)]
246    pub exclude_functions: Vec<String>,
247    /// Types to exclude from Ruby binding generation.
248    #[serde(default)]
249    pub exclude_types: Vec<String>,
250    /// Additional Cargo dependencies for this language's binding crate only.
251    #[serde(default)]
252    pub extra_dependencies: HashMap<String, toml::Value>,
253    /// Override the scaffold output directory for this language's Cargo.toml and package files.
254    #[serde(default)]
255    pub scaffold_output: Option<PathBuf>,
256    /// Per-field name remapping for this language. Key is `TypeName.field_name`, value is the
257    /// desired binding field name. Applied after automatic keyword escaping.
258    #[serde(default)]
259    pub rename_fields: HashMap<String, String>,
260    /// Prefix wrapper for default tool invocations. When set, prepends this string to default
261    /// commands across all pipelines (lint, test, build, etc.).
262    #[serde(default)]
263    pub run_wrapper: Option<String>,
264    /// Extra paths to append to default lint commands (format, check, typecheck).
265    #[serde(default)]
266    pub extra_lint_paths: Vec<String>,
267}
268
269#[derive(Debug, Clone, Serialize, Deserialize)]
270pub struct PhpConfig {
271    pub extension_name: Option<String>,
272    /// Cargo crate name for the PHP binding (e.g. `"ts-pack-core-php"`).
273    /// Used to derive the shared library filename in the e2e test runner.
274    /// When absent, the lib name is derived from `extension_name` by appending `_php`.
275    #[serde(default)]
276    pub cargo_crate_name: Option<String>,
277    /// Override the PHP namespace used for class registration and PSR-4 autoloading.
278    ///
279    /// When set, this value is used verbatim as the PHP namespace (e.g. `"HtmlToMarkdown"`).
280    /// When absent, the namespace is derived from `extension_name` by splitting on `_` and
281    /// converting each segment to PascalCase (e.g. `html_to_markdown` → `Html\To\Markdown`).
282    #[serde(default)]
283    pub namespace: Option<String>,
284    /// Feature gate for ext-php-rs (default: "extension-module").
285    /// All generated code is wrapped in `#[cfg(feature = "...")]`.
286    #[serde(default)]
287    pub feature_gate: Option<String>,
288    /// Output directory for generated PHP facade / stubs (e.g., `packages/php/src/`).
289    #[serde(default)]
290    pub stubs: Option<StubsConfig>,
291    #[serde(default)]
292    pub features: Option<Vec<String>>,
293    /// Override the serde rename_all strategy for JSON field names (e.g. "camelCase", "snake_case").
294    /// When set, this takes priority over the IR type-level serde_rename_all.
295    #[serde(default)]
296    pub serde_rename_all: Option<String>,
297    /// Functions to exclude from PHP binding generation.
298    #[serde(default)]
299    pub exclude_functions: Vec<String>,
300    /// Types to exclude from PHP binding generation.
301    #[serde(default)]
302    pub exclude_types: Vec<String>,
303    /// Additional Cargo dependencies for this language's binding crate only.
304    #[serde(default)]
305    pub extra_dependencies: HashMap<String, toml::Value>,
306    /// Override the scaffold output directory for this language's Cargo.toml and package files.
307    #[serde(default)]
308    pub scaffold_output: Option<PathBuf>,
309    /// Per-field name remapping for this language. Key is `TypeName.field_name`, value is the
310    /// desired binding field name. Applied after automatic keyword escaping.
311    #[serde(default)]
312    pub rename_fields: HashMap<String, String>,
313    /// Prefix wrapper for default tool invocations. When set, prepends this string to default
314    /// commands across all pipelines (lint, test, build, etc.).
315    #[serde(default)]
316    pub run_wrapper: Option<String>,
317    /// Extra paths to append to default lint commands (format, check, typecheck).
318    #[serde(default)]
319    pub extra_lint_paths: Vec<String>,
320}
321
322#[derive(Debug, Clone, Serialize, Deserialize)]
323pub struct ElixirConfig {
324    pub app_name: Option<String>,
325    #[serde(default)]
326    pub features: Option<Vec<String>>,
327    /// Override the serde rename_all strategy for JSON field names (e.g. "camelCase", "snake_case").
328    /// When set, this takes priority over the IR type-level serde_rename_all.
329    #[serde(default)]
330    pub serde_rename_all: Option<String>,
331    /// Functions to exclude from Elixir NIF generation.
332    #[serde(default)]
333    pub exclude_functions: Vec<String>,
334    /// Types to exclude from Elixir NIF generation.
335    #[serde(default)]
336    pub exclude_types: Vec<String>,
337    /// Additional Cargo dependencies for this language's binding crate only.
338    #[serde(default)]
339    pub extra_dependencies: HashMap<String, toml::Value>,
340    /// Override the scaffold output directory for this language's Cargo.toml and package files.
341    #[serde(default)]
342    pub scaffold_output: Option<PathBuf>,
343    /// Per-field name remapping for this language. Key is `TypeName.field_name`, value is the
344    /// desired binding field name. Applied after automatic keyword escaping.
345    #[serde(default)]
346    pub rename_fields: HashMap<String, String>,
347    /// Prefix wrapper for default tool invocations. When set, prepends this string to default
348    /// commands across all pipelines (lint, test, build, etc.).
349    #[serde(default)]
350    pub run_wrapper: Option<String>,
351    /// Extra paths to append to default lint commands (format, check, typecheck).
352    #[serde(default)]
353    pub extra_lint_paths: Vec<String>,
354    /// Functions that should be scheduled on the dirty CPU scheduler.
355    /// HTML parsing and other CPU-intensive NIFs should be listed here to avoid
356    /// blocking BEAM scheduler threads.
357    #[serde(default)]
358    pub cpu_bound_functions: Vec<String>,
359}
360
361#[derive(Debug, Clone, Serialize, Deserialize)]
362pub struct WasmConfig {
363    #[serde(default)]
364    pub exclude_functions: Vec<String>,
365    #[serde(default)]
366    pub exclude_types: Vec<String>,
367    #[serde(default)]
368    pub type_overrides: HashMap<String, String>,
369    #[serde(default)]
370    pub features: Option<Vec<String>>,
371    /// Override the serde rename_all strategy for JSON field names (e.g. "camelCase", "snake_case").
372    /// When set, this takes priority over the IR type-level serde_rename_all.
373    #[serde(default)]
374    pub serde_rename_all: Option<String>,
375    /// Prefix for generated type names (e.g. "Wasm" produces `WasmConversionOptions`).
376    /// Defaults to `"Wasm"`.
377    #[serde(default)]
378    pub type_prefix: Option<String>,
379    /// Functions to exclude from the public TypeScript re-export (index.ts) while still
380    /// generating the Rust binding. Use this when a custom module provides a wrapper.
381    #[serde(default)]
382    pub exclude_reexports: Vec<String>,
383    /// Wide-character C functions to shim for WASM external scanner interop.
384    #[serde(default)]
385    pub env_shims: Vec<String>,
386    /// Additional Cargo dependencies for the WASM binding crate only.
387    #[serde(default)]
388    pub extra_dependencies: HashMap<String, toml::Value>,
389    /// Per-field name remapping for this language. Key is `TypeName.field_name`, value is the
390    /// desired binding field name. Applied after automatic keyword escaping.
391    #[serde(default)]
392    pub rename_fields: HashMap<String, String>,
393    /// Prefix wrapper for default tool invocations. When set, prepends this string to default
394    /// commands across all pipelines (lint, test, build, etc.).
395    #[serde(default)]
396    pub run_wrapper: Option<String>,
397    /// Extra paths to append to default lint commands (format, check, typecheck).
398    #[serde(default)]
399    pub extra_lint_paths: Vec<String>,
400    /// Override the core Cargo dependency name and path for the WASM binding crate.
401    /// When set, the binding `Cargo.toml` depends on this crate (resolved as
402    /// `../<override>`) instead of the umbrella `[crate.name]`. Use this to point
403    /// the WASM binding at a wasm-safe sub-crate while other languages keep the
404    /// facade. Defaults to unset.
405    #[serde(default)]
406    pub core_crate_override: Option<String>,
407    /// Keys to subtract from the merged `extra_dependencies` set for this
408    /// language only. Useful when `[crate.extra_dependencies]` lists sibling
409    /// crates that the WASM target cannot link.
410    #[serde(default)]
411    pub exclude_extra_dependencies: Vec<String>,
412    /// Hand-written Rust modules to declare in the generated lib.rs with `pub mod <name>;`
413    /// and re-export with `pub use <name>::*;`. Separate from `[custom_modules].wasm` which
414    /// only adds TypeScript `export *` re-exports. Use this for Rust-side dispatch/glue modules.
415    #[serde(default)]
416    pub custom_rust_modules: Vec<String>,
417    /// Per-type field exclusions for the generated From impls and binding struct.
418    /// Key is the type name (e.g. "ServerConfig"), value is a list of field names to skip.
419    /// Use when source fields are gated behind `#[cfg(not(target_arch = "wasm32"))]` and
420    /// therefore don't exist in the wasm32 compilation environment.
421    #[serde(default)]
422    pub exclude_fields: HashMap<String, Vec<String>>,
423    /// Source crate names whose types are re-exported by the `core_crate_override`
424    /// crate. References to `<original_crate>::TypeName` in generated code are
425    /// rewritten to `<override_crate>::TypeName`. Only meaningful when
426    /// `core_crate_override` is set.
427    /// Example: with `core_crate_override = "mylib-http"`, setting
428    /// `source_crate_remaps = ["mylib-core", "mylib"]` rewrites
429    /// `mylib_core::Method` and `mylib::Method` references to
430    /// `mylib_http::Method` (assumes `mylib-http` re-exports them via
431    /// `pub use mylib_core::*`).
432    #[serde(default)]
433    pub source_crate_remaps: Vec<String>,
434}
435
436#[derive(Debug, Clone, Serialize, Deserialize)]
437pub struct FfiConfig {
438    pub prefix: Option<String>,
439    #[serde(default = "default_error_style")]
440    pub error_style: String,
441    pub header_name: Option<String>,
442    /// Native library name for Go cgo/Java Panama/C# P/Invoke (e.g., "ts_pack_ffi").
443    /// Defaults to `{prefix}_ffi`.
444    #[serde(default)]
445    pub lib_name: Option<String>,
446    /// If true, generate visitor/callback FFI support.
447    #[serde(default)]
448    pub visitor_callbacks: bool,
449    #[serde(default)]
450    pub features: Option<Vec<String>>,
451    /// Override the serde rename_all strategy for JSON field names (e.g. "camelCase", "snake_case").
452    /// When set, this takes priority over the IR type-level serde_rename_all.
453    #[serde(default)]
454    pub serde_rename_all: Option<String>,
455    /// Functions to exclude from FFI binding generation.
456    #[serde(default)]
457    pub exclude_functions: Vec<String>,
458    /// Types to exclude from FFI binding generation.
459    #[serde(default)]
460    pub exclude_types: Vec<String>,
461    /// Per-field name remapping for this language. Key is `TypeName.field_name`, value is the
462    /// desired binding field name. Applied after automatic keyword escaping.
463    #[serde(default)]
464    pub rename_fields: HashMap<String, String>,
465    /// Rust expression used to construct an error value of this crate's
466    /// `error_type` from a runtime `String` message inside generated FFI
467    /// trait-bridge plugin shims (`plugin_impl_initialize`, `plugin_impl_shutdown`).
468    ///
469    /// The expression has access to a local variable `msg: String` containing
470    /// the underlying error message and is interpolated verbatim. Example
471    /// values:
472    ///
473    /// ```toml
474    /// # downstream whose error type has a struct variant with two fields:
475    /// plugin_error_constructor = """
476    /// kreuzberg::KreuzbergError::Plugin { message: msg, plugin_name: String::new() }
477    /// """
478    ///
479    /// # downstream whose error type implements `From<String>`:
480    /// plugin_error_constructor = "MyError::from(msg)"
481    /// ```
482    ///
483    /// Defaults to `None`. When unset, the plugin shim still emits — backends
484    /// fall back to a `format!("{}: {}", prefix, msg)`-style construction via
485    /// the configured `error_constructor`. Downstreams that don't expose
486    /// trait-bridged plugins can ignore this knob entirely.
487    #[serde(default)]
488    pub plugin_error_constructor: Option<String>,
489}
490
491fn default_error_style() -> String {
492    "last_error".to_string()
493}
494
495#[derive(Debug, Clone, Serialize, Deserialize)]
496pub struct GoConfig {
497    pub module: Option<String>,
498    /// Override the Go package name (default: derived from module path)
499    pub package_name: Option<String>,
500    #[serde(default)]
501    pub features: Option<Vec<String>>,
502    /// Override the serde rename_all strategy for JSON field names (e.g. "camelCase", "snake_case").
503    /// When set, this takes priority over the IR type-level serde_rename_all.
504    #[serde(default)]
505    pub serde_rename_all: Option<String>,
506    /// Per-field name remapping for this language. Key is `TypeName.field_name`, value is the
507    /// desired binding field name. Applied after automatic keyword escaping.
508    #[serde(default)]
509    pub rename_fields: HashMap<String, String>,
510    /// Prefix wrapper for default tool invocations. When set, prepends this string to default
511    /// commands across all pipelines (lint, test, build, etc.).
512    #[serde(default)]
513    pub run_wrapper: Option<String>,
514    /// Extra paths to append to default lint commands (format, check, typecheck).
515    #[serde(default)]
516    pub extra_lint_paths: Vec<String>,
517}
518
519#[derive(Debug, Clone, Serialize, Deserialize)]
520pub struct JavaConfig {
521    pub package: Option<String>,
522    /// Override the Maven `<groupId>` emitted by alef-scaffold and alef-e2e. When unset,
523    /// `java_group_id()` falls back to the Java `package` value. Set this when the
524    /// published Maven coords differ from the Java package path (e.g. group
525    /// `dev.kreuzberg`, package `dev.kreuzberg.htmltomarkdown`).
526    #[serde(default)]
527    pub group_id: Option<String>,
528    /// Override the Maven `<artifactId>` emitted by alef-scaffold and alef-e2e. When
529    /// unset, defaults to the crate name (the `[[crates]] name = "..."`). Set this when
530    /// the published artifactId differs from the source crate name (e.g. crate
531    /// `html-to-markdown-rs` published as `html-to-markdown`).
532    #[serde(default)]
533    pub artifact_id: Option<String>,
534    #[serde(default = "default_java_ffi_style")]
535    pub ffi_style: String,
536    #[serde(default)]
537    pub features: Option<Vec<String>>,
538    /// Override the serde rename_all strategy for JSON field names (e.g. "camelCase", "snake_case").
539    /// When set, this takes priority over the IR type-level serde_rename_all.
540    #[serde(default)]
541    pub serde_rename_all: Option<String>,
542    /// Per-field name remapping for this language. Key is `TypeName.field_name`, value is the
543    /// desired binding field name. Applied after automatic keyword escaping.
544    #[serde(default)]
545    pub rename_fields: HashMap<String, String>,
546    /// Prefix wrapper for default tool invocations. When set, prepends this string to default
547    /// commands across all pipelines (lint, test, build, etc.).
548    #[serde(default)]
549    pub run_wrapper: Option<String>,
550    /// Extra paths to append to default lint commands (format, check, typecheck).
551    /// Ignored when project_file is set.
552    #[serde(default)]
553    pub extra_lint_paths: Vec<String>,
554    /// Project file for Maven/Gradle (e.g., "pom.xml", "build.gradle"). When set, default
555    /// lint/build/test commands target this file instead of the output directory.
556    #[serde(default)]
557    pub project_file: Option<String>,
558}
559
560fn default_java_ffi_style() -> String {
561    "panama".to_string()
562}
563
564/// Target platform for Kotlin code generation.
565///
566/// - `"jvm"` (default): emits source consuming the Java/Panama FFM facade.
567/// - `"native"`: emits Kotlin/Native source consuming the cbindgen C FFI library.
568/// - `"multiplatform"`: reserved for the KMP stage (Phase 3 follow-up).
569#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
570#[serde(rename_all = "lowercase")]
571pub enum KotlinTarget {
572    #[default]
573    Jvm,
574    Native,
575    // Multiplatform — Phase 3 KMP stage; placeholder so the enum is forward-compatible.
576    Multiplatform,
577}
578
579#[derive(Debug, Clone, Serialize, Deserialize)]
580pub struct KotlinConfig {
581    pub package: Option<String>,
582    #[serde(default)]
583    pub features: Option<Vec<String>>,
584    /// Override the serde rename_all strategy for JSON field names (e.g. "camelCase", "snake_case").
585    /// When set, this takes priority over the IR type-level serde_rename_all.
586    #[serde(default)]
587    pub serde_rename_all: Option<String>,
588    /// Per-field name remapping for this language. Key is `TypeName.field_name`, value is the
589    /// desired binding field name. Applied after automatic keyword escaping.
590    #[serde(default)]
591    pub rename_fields: HashMap<String, String>,
592    /// Functions to exclude from Kotlin binding generation.
593    #[serde(default)]
594    pub exclude_functions: Vec<String>,
595    /// Types to exclude from Kotlin binding generation.
596    #[serde(default)]
597    pub exclude_types: Vec<String>,
598    /// Prefix wrapper for default tool invocations. When set, prepends this string to default
599    /// commands across all pipelines (lint, test, build, etc.).
600    #[serde(default)]
601    pub run_wrapper: Option<String>,
602    /// Extra paths to append to default lint commands (format, check, typecheck).
603    #[serde(default)]
604    pub extra_lint_paths: Vec<String>,
605    /// Target platform for Kotlin output. `"jvm"` (default) emits source consuming
606    /// the Java/Panama FFM facade; `"native"` emits Kotlin/Native source consuming
607    /// the cbindgen C FFI library. `"multiplatform"` is reserved for the KMP stage.
608    #[serde(default)]
609    pub target: KotlinTarget,
610    /// Emission mode controlling which Kotlin project layout is generated.
611    ///
612    /// Accepted values:
613    /// - `"jvm"` (default) — standard JVM-only project under `packages/kotlin/`
614    /// - `"kmp"` — Kotlin Multiplatform project under `packages/kotlin-mpp/`
615    /// - `"android"` — Android library project under `packages/kotlin-android/`
616    ///
617    /// When `None`, defaults to `"jvm"`.
618    #[serde(default)]
619    pub mode: Option<String>,
620}
621
622/// Dart bridging style: FRB (default) or raw `dart:ffi`.
623#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
624#[serde(rename_all = "lowercase")]
625pub enum DartStyle {
626    /// flutter_rust_bridge — emits a Rust crate plus Dart wrappers using
627    /// FRB-generated bridge symbols. Default.
628    #[default]
629    Frb,
630    /// Raw `dart:ffi` over the cbindgen C ABI — emits Dart-only source that
631    /// loads the shared library at runtime. Cheaper to ship; loses FRB's
632    /// async ergonomics and freezed-style data classes.
633    Ffi,
634}
635
636#[derive(Debug, Clone, Default, Serialize, Deserialize)]
637pub struct DartConfig {
638    /// Dart pub.dev package name (e.g. `"my_package"`). Used as the `name` in
639    /// `pubspec.yaml`. Defaults to a snake_case derivation of the crate name.
640    #[serde(default)]
641    pub pubspec_name: Option<String>,
642    /// Dart library name (the `library` declaration). Defaults to the pubspec name.
643    #[serde(default)]
644    pub lib_name: Option<String>,
645    /// Dart package name override (e.g. for pub.dev scoped packages).
646    #[serde(default)]
647    pub package_name: Option<String>,
648    /// Bridging style. `"frb"` (default) uses flutter_rust_bridge; `"ffi"` emits
649    /// raw `dart:ffi` source over the cbindgen C library.
650    #[serde(default)]
651    pub style: DartStyle,
652    /// flutter_rust_bridge version to pin in generated pubspec.yaml.
653    /// Defaults to `template_versions::cargo::FLUTTER_RUST_BRIDGE` when unset.
654    #[serde(default)]
655    pub frb_version: Option<String>,
656    /// Cargo features to enable on the binding crate.
657    #[serde(default)]
658    pub features: Option<Vec<String>>,
659    /// Override the serde rename_all strategy for JSON field names (e.g. "camelCase", "snake_case").
660    #[serde(default)]
661    pub serde_rename_all: Option<String>,
662    /// Per-field name remapping. Key is `TypeName.field_name`, value is the
663    /// desired binding field name. Applied after automatic keyword escaping.
664    #[serde(default)]
665    pub rename_fields: HashMap<String, String>,
666    /// Functions to exclude from Dart binding generation.
667    #[serde(default)]
668    pub exclude_functions: Vec<String>,
669    /// Types to exclude from Dart binding generation.
670    #[serde(default)]
671    pub exclude_types: Vec<String>,
672    /// Prefix wrapper for default tool invocations.
673    #[serde(default)]
674    pub run_wrapper: Option<String>,
675    /// Extra paths to append to default lint commands.
676    #[serde(default)]
677    pub extra_lint_paths: Vec<String>,
678    /// Override the core Cargo dependency name and path for the Dart binding crate.
679    /// When set, the binding `Cargo.toml` depends on this crate (resolved as
680    /// `../../../crates/<override>`) instead of the umbrella `[crate.name]`.
681    /// Defaults to unset.
682    #[serde(default)]
683    pub core_crate_override: Option<String>,
684    /// Keys to subtract from the merged `extra_dependencies` set for this
685    /// language only.
686    #[serde(default)]
687    pub exclude_extra_dependencies: Vec<String>,
688    /// Method names whose Rust bridge body should be emitted as `unimplemented!()`.
689    ///
690    /// Use this when a function's FFI signature (e.g. nested tuples containing
691    /// `Vec<u8>`) cannot be represented across the FRB bridge at all. Consumers must
692    /// list the method names explicitly — this field has no built-in defaults so the
693    /// knob is library-agnostic.
694    ///
695    /// Example (`alef.toml`):
696    /// ```toml
697    /// [crates.dart]
698    /// stub_methods = ["batch_extract_bytes", "batch_extract_bytes_sync"]
699    /// ```
700    #[serde(default)]
701    pub stub_methods: Vec<String>,
702}
703
704#[derive(Debug, Clone, Default, Serialize, Deserialize)]
705pub struct SwiftConfig {
706    /// Swift module name (e.g. `"MyLibrary"`). Defaults to PascalCase of the crate name.
707    #[serde(default)]
708    pub module_name: Option<String>,
709    /// Swift package name. Defaults to the module name.
710    #[serde(default)]
711    pub package_name: Option<String>,
712    /// swift-bridge version. Defaults to `template_versions::cargo::SWIFT_BRIDGE` when unset.
713    #[serde(default)]
714    pub swift_bridge_version: Option<String>,
715    /// Minimum macOS deployment target. Defaults to `template_versions::toolchain::SWIFT_MIN_MACOS` when unset.
716    #[serde(default)]
717    pub min_macos_version: Option<String>,
718    /// Minimum iOS deployment target. Defaults to `template_versions::toolchain::SWIFT_MIN_IOS` when unset.
719    #[serde(default)]
720    pub min_ios_version: Option<String>,
721    /// Cargo features to enable on the binding crate.
722    #[serde(default)]
723    pub features: Option<Vec<String>>,
724    /// Override the serde rename_all strategy for JSON field names (e.g. "camelCase", "snake_case").
725    #[serde(default)]
726    pub serde_rename_all: Option<String>,
727    /// Per-field name remapping. Key is `TypeName.field_name`, value is the
728    /// desired binding field name. Applied after automatic keyword escaping.
729    #[serde(default)]
730    pub rename_fields: HashMap<String, String>,
731    /// Functions to exclude from Swift binding generation.
732    #[serde(default)]
733    pub exclude_functions: Vec<String>,
734    /// Types to exclude from Swift binding generation.
735    #[serde(default)]
736    pub exclude_types: Vec<String>,
737    /// Fields to exclude from Swift binding generation.
738    /// Format: `"TypeName.field_name"`.
739    #[serde(default)]
740    pub exclude_fields: Vec<String>,
741    /// Prefix wrapper for default tool invocations.
742    #[serde(default)]
743    pub run_wrapper: Option<String>,
744    /// Extra paths to append to default lint commands.
745    #[serde(default)]
746    pub extra_lint_paths: Vec<String>,
747    /// Override the core Cargo dependency name and path for the Swift binding crate.
748    /// When set, the binding `Cargo.toml` depends on this crate (resolved as
749    /// `../../../crates/<override>`) instead of the umbrella `[crate.name]`.
750    /// Defaults to unset.
751    #[serde(default)]
752    pub core_crate_override: Option<String>,
753    /// Keys to subtract from the merged `extra_dependencies` set for this
754    /// language only.
755    #[serde(default)]
756    pub exclude_extra_dependencies: Vec<String>,
757    /// Override the auto-generated `create_<type>(api_key, base_url)` constructor
758    /// body for opaque client types that expose methods. When set, the swift backend
759    /// emits this snippet verbatim as the function body (no implicit `Ok(...)`).
760    ///
761    /// Use this when the source crate's constructor signature differs from the
762    /// default `Type::new(api_key, base_url)` shape — e.g. liter-llm uses
763    /// `DefaultClient::new(ClientConfig, Option<&str>)` and needs to build a
764    /// `ClientConfig` from the bridge inputs first.
765    ///
766    /// The snippet is parameterised by `{type_name}` (the wrapper newtype name)
767    /// and runs in a function body with `api_key: String` and `base_url: Option<String>`
768    /// already in scope. It must return `Result<{type_name}, String>`.
769    #[serde(default)]
770    pub client_constructor_body: HashMap<String, String>,
771}
772
773#[derive(Debug, Clone, Serialize, Deserialize)]
774pub struct ZigConfig {
775    pub module_name: Option<String>,
776    #[serde(default)]
777    pub features: Option<Vec<String>>,
778    /// Override the serde rename_all strategy for JSON field names (e.g. "camelCase", "snake_case").
779    /// When set, this takes priority over the IR type-level serde_rename_all.
780    #[serde(default)]
781    pub serde_rename_all: Option<String>,
782    /// Per-field name remapping for this language. Key is `TypeName.field_name`, value is the
783    /// desired binding field name. Applied after automatic keyword escaping.
784    #[serde(default)]
785    pub rename_fields: HashMap<String, String>,
786    /// Functions to exclude from Zig binding generation.
787    #[serde(default)]
788    pub exclude_functions: Vec<String>,
789    /// Types to exclude from Zig binding generation.
790    #[serde(default)]
791    pub exclude_types: Vec<String>,
792    /// Prefix wrapper for default tool invocations.
793    #[serde(default)]
794    pub run_wrapper: Option<String>,
795    /// Extra paths to append to default lint commands.
796    #[serde(default)]
797    pub extra_lint_paths: Vec<String>,
798}
799
800#[derive(Debug, Clone, Serialize, Deserialize)]
801pub struct CSharpConfig {
802    pub namespace: Option<String>,
803    /// NuGet `<PackageId>` to publish under. When unset, falls back to `namespace`.
804    /// Use this when the published artifact id must differ from the C# `RootNamespace` —
805    /// e.g. when the unprefixed name is owned by a third party on nuget.org and
806    /// you publish under a vendor-prefixed id like `KreuzbergDev.<Lib>`.
807    #[serde(default)]
808    pub package_id: Option<String>,
809    pub target_framework: Option<String>,
810    #[serde(default)]
811    pub features: Option<Vec<String>>,
812    /// Override the serde rename_all strategy for JSON field names (e.g. "camelCase", "snake_case").
813    /// When set, this takes priority over the IR type-level serde_rename_all.
814    #[serde(default)]
815    pub serde_rename_all: Option<String>,
816    /// Per-field name remapping for this language. Key is `TypeName.field_name`, value is the
817    /// desired binding field name. Applied after automatic keyword escaping.
818    #[serde(default)]
819    pub rename_fields: HashMap<String, String>,
820    /// Prefix wrapper for default tool invocations. When set, prepends this string to default
821    /// commands across all pipelines (lint, test, build, etc.).
822    #[serde(default)]
823    pub run_wrapper: Option<String>,
824    /// Extra paths to append to default lint commands (format, check, typecheck).
825    /// Ignored when project_file is set.
826    #[serde(default)]
827    pub extra_lint_paths: Vec<String>,
828    /// Project file for C# (e.g., "MyProject.csproj", "MySolution.sln"). When set, default
829    /// lint/build/test commands target this file instead of the output directory.
830    #[serde(default)]
831    pub project_file: Option<String>,
832    /// Functions to exclude from C# binding generation (e.g., functions not present in the
833    /// C FFI layer). Excluded functions are omitted from both NativeMethods.cs and the
834    /// wrapper class.
835    #[serde(default)]
836    pub exclude_functions: Vec<String>,
837}
838
839#[derive(Debug, Clone, Serialize, Deserialize)]
840pub struct RConfig {
841    pub package_name: Option<String>,
842    #[serde(default)]
843    pub features: Option<Vec<String>>,
844    /// Override the serde rename_all strategy for JSON field names (e.g. "camelCase", "snake_case").
845    /// When set, this takes priority over the IR type-level serde_rename_all.
846    #[serde(default)]
847    pub serde_rename_all: Option<String>,
848    /// Per-field name remapping for this language. Key is `TypeName.field_name`, value is the
849    /// desired binding field name. Applied after automatic keyword escaping.
850    #[serde(default)]
851    pub rename_fields: HashMap<String, String>,
852    /// Prefix wrapper for default tool invocations. When set, prepends this string to default
853    /// commands across all pipelines (lint, test, build, etc.).
854    #[serde(default)]
855    pub run_wrapper: Option<String>,
856    /// Extra paths to append to default lint commands (format, check, typecheck).
857    #[serde(default)]
858    pub extra_lint_paths: Vec<String>,
859}
860
861/// Custom modules that alef should declare (mod X;) but not generate.
862/// These are hand-written modules imported by the generated lib.rs.
863#[derive(Debug, Clone, Default, Serialize, Deserialize)]
864pub struct CustomModulesConfig {
865    #[serde(default)]
866    pub python: Vec<String>,
867    #[serde(default)]
868    pub node: Vec<String>,
869    #[serde(default)]
870    pub ruby: Vec<String>,
871    #[serde(default)]
872    pub php: Vec<String>,
873    #[serde(default)]
874    pub elixir: Vec<String>,
875    #[serde(default)]
876    pub wasm: Vec<String>,
877    #[serde(default)]
878    pub ffi: Vec<String>,
879    #[serde(default)]
880    pub go: Vec<String>,
881    #[serde(default)]
882    pub java: Vec<String>,
883    #[serde(default)]
884    pub csharp: Vec<String>,
885    #[serde(default)]
886    pub r: Vec<String>,
887}
888
889impl CustomModulesConfig {
890    pub fn for_language(&self, lang: Language) -> &[String] {
891        match lang {
892            Language::Python => &self.python,
893            Language::Node => &self.node,
894            Language::Ruby => &self.ruby,
895            Language::Php => &self.php,
896            Language::Elixir => &self.elixir,
897            Language::Wasm => &self.wasm,
898            Language::Ffi => &self.ffi,
899            Language::Go => &self.go,
900            Language::Java => &self.java,
901            Language::Csharp => &self.csharp,
902            Language::R => &self.r,
903            Language::Rust => &[], // Rust doesn't need custom modules (no binding crate)
904            Language::Kotlin | Language::Swift | Language::Dart | Language::Zig | Language::C => &[],
905        }
906    }
907}
908
909/// Custom classes/functions from hand-written modules to register in module init.
910#[derive(Debug, Clone, Default, Serialize, Deserialize)]
911pub struct CustomRegistration {
912    #[serde(default)]
913    pub classes: Vec<String>,
914    #[serde(default)]
915    pub functions: Vec<String>,
916    #[serde(default)]
917    pub init_calls: Vec<String>,
918}
919
920/// Per-language custom registrations.
921#[derive(Debug, Clone, Default, Serialize, Deserialize)]
922pub struct CustomRegistrationsConfig {
923    #[serde(default)]
924    pub python: Option<CustomRegistration>,
925    #[serde(default)]
926    pub node: Option<CustomRegistration>,
927    #[serde(default)]
928    pub ruby: Option<CustomRegistration>,
929    #[serde(default)]
930    pub php: Option<CustomRegistration>,
931    #[serde(default)]
932    pub elixir: Option<CustomRegistration>,
933    #[serde(default)]
934    pub wasm: Option<CustomRegistration>,
935}
936
937impl CustomRegistrationsConfig {
938    pub fn for_language(&self, lang: Language) -> Option<&CustomRegistration> {
939        match lang {
940            Language::Python => self.python.as_ref(),
941            Language::Node => self.node.as_ref(),
942            Language::Ruby => self.ruby.as_ref(),
943            Language::Php => self.php.as_ref(),
944            Language::Elixir => self.elixir.as_ref(),
945            Language::Wasm => self.wasm.as_ref(),
946            _ => None,
947        }
948    }
949}