Expand description
Kotlin code generator — a self-contained declaration model + renderer (KotlinPoet-style “spec model with raw code bodies”).
Declarations (files, classes, functions, properties, parameters, types,
annotations) are typed model values; imports and formatting derive from
the model. Statement bodies stay raw Kotlin text structured through
the indentation-aware KtCode builder.
This crate is deliberately dependency-free: it receives model values and strings, and produces source text and file paths. Consumers build the model; this crate renders it.
Structs§
- Diagnostic
- One problem found in a
KtFile. - Import
Set - Per-file import collector. Maps simple name → owning FQN; first registration wins, later distinct FQNs with the same simple name render fully qualified.
- KtClass
- A class / object / enum / data / value-class declaration.
- KtCode
- A sequence of raw lines and nested blocks plus the imports its text references. Rendered with 4-space indentation per nesting level.
- KtCompanion
- A
companion object. - KtCtor
Param - Primary-constructor parameter, optionally a
val/varproperty. - KtEnum
Entry - KtFile
- One Kotlin source file fragment: a package plus top-level declarations.
Fragments of the same package are merged by
super::file::merge_files. - KtFun
- A function declaration (top-level or member).
- KtFun
Interface - A
fun interface(SAM) declaration: exactly one abstract method. - KtFun
Sig - A function signature: everything a
KtFunhas except a body and modifiers. - KtParam
- A function parameter with an optional default-value expression.
- KtProperty
- A property declaration (top-level or member).
- KtSuperclass
- The one superclass a class may construct:
: NativeHandle(initialPtr). - KtSupertypes
- What a class extends and implements.
- Validation
Policy - Per-check severity, so a consumer can downgrade or disable a check instead of pinning an old version of this crate when one misfires.
Enums§
- Check
- Which check produced a
Diagnostic. - KtBody
- A function body — or the reason there isn’t one.
- KtClass
Kind - The kind of class-like declaration, carrying whatever that kind — and only that kind — can hold.
- KtClass
Modifier - The modifier a
classcarries, if any. - KtDecl
- A top-level (or member-level, for
Raw) declaration. - KtProperty
Value - A property’s value: no value, an initializer, or a delegate.
- KtType
- A Kotlin type reference.
- KtVis
- Visibility modifier.
Publicrenders explicitly (matching the existing generated style);Defaultrenders nothing. - Severity
- How much a diagnostic matters. A
Checkset to neither is off. - Write
Kotlin Error - Errors surfaced by Kotlin emission.
Constants§
- KOTLIN_
BANNER - The default first line of every generated file — the “do not edit” marker a reader needs to know the file is machine-written.
- KOTLIN_
HARD_ KEYWORDS - Kotlin’s hard keywords — reserved everywhere, so never usable as a plain
identifier. Soft and modifier keywords (
data,value,inline,operator, …) are contextual and are valid identifiers, so they are deliberately absent.
Functions§
- escape_
kotlin_ ident - Keep a name Kotlin would otherwise reject by wrapping it in back-ticks —
the alternative to
mangle_kotlin_ident, which changes it instead. - is_
escaped_ kotlin_ ident - Whether
sis an already back-ticked identifier — the formescape_kotlin_identproduces, and a legal way to write a name Kotlin would otherwise reject. - is_
kotlin_ hard_ keyword - Whether
sis one ofKOTLIN_HARD_KEYWORDS. - is_
valid_ kotlin_ ident - Whether
sis a legal plain Kotlin identifier: non-empty, first character a letter or_, the rest letters / digits /_, and not a hard keyword. - is_
valid_ kotlin_ package - Whether
sis a legal Kotlin package path: one or more valid identifiers separated by single dots. The empty string is legal — it is the default (root) package. - is_
writable_ kotlin_ ident - Whether
scan be written as a name in Kotlin source: either a plain identifier or an escaped one. - mangle_
kotlin_ ident - Turn any string into a legal Kotlin identifier, deterministically and idempotently:
- mangle_
kotlin_ package - Mangle each segment of a dot-separated package path, dropping empty segments (a leading, trailing or doubled dot). Idempotent.
- merge_
files - Merge fragments into one
KtFileper package (sorted package order; within a package, fragments keep their emission order), then validate each merged file with every check at its default severity. - merge_
files_ with merge_fileswith per-check severities, also returning the diagnostics that did not stop generation.- merged_
file_ path - The flattened on-disk path of one merged file under
kotlin_root:io.zenoh.jni.bytesbecomes<kotlin_root>/io/zenoh/jni/bytes.kt. - write_
files - Render and write every merged file; returns the written paths.
- write_
files_ with write_fileswith per-check severities, also returning the diagnostics that did not stop generation. Nothing is written when any check errors.