pub trait TypeMapper {
Show 17 methods
// Required methods
fn map_primitive(&self, ty: &PrimitiveType) -> String;
fn map_option(&self, inner: &TypeKind) -> String;
fn map_vec(&self, inner: &TypeKind) -> String;
fn map_hashmap(&self, key: &TypeKind, value: &TypeKind) -> String;
fn map_tuple(&self, elements: &[TypeKind]) -> String;
fn map_named(&self, name: &str) -> String;
fn emit_struct(&self, def: &StructDef) -> String;
fn emit_enum(&self, def: &EnumDef) -> String;
fn file_header(&self, type_name: &str) -> String;
fn file_extension(&self) -> &str;
fn file_naming(&self, type_name: &str) -> String;
// Provided methods
fn file_footer(&self) -> String { ... }
fn map_generic(&self, name: &str, params: &[TypeKind]) -> String { ... }
fn map_type(&self, ty: &TypeKind) -> String { ... }
fn emit_imports(&self, _def: &TypeDef) -> String { ... }
fn emit_type_def(&self, def: &TypeDef) -> String { ... }
fn output_filename(&self, type_name: &str) -> String { ... }
}Expand description
The core trait that every language emitter must implement.
This trait defines the full contract for converting typewriter’s IR into target language source code.
Required Methods§
Sourcefn map_primitive(&self, ty: &PrimitiveType) -> String
fn map_primitive(&self, ty: &PrimitiveType) -> String
Map a Rust primitive type to the target language’s equivalent.
§Examples
PrimitiveType::String→"string"(TS) or"str"(Python)PrimitiveType::U32→"number"(TS) or"int"(Python)
Sourcefn map_option(&self, inner: &TypeKind) -> String
fn map_option(&self, inner: &TypeKind) -> String
Map an Option<T> type to the target language.
§Examples
Option<String>→"string | undefined"(TS) or"Optional[str]"(Python)
Sourcefn map_vec(&self, inner: &TypeKind) -> String
fn map_vec(&self, inner: &TypeKind) -> String
Map a Vec<T> type to the target language.
§Examples
Vec<String>→"string[]"(TS) or"list[str]"(Python)
Sourcefn map_hashmap(&self, key: &TypeKind, value: &TypeKind) -> String
fn map_hashmap(&self, key: &TypeKind, value: &TypeKind) -> String
Map a HashMap<K, V> type to the target language.
§Examples
HashMap<String, u32>→"Record<string, number>"(TS)
Sourcefn map_tuple(&self, elements: &[TypeKind]) -> String
fn map_tuple(&self, elements: &[TypeKind]) -> String
Map a tuple type (A, B, ...) to the target language.
Sourcefn map_named(&self, name: &str) -> String
fn map_named(&self, name: &str) -> String
Map a named custom type (reference to another struct or enum).
Usually the name is kept as-is in most languages.
Sourcefn emit_struct(&self, def: &StructDef) -> String
fn emit_struct(&self, def: &StructDef) -> String
Render a complete struct as target language source code.
Sourcefn emit_enum(&self, def: &EnumDef) -> String
fn emit_enum(&self, def: &EnumDef) -> String
Render a complete enum as target language source code.
Sourcefn file_header(&self, type_name: &str) -> String
fn file_header(&self, type_name: &str) -> String
File header content (auto-generated comment, import statements, pragmas).
Sourcefn file_extension(&self) -> &str
fn file_extension(&self) -> &str
File extension for the output file (without the leading dot).
Sourcefn file_naming(&self, type_name: &str) -> String
fn file_naming(&self, type_name: &str) -> String
Naming convention for output files.
§Examples
"UserProfile"→"user-profile"(kebab-case for TS)"UserProfile"→"user_profile"(snake_case for Python)
Provided Methods§
File footer content (if needed). Default: empty.
Sourcefn map_generic(&self, name: &str, params: &[TypeKind]) -> String
fn map_generic(&self, name: &str, params: &[TypeKind]) -> String
Map a generic type Name<A, B> to the target language.
§Examples
Pagination<User>→"Pagination<User>"(TS)Pagination<User>→"Pagination[User]"(Python)
Sourcefn map_type(&self, ty: &TypeKind) -> String
fn map_type(&self, ty: &TypeKind) -> String
Map any TypeKind to the target language string.
This has a default implementation that dispatches to the specific mapping methods based on the variant. Override only if you need custom behavior.
Sourcefn emit_imports(&self, _def: &TypeDef) -> String
fn emit_imports(&self, _def: &TypeDef) -> String
Generate import statements for types referenced by this type definition.
Override this in language mappers to produce language-specific imports. Default: no imports.
Sourcefn emit_type_def(&self, def: &TypeDef) -> String
fn emit_type_def(&self, def: &TypeDef) -> String
Emit a complete TypeDef (struct or enum) as target language source code.
Includes the file header, import statements, and the type definition.
Sourcefn output_filename(&self, type_name: &str) -> String
fn output_filename(&self, type_name: &str) -> String
Get the full output filename for a type.