Crates.io | rdc |
lib.rs | rdc |
version | 0.1.0 |
source | src |
created_at | 2023-01-29 18:18:58.743592 |
updated_at | 2023-01-29 18:18:58.743592 |
description | Library for generating Java code from rust structs |
homepage | |
repository | https://github.com/skdziwak/rs-rdc |
max_upload_size | |
id | 771138 |
size | 64,697 |
RDC - Rust Data Codegen
This crate is used to generate code for other languages from Rust's data structures. It can be used to generate DTO classes to make it easier to interact with other languages.
It currently supports only Java, but it can be easily extended to support other languages in the future.
It relies on the serde
crate to serialize and deserialize data.
For testing purposes it uses gradle to compile and run the generated Java code.
use rdc::{rdc_java, RDC};
use rdc::targets::java::JavaClass;
use serde::{Serialize, Deserialize};
#[derive(RDC, Serialize, Deserialize)]
struct MyStruct {
field1: String,
field2: i32,
}
#[derive(RDC)]
struct MyStruct2 {
field1: Vec<String>,
}
let classes: Vec<JavaClass> = rdc_java!(MyStruct, MyStruct2).unwrap();
You do not have to specify all the types that are used in your data structures. RDC will automatically add all the dependencies.
use rdc::{rdc_java, RDC};
use rdc::targets::java::JavaClass;
#[derive(RDC)]
struct Dependency {
field1: String,
}
#[derive(RDC)]
struct MyStruct {
field1: String,
dependency: Dependency,
}
let classes: Vec<JavaClass> = rdc_java!(MyStruct).unwrap();
assert_eq!(classes.len(), 2);
use rdc::{rdc_java, RDC};
#[derive(RDC)]
enum MyEnum {
Variant1,
Variant2
}
rdc_java!(MyEnum).unwrap();
Data enum is a special type of enum that can contain data.
use rdc::{rdc_java, RDC};
#[derive(RDC)]
enum MyEnum {
Variant1(i32),
Variant2,
Variant3 { field1: String }
}
rdc_java!(MyEnum).unwrap();
Example JSON representations:
[
{
"Variant1": 1
},
"Variant2",
{
"Variant3": {
"field1": "value"
}
}
]
There is support for generics in RDC.
It works by generating a Java class for each combination of generic types.
Every used generic type should implement rdc::RDCType
trait.
Please note that this trait is implemented automatically by #[derive(RDC)]
.
use rdc::{rdc_java, RDC, RDCType};
#[derive(RDC)]
struct MyStruct<T> where T: RDCType {
value: T,
}
let classes = rdc_java!(MyStruct<i32>, MyStruct<String>).unwrap();
assert_eq!(classes.len(), 2);
RDC can write the generated code to files.
use rdc::{rdc_java, RDC};
use rdc::targets::java::{JavaClass, write_java};
#[derive(RDC)]
enum MyEnum {
Variant1,
Variant2
}
let classes: Vec<JavaClass> = rdc_java!(MyEnum).unwrap();
write_java(&classes, "com.example", "target/test-tmp/src/main/java").unwrap();
You can use #[serde(rename = "new_name")]
to rename fields and it will be reflected in the generated code.
License: MIT