Skip to main content

CodegenBackend

Trait CodegenBackend 

Source
pub trait CodegenBackend {
    // Required methods
    fn map_type(&self, capnp_type: &str) -> String;
    fn setter_method(&self, type_name: &str) -> Option<String>;
    fn getter_expr(&self, reader_var: &str, field: &FieldDef) -> String;
    fn default_value(&self, type_name: &str) -> String;
    fn generate_service_file(
        &self,
        service_name: &str,
        schema: &ParsedSchema,
    ) -> String;
    fn runtime_file(&self) -> (String, String);
    fn file_extension(&self) -> &str;

    // Provided method
    fn index_file(
        &self,
        _all_names: &[String],
        _client_names: &[String],
        _module_contents: &[(String, String)],
    ) -> Option<(String, String)> { ... }
}
Expand description

Backend trait for multi-language code generation from Cap’n Proto schemas.

Implementations map Cap’n Proto types and wire format to a target language’s type system, serialization API, and file layout. The language-agnostic generate_all driver calls these methods to produce output files.

Required Methods§

Source

fn map_type(&self, capnp_type: &str) -> String

Map a Cap’n Proto type name to the target language’s type.

E.g., "Text" → "string" for TypeScript, "str" for Python.

Source

fn setter_method(&self, type_name: &str) -> Option<String>

Return the setter method name on a builder for this type, if the runtime supports a direct method call. Returns None for types handled specially (struct pointers, enum ordinals, etc.).

Source

fn getter_expr(&self, reader_var: &str, field: &FieldDef) -> String

Return an expression that reads a field’s value from a reader variable.

Source

fn default_value(&self, type_name: &str) -> String

Return the default value expression for a type (used for optional field coalescing).

Source

fn generate_service_file( &self, service_name: &str, schema: &ParsedSchema, ) -> String

Generate the complete source file content for a service schema.

Source

fn runtime_file(&self) -> (String, String)

Return (filename, content) for the shared runtime file included in all output.

E.g., TypeScript returns ("capnp.ts", <wire-format runtime>).

Source

fn file_extension(&self) -> &str

Return the file extension for generated service files (e.g., "ts", "py").

Provided Methods§

Source

fn index_file( &self, _all_names: &[String], _client_names: &[String], _module_contents: &[(String, String)], ) -> Option<(String, String)>

Return (filename, content) for an optional index/manifest file.

Returns None if this backend does not generate an index file. TypeScript overrides this to produce index.ts with re-exports.

module_contents pairs each schema’s module name with the full source text already written for it, so the barrel generator can detect cross-module export name collisions from the single source of truth (the actual emitted symbols) instead of re-deriving naming rules independently and risking drift.

Implementors§