Skip to main content

alef_core/
ir.rs

1use serde::{Deserialize, Serialize};
2
3/// Indicates the core Rust type wraps the resolved type in a smart pointer or cow.
4/// Used by codegen to generate correct From/Into conversions.
5#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
6pub enum CoreWrapper {
7    #[default]
8    None,
9    /// `Cow<'static, str>` — binding uses String, core needs `.into()`
10    Cow,
11    /// `Arc<T>` — binding unwraps, core wraps with `Arc::new()`
12    Arc,
13    /// `bytes::Bytes` — binding uses `Vec<u8>`, core needs `Bytes::from()`
14    Bytes,
15    /// `Arc<Mutex<T>>` — binding wraps with `Arc::new(Mutex::new())`, methods call `.lock()`
16    ArcMutex,
17}
18
19/// Typed default value for a field, enabling backends to emit language-native defaults.
20#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
21pub enum DefaultValue {
22    BoolLiteral(bool),
23    StringLiteral(String),
24    IntLiteral(i64),
25    FloatLiteral(f64),
26    EnumVariant(String),
27    /// Empty collection or Default::default()
28    Empty,
29    /// None / null
30    None,
31}
32
33/// Complete API surface extracted from a Rust crate's public interface.
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct ApiSurface {
36    pub crate_name: String,
37    pub version: String,
38    pub types: Vec<TypeDef>,
39    pub functions: Vec<FunctionDef>,
40    pub enums: Vec<EnumDef>,
41    pub errors: Vec<ErrorDef>,
42}
43
44/// A public struct exposed to bindings.
45#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct TypeDef {
47    pub name: String,
48    pub rust_path: String,
49    /// Original rust_path before path mapping rewrites. Used for From impl
50    /// targets to avoid orphan rule violations when core_import is a re-export facade.
51    #[serde(default)]
52    pub original_rust_path: String,
53    pub fields: Vec<FieldDef>,
54    pub methods: Vec<MethodDef>,
55    pub is_opaque: bool,
56    pub is_clone: bool,
57    /// True if the type derives `Copy` (or is bitwise-copyable).
58    /// Used by FFI codegen to avoid emitting `.clone()` (which trips clippy::clone_on_copy).
59    #[serde(default)]
60    pub is_copy: bool,
61    pub doc: String,
62    #[serde(default)]
63    pub cfg: Option<String>,
64    /// True if this type was extracted from a trait definition.
65    /// Trait types need `dyn` keyword when used as opaque inner types.
66    #[serde(default)]
67    pub is_trait: bool,
68    /// True if the type implements Default (via derive or manual impl).
69    /// Used by backends like NAPI to make all fields optional with defaults.
70    #[serde(default)]
71    pub has_default: bool,
72    /// True if some fields were stripped due to `#[cfg]` conditions.
73    /// When true, struct literal initializers need `..Default::default()` to fill
74    /// the missing fields that may exist when the core crate is compiled with features.
75    #[serde(default)]
76    pub has_stripped_cfg_fields: bool,
77    /// True if this type appears as a function return type.
78    /// Used to select output DTO style (e.g., TypedDict for Python return types).
79    #[serde(default)]
80    pub is_return_type: bool,
81    /// Serde `rename_all` strategy for this type (e.g., `"camelCase"`, `"snake_case"`).
82    /// Used by Go/Java/C# backends to emit correct JSON tags matching Rust serde config.
83    #[serde(default)]
84    pub serde_rename_all: Option<String>,
85    /// True if the type derives `serde::Serialize` and `serde::Deserialize`.
86    /// Used by FFI backend to gate `from_json`/`to_json` generation — types
87    /// without serde derives cannot be (de)serialized.
88    #[serde(default)]
89    pub has_serde: bool,
90    /// Super-traits of this trait (e.g., `["Plugin"]` for `OcrBackend: Plugin`).
91    /// Only populated when `is_trait` is true. Used by trait bridge codegen
92    /// to determine which super-trait impls to generate.
93    #[serde(default)]
94    pub super_traits: Vec<String>,
95}
96
97/// A field on a public struct.
98#[derive(Debug, Clone, Serialize, Deserialize)]
99pub struct FieldDef {
100    pub name: String,
101    pub ty: TypeRef,
102    pub optional: bool,
103    pub default: Option<String>,
104    pub doc: String,
105    /// True if this field's type was sanitized (e.g., Duration→u64, trait object→String).
106    /// Fields marked sanitized cannot participate in auto-generated From/Into conversions.
107    #[serde(default)]
108    pub sanitized: bool,
109    /// True if the core field type is `Box<T>` (or `Option<Box<T>>`).
110    /// Used by FFI backends to insert proper deref when cloning field values.
111    #[serde(default)]
112    pub is_boxed: bool,
113    /// Fully qualified Rust path for the field's type (e.g. `my_crate::types::OutputFormat`).
114    /// Used by backends to disambiguate types with the same short name.
115    #[serde(default)]
116    pub type_rust_path: Option<String>,
117    /// `#[cfg(...)]` condition string on this field, if any.
118    /// Used by backends to conditionally include fields in struct literals.
119    #[serde(default)]
120    pub cfg: Option<String>,
121    /// Typed default value for language-native default emission.
122    #[serde(default)]
123    pub typed_default: Option<DefaultValue>,
124    /// Core wrapper on this field (Cow, Arc, Bytes). Affects From/Into codegen.
125    #[serde(default)]
126    pub core_wrapper: CoreWrapper,
127    /// Core wrapper on Vec inner elements (e.g., `Vec<Arc<T>>`).
128    #[serde(default)]
129    pub vec_inner_core_wrapper: CoreWrapper,
130    /// Full Rust path of the newtype wrapper that was resolved away for this field,
131    /// e.g. `"my_crate::NodeIndex"` when `NodeIndex(u32)` was resolved to `u32`.
132    /// When set, binding→core codegen must wrap values into the newtype
133    /// (e.g. `my_crate::NodeIndex(val.field)`) and core→binding codegen must unwrap (`.0`).
134    #[serde(default)]
135    pub newtype_wrapper: Option<String>,
136}
137
138/// A method on a public struct.
139#[derive(Debug, Clone, Serialize, Deserialize)]
140pub struct MethodDef {
141    pub name: String,
142    pub params: Vec<ParamDef>,
143    pub return_type: TypeRef,
144    pub is_async: bool,
145    pub is_static: bool,
146    pub error_type: Option<String>,
147    pub doc: String,
148    pub receiver: Option<ReceiverKind>,
149    /// True if any param or return type was sanitized during unknown type resolution.
150    /// Methods with sanitized signatures cannot be auto-delegated.
151    #[serde(default)]
152    pub sanitized: bool,
153    /// Fully qualified trait path if this method comes from a trait impl
154    /// (e.g. "liter_llm::LlmClient"). None for inherent methods.
155    #[serde(default)]
156    pub trait_source: Option<String>,
157    /// True if the core function returns a reference (`&T`, `Option<&T>`, etc.).
158    /// Used by code generators to insert `.clone()` before type conversion.
159    #[serde(default)]
160    pub returns_ref: bool,
161    /// True if the core function returns `Cow<'_, T>` where T is a named type (not str/bytes).
162    /// Used by code generators to emit `.into_owned()` before type conversion.
163    #[serde(default)]
164    pub returns_cow: bool,
165    /// Full Rust path of the newtype wrapper that was resolved away for the return type,
166    /// e.g. `"my_crate::NodeIndex"` when the return type `NodeIndex(u32)` was resolved to `u32`.
167    /// When set, codegen must unwrap the returned newtype value (e.g. `result.0`) before returning.
168    #[serde(default)]
169    pub return_newtype_wrapper: Option<String>,
170    /// True if this method has a default implementation in the trait definition.
171    /// Methods with defaults can be optionally implemented by the foreign object
172    /// in trait bridge codegen.
173    #[serde(default)]
174    pub has_default_impl: bool,
175}
176
177/// How `self` is received.
178#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
179pub enum ReceiverKind {
180    Ref,
181    RefMut,
182    Owned,
183}
184
185/// A free function exposed to bindings.
186#[derive(Debug, Clone, Serialize, Deserialize)]
187pub struct FunctionDef {
188    pub name: String,
189    pub rust_path: String,
190    #[serde(default)]
191    pub original_rust_path: String,
192    pub params: Vec<ParamDef>,
193    pub return_type: TypeRef,
194    pub is_async: bool,
195    pub error_type: Option<String>,
196    pub doc: String,
197    #[serde(default)]
198    pub cfg: Option<String>,
199    /// True if any param or return type was sanitized during unknown type resolution.
200    #[serde(default)]
201    pub sanitized: bool,
202    /// True if the return type was sanitized (Named replaced with String).  When true,
203    /// the binding-side return type is wider than the actual core return — codegen must
204    /// JSON-serialize the core value rather than treating it as the binding type.
205    #[serde(default)]
206    pub return_sanitized: bool,
207    /// True if the core function returns a reference (`&T`, `Option<&T>`, etc.).
208    /// Used by code generators to insert `.clone()` before type conversion.
209    #[serde(default)]
210    pub returns_ref: bool,
211    /// True if the core function returns `Cow<'_, T>` where T is a named type (not str/bytes).
212    /// Used by code generators to emit `.into_owned()` before type conversion.
213    #[serde(default)]
214    pub returns_cow: bool,
215    /// Full Rust path of the newtype wrapper that was resolved away for the return type.
216    /// When set, codegen must unwrap the returned newtype value (e.g. `result.0`).
217    #[serde(default)]
218    pub return_newtype_wrapper: Option<String>,
219}
220
221/// A function/method parameter.
222#[derive(Debug, Clone, Serialize, Deserialize)]
223pub struct ParamDef {
224    pub name: String,
225    pub ty: TypeRef,
226    pub optional: bool,
227    pub default: Option<String>,
228    /// True if this param's type was sanitized during unknown type resolution.
229    #[serde(default)]
230    pub sanitized: bool,
231    /// Typed default value for language-native default emission.
232    #[serde(default)]
233    pub typed_default: Option<DefaultValue>,
234    /// True if the original Rust parameter was a reference (`&T`).
235    /// Used by codegen to generate owned intermediates and pass refs.
236    #[serde(default)]
237    pub is_ref: bool,
238    /// True if the original Rust parameter was a mutable reference (`&mut T`).
239    /// Used by codegen to generate `&mut` refs when calling core functions.
240    #[serde(default)]
241    pub is_mut: bool,
242    /// Full Rust path of the newtype wrapper that was resolved away for this param,
243    /// e.g. `"my_crate::NodeIndex"` when `NodeIndex(u32)` was resolved to `u32`.
244    /// When set, codegen must wrap the raw value back into the newtype when calling core:
245    /// `my_crate::NodeIndex(param)` instead of just `param`.
246    #[serde(default)]
247    pub newtype_wrapper: Option<String>,
248    /// Original Rust type before sanitization, stored when param.sanitized=true.
249    /// Allows codegen to reconstruct proper deserialization logic.
250    /// E.g. `"Vec<(PathBuf, Option<FileExtractionConfig>)>"` when sanitized to `Vec<String>`.
251    #[serde(default)]
252    pub original_type: Option<String>,
253}
254
255/// A public enum.
256#[derive(Debug, Clone, Serialize, Deserialize)]
257pub struct EnumDef {
258    pub name: String,
259    pub rust_path: String,
260    #[serde(default)]
261    pub original_rust_path: String,
262    pub variants: Vec<EnumVariant>,
263    pub doc: String,
264    #[serde(default)]
265    pub cfg: Option<String>,
266    /// True if the enum derives `Copy`. Only unit-variant enums can derive Copy.
267    /// Used by FFI codegen to avoid emitting `.clone()` (which trips clippy::clone_on_copy).
268    #[serde(default)]
269    pub is_copy: bool,
270    /// True if the enum derives both `serde::Serialize` and `serde::Deserialize`.
271    /// Used by host-language emission (e.g. Swift `Codable`) to gate JSON-bridge conformance.
272    #[serde(default)]
273    pub has_serde: bool,
274    /// Serde tag property name for internally tagged enums (from `#[serde(tag = "...")]`)
275    #[serde(default, skip_serializing_if = "Option::is_none")]
276    pub serde_tag: Option<String>,
277    /// Serde rename strategy for enum variants (from `#[serde(rename_all = "...")]`)
278    #[serde(default, skip_serializing_if = "Option::is_none")]
279    pub serde_rename_all: Option<String>,
280}
281
282/// An enum variant.
283#[derive(Debug, Clone, Serialize, Deserialize)]
284pub struct EnumVariant {
285    pub name: String,
286    pub fields: Vec<FieldDef>,
287    pub doc: String,
288    /// True if this variant has `#[default]` attribute (used by `#[derive(Default)]`).
289    #[serde(default)]
290    pub is_default: bool,
291    /// Explicit serde rename for this variant (from `#[serde(rename = "...")]`).
292    #[serde(default, skip_serializing_if = "Option::is_none")]
293    pub serde_rename: Option<String>,
294    /// True if this is a tuple variant (unnamed fields like `Variant(T1, T2)`).
295    /// False for struct variants with named fields or unit variants.
296    #[serde(default)]
297    pub is_tuple: bool,
298}
299
300/// An error type (enum used in Result<T, E>).
301#[derive(Debug, Clone, Serialize, Deserialize)]
302pub struct ErrorDef {
303    pub name: String,
304    pub rust_path: String,
305    #[serde(default)]
306    pub original_rust_path: String,
307    pub variants: Vec<ErrorVariant>,
308    pub doc: String,
309}
310
311/// An error variant.
312#[derive(Debug, Clone, Serialize, Deserialize)]
313pub struct ErrorVariant {
314    pub name: String,
315    /// The `#[error("...")]` message template string, e.g. `"I/O error: {0}"`.
316    pub message_template: Option<String>,
317    /// Fields on this variant (struct or tuple fields).
318    #[serde(default)]
319    pub fields: Vec<FieldDef>,
320    /// True if any field has `#[source]` or `#[from]`.
321    #[serde(default)]
322    pub has_source: bool,
323    /// True if any field has `#[from]` (auto From conversion).
324    #[serde(default)]
325    pub has_from: bool,
326    /// True if this is a unit variant (no fields).
327    #[serde(default)]
328    pub is_unit: bool,
329    pub doc: String,
330}
331
332/// Reference to a type, with enough info for codegen.
333#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
334pub enum TypeRef {
335    Primitive(PrimitiveType),
336    String,
337    /// Rust `char` — single Unicode character. Binding layer represents as single-char string.
338    Char,
339    Bytes,
340    Optional(Box<TypeRef>),
341    Vec(Box<TypeRef>),
342    Map(Box<TypeRef>, Box<TypeRef>),
343    Named(String),
344    Path,
345    Unit,
346    Json,
347    Duration,
348}
349
350impl TypeRef {
351    /// Returns true if this type reference contains `Named(name)` at any depth.
352    pub fn references_named(&self, name: &str) -> bool {
353        match self {
354            Self::Named(n) => n == name,
355            Self::Optional(inner) | Self::Vec(inner) => inner.references_named(name),
356            Self::Map(k, v) => k.references_named(name) || v.references_named(name),
357            _ => false,
358        }
359    }
360}
361
362/// Rust primitive types.
363#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
364pub enum PrimitiveType {
365    Bool,
366    U8,
367    U16,
368    U32,
369    U64,
370    I8,
371    I16,
372    I32,
373    I64,
374    F32,
375    F64,
376    Usize,
377    Isize,
378}