rust-source-generator-ksp/
├── processor/ # KSP processor implementation
│ └── src/main/kotlin/dev/nemecec/rustmodel/ksp/
│ ├── TypeMapper.kt # Kotlin → Rust type mapping
│ ├── ProcessorConfig.kt # Configuration handling
│ ├── RustCodeGenerator.kt # Rust code generation logic
│ ├── RustGeneratorProcessor.kt # Main KSP processor
│ └── RustGeneratorProcessorProvider.kt # KSP provider
├── example/ # Example usage
│ └── src/main/kotlin/com/example/models/
│ ├── User.kt # Basic data classes
│ ├── Theme.kt # Enum example
│ ├── Message.kt # Sealed class example
│ └── Product.kt # Collections example
└── README.md # Full documentation
# 1. Build the processor
./gradlew :processor:build
# 2. Generate Rust files from example models
./gradlew :example:kspKotlin
# 3. View generated Rust files
ls -la example/build/generated/rust/
cat example/build/generated/rust/user.rsksp {
arg("rust.output.dir", "${project.buildDir}/rust")
arg("rust.filter.packages", "com.example.models")
}ksp {
arg("rust.filter.files", "User.kt,Product.kt")
}ksp {
arg("rust.process.all", "true")
}- UUID, String, Int, Double, Boolean
- List → Vec
- Set → HashSet
- Map → HashMap
- Nullable types → Option
- Simple enums with @SerialName
- Rust enum with serde rename
- Polymorphic types
- Type discriminator support
- Multiple subclass variants
- camelCase → snake_case for fields
- PascalCase preserved for types
- @SerialName annotation support
After running ./gradlew :example:kspKotlin, you should see:
example/build/generated/rust/
├── user.rs # User + UserPreferences
├── theme.rs # Theme enum
├── message.rs # Message sealed class + variants
└── product.rs # Product + ShoppingCart + CartItem
- Copy the
processor/module to your project - Add KSP plugin to your
build.gradle.kts - Add processor as ksp dependency
- Annotate models with
@Serializable - Configure output directory and filters
- Run
./gradlew kspKotlin
- Check that classes are annotated with
@Serializable - Verify package/file filters match your models
- Check KSP logs for errors
- Verify
rust.output.dirsetting - Check project build directory path
- Review TypeMapper.kt for custom type mappings
- Add custom mappings as needed