typeshare-java
typeshare-java is a CLI tool for converting Rust types into Java types. It is
part of the wider typeshare ecosystem.
Installation
First, install the CLI:
Then, install the annotations:
Feature Support
| Feature | Status | Comment |
|---|---|---|
| Structs | ✅ | - |
| Struct Generics | ✅ | - |
| Unit Enums | ✅ | - |
| Algebraic Enums | 🚧 | Gson only. Anonymous structs not supported yet. |
| Enum Generics | ❌ | - |
| Type Aliases | ❌ | - |
| Constants | ❌ | - |
Usage
CLI
To get started, run:
Annotations
Getting Started
For typeshare-java to generate types from your Rust code, it requires you to
add a special annotation:
In some cases, you may also need to add serde annotations:
Adding Java Annotations
Java annotations can be added as follows:
Multiple annotations can be added like this:
Config
In most cases, config options can be passed via the command line. However, some
options can only be added in a typeshare.toml file. Here is an example config
file:
[]
= "com.typeshare.java"
= true
[]
= "None"
[]
= "Gson"
Options
header-comment
A header comment can be added to each generated file.
[]
= "Default" // Generated by typeshare-java <version>
// Or...
= "None"
// Or...
= "Custom"
= "This comment will be included at the top of each generated file"
package
The package name of the output.
<>; // Added to the top of output files
prefix
An optional prefix for type names.
With prefix set to "Tada":
public record
namespace_class
Java only supports one top level file per class. We can get around this by wrapping classes in a namespace class. This will have the same name as the crate, converted to pascal case.
If the crate is named my_crate and namespace classes are enabled, the
following Java code will be generated:
serializer
Java has several serialization/deserialization packages. Depending on the Rust
code you're working with, serializer specific code may need to be emitted.
Currently, only None and Gson are supported. If None (default), then
some data structures, such as algebraic enums, cannot be converted to Java code.
[]
= "None"
// Or...
= "Gson"
indent
The indent type and size can be configured as follows:
[]
= "Spaces"
= 4