Skip to main content

Crate kotlin_codegen

Crate kotlin_codegen 

Source
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.
ImportSet
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.
KtCtorParam
Primary-constructor parameter, optionally a val/var property.
KtEnumEntry
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).
KtFunInterface
A fun interface (SAM) declaration: exactly one abstract method.
KtFunSig
A function signature: everything a KtFun has 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.
ValidationPolicy
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.
KtClassKind
The kind of class-like declaration, carrying whatever that kind — and only that kind — can hold.
KtClassModifier
The modifier a class carries, if any.
KtDecl
A top-level (or member-level, for Raw) declaration.
KtPropertyValue
A property’s value: no value, an initializer, or a delegate.
KtType
A Kotlin type reference.
KtVis
Visibility modifier. Public renders explicitly (matching the existing generated style); Default renders nothing.
Severity
How much a diagnostic matters. A Check set to neither is off.
WriteKotlinError
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 s is an already back-ticked identifier — the form escape_kotlin_ident produces, and a legal way to write a name Kotlin would otherwise reject.
is_kotlin_hard_keyword
Whether s is one of KOTLIN_HARD_KEYWORDS.
is_valid_kotlin_ident
Whether s is 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 s is 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 s can 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 KtFile per 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_files with 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.bytes becomes <kotlin_root>/io/zenoh/jni/bytes.kt.
write_files
Render and write every merged file; returns the written paths.
write_files_with
write_files with per-check severities, also returning the diagnostics that did not stop generation. Nothing is written when any check errors.