Skip to main content

alef_codegen/
type_mapper.rs

1use alef_core::ir::{PrimitiveType, TypeRef};
2use std::borrow::Cow;
3
4/// Trait for mapping IR types to language-specific type strings.
5/// Backends implement only what differs from the Rust default.
6pub trait TypeMapper {
7    /// Map a primitive type. Default: Rust type names.
8    fn primitive(&self, prim: &PrimitiveType) -> Cow<'static, str> {
9        Cow::Borrowed(match prim {
10            PrimitiveType::Bool => "bool",
11            PrimitiveType::U8 => "u8",
12            PrimitiveType::U16 => "u16",
13            PrimitiveType::U32 => "u32",
14            PrimitiveType::U64 => "u64",
15            PrimitiveType::I8 => "i8",
16            PrimitiveType::I16 => "i16",
17            PrimitiveType::I32 => "i32",
18            PrimitiveType::I64 => "i64",
19            PrimitiveType::F32 => "f32",
20            PrimitiveType::F64 => "f64",
21            PrimitiveType::Usize => "usize",
22            PrimitiveType::Isize => "isize",
23        })
24    }
25
26    /// Map a string type. Default: "String"
27    fn string(&self) -> Cow<'static, str> {
28        Cow::Borrowed("String")
29    }
30
31    /// Map a bytes type. Default: "Vec<u8>"
32    fn bytes(&self) -> Cow<'static, str> {
33        Cow::Borrowed("Vec<u8>")
34    }
35
36    /// Map a path type. Default: "String"
37    fn path(&self) -> Cow<'static, str> {
38        Cow::Borrowed("String")
39    }
40
41    /// Map a JSON type. Default: "serde_json::Value"
42    fn json(&self) -> Cow<'static, str> {
43        Cow::Borrowed("serde_json::Value")
44    }
45
46    /// Map a unit type. Default: "()"
47    fn unit(&self) -> Cow<'static, str> {
48        Cow::Borrowed("()")
49    }
50
51    /// Map a duration type. Default: "u64" (seconds)
52    fn duration(&self) -> Cow<'static, str> {
53        Cow::Borrowed("u64")
54    }
55
56    /// Map an optional type. Default: "Option<T>"
57    fn optional(&self, inner: &str) -> String {
58        format!("Option<{inner}>")
59    }
60
61    /// Map a vec type. Default: "Vec<T>"
62    fn vec(&self, inner: &str) -> String {
63        format!("Vec<{inner}>")
64    }
65
66    /// Map a map type. Default: "HashMap<K, V>"
67    fn map(&self, key: &str, value: &str) -> String {
68        format!("HashMap<{key}, {value}>")
69    }
70
71    /// Map a named type. Default: identity.
72    fn named<'a>(&self, name: &'a str) -> Cow<'a, str> {
73        Cow::Borrowed(name)
74    }
75
76    /// Map a full TypeRef. Typically not overridden.
77    fn map_type(&self, ty: &TypeRef) -> String {
78        match ty {
79            TypeRef::Primitive(p) => self.primitive(p).into_owned(),
80            TypeRef::String | TypeRef::Char => self.string().into_owned(),
81            TypeRef::Bytes => self.bytes().into_owned(),
82            TypeRef::Path => self.path().into_owned(),
83            TypeRef::Json => self.json().into_owned(),
84            TypeRef::Unit => self.unit().into_owned(),
85            TypeRef::Optional(inner) => self.optional(&self.map_type(inner)),
86            TypeRef::Vec(inner) => self.vec(&self.map_type(inner)),
87            TypeRef::Map(k, v) => self.map(&self.map_type(k), &self.map_type(v)),
88            TypeRef::Named(name) => self.named(name).into_owned(),
89            TypeRef::Duration => self.duration().into_owned(),
90        }
91    }
92
93    /// The error wrapper type for this language. e.g. "PyResult", "napi::Result", "PhpResult"
94    fn error_wrapper(&self) -> &str;
95
96    /// Wrap a return type with error handling if needed.
97    fn wrap_return(&self, base: &str, has_error: bool) -> String {
98        if has_error {
99            format!("{}<{base}>", self.error_wrapper())
100        } else {
101            base.to_string()
102        }
103    }
104}