kotlin-codegen 0.2.0

A declaration model and renderer for generating Kotlin source code
Documentation
# Changelog

All notable changes to this project are documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.2.0 - 2026-08-06

Breaking: a `0.x` crate signals incompatibility by raising the minor, and much
of the declaration model changed shape. The theme is that Kotlin which does not
compile should be hard to produce — bad shapes are now unbuildable, and what
the types cannot rule out is checked before anything is written.

### Added

- Generation-time validation. `KtFile::validate` / `validate_with` return
  `Diagnostic`s carrying a `Check`, a `Severity` and a scope path locating the
  problem. `merge_files` and `write_files` run it and refuse to write;
  `merge_files_with` / `write_files_with` take a `ValidationPolicy` and return
  the warnings that survived. `ValidationPolicy::warn_all` downgrades every
  check, for adopting validation in a generator that already produces output.
- Kotlin identifier utilities: `is_valid_kotlin_ident`, `mangle_kotlin_ident`,
  `escape_kotlin_ident`, `is_escaped_kotlin_ident`, `is_writable_kotlin_ident`,
  `is_kotlin_hard_keyword`, `KOTLIN_HARD_KEYWORDS`, and the package-path
  equivalents `is_valid_kotlin_package` / `mangle_kotlin_package`.
- Extension functions: `receiver` on `KtFun` and `KtFunSig`, plus
  `KtType::render_receiver` for the parentheses a function-type receiver needs.
- `KtCompanion`, `KtSupertypes`, `KtSuperclass`, `KtFunSig`, `KtClassModifier`,
  and `KtBody::External`.
- `open` and `sealed` classes, and named companion objects, are expressible for
  the first time.
- Kind-specific constructors: `KtClass::class_`, `class_with`, `data`, `value`,
  `enum_`, `object_`, `data_object`, `interface_`, `sealed_interface`.
- `KtFun::signature()`, converting a concrete function to the abstract member
  it would be.
- `KOTLIN_BANNER` and `merged_file_path` are exported. Both were `pub` inside
  private modules, so no consumer could reach them.
- Two runnable examples, `showcase` and `invalid`, pinned by golden files.

### Changed - breaking

- `KtClassKind` variants carry their own data, so primary-constructor
  parameters and enum entries live on the kind rather than on `KtClass`;
  `KtClass::ctor_params()` is now a method. `Plain` and `Abstract` became
  `Class { modifier }`; `Companion` and `ValueInline` are gone.
- A companion object is a `KtCompanion` reached only through
  `KtClass::companion`, which is now `Option<Box<KtCompanion>>`.
  `KtClass::companion_object()` is removed.
- Supertypes are a `KtSupertypes { superclass, interfaces }`;
  `KtClass::supertype()` is replaced by `extends` and `implements`.
- `KtFunInterface::method` is a `KtFunSig`, which cannot carry a body.
- `external` is a `KtBody` variant set by `KtFun::external()`, not a modifier
  string. `KtFun::modifier("external")` panics.
- `KtFun` and `KtFunSig` gained a `receiver` field. Builder callers are
  unaffected; constructing either with a struct literal needs the new field.
- `WriteKotlinError` gained a `Validation` variant, so an exhaustive `match`
  on it must be extended.
- `merge_files` and `write_files` validate, and so can reject a model they
  previously accepted.
- Builders panic on shapes Kotlin rejects rather than rendering them:
  constructor parameters on an `object` or `interface`, a `data class`
  parameter that is not a property, a `value class` field that is not a `val`,
  a second `extends`, an enum entry on a non-enum, and an empty companion name.

### Fixed

- `merge_files` dropped a fragment's `KtFile::banner` override, so a
  per-package banner was lost as soon as anything was merged.
- `class Foo` and `val Foo` in one package were reported as a duplicate.
  Kotlin keeps types and values in separate namespaces and allows both.
- Duplicate detection missed `fun interface`s, functions, and everything
  inside class and companion bodies — where most generated declarations live.
- Merging reported only the first problem, so clearing several took one build
  each. All diagnostics are reported together.
- Identical `Raw` blocks from different fragments are merged rather than
  emitted twice.

## 0.1.0

Initial release.

- Declaration model for Kotlin sources: files, classes, enums, functions,
  functional interfaces, properties, parameters, and constructor parameters.
- Indentation-aware `KtCode` builder for rendering statement and expression
  bodies.
- Renderer that turns the model into formatted Kotlin source, with a
  configurable file banner.
- File writer (`write_files`) with ownership-marker safety and `merge_files`
  helper that validates function-overload uniqueness.
- Import collection and `KtType` modelling with no external dependencies.