| Crates.io | export-type |
| lib.rs | export-type |
| version | 0.1.15 |
| created_at | 2024-12-03 07:14:32.886869+00 |
| updated_at | 2024-12-04 23:22:39.024967+00 |
| description | Export Rust types to other languages |
| homepage | https://github.com/auser/traefikctl |
| repository | https://github.com/auser/traefikctl |
| max_upload_size | |
| id | 1469742 |
| size | 98,419 |
A Rust proc-macro crate for automatically generating TypeScript type definitions from Rust structs and enums.
rename and rename_all attributesAdd this to your Cargo.toml:
export-type = { version = "0.1.1", optional = true }
Use the #[export_type] attribute on structs and enums.
Example:
#[derive(ExportType)]
#[export_type(rename_all = "camelCase", path = "frontend/src/types")]
struct MyStruct {
field: String,
}
#[derive(ExportType)]
#[export_type(rename_all = "camelCase", path = "frontend/src/types")]
enum MyEnum {
Variant1,
Variant2,
}
And you'll get a index.ts file in the specified path with the following contents:
export type MyStruct = {
field: string;
};
export type MyEnum = "Variant1" | "Variant2";
You'll need to add a build.rs file to your project that builds the TypeScript file at build-time. Here's an example of how to do this:
use std::env;
use std::fs;
use std::path::Path;
fn main() {
// Skip file copying during cargo publish
if env::var("CARGO_PUBLISH").is_ok() {
return;
}
// Get the output directory from the proc macro
if let Ok(types_dir) = env::var("TYPES_OUT_DIR") {
let types_path = Path::new(&types_dir).join("types");
// Get the target directory for types (customize this path as needed)
let target_dir = Path::new("frontend/src/types");
// Only copy if the types were generated
if types_path.exists() {
// Create target directory if it doesn't exist
fs::create_dir_all(target_dir).expect("Failed to create target directory");
// Copy the generated index.ts file
if let Ok(entries) = fs::read_dir(&types_path) {
for entry in entries.flatten() {
let path = entry.path();
if path.is_file() && path.extension().map_or(false, |ext| ext == "ts") {
let file_name = path.file_name().unwrap();
let target_path = target_dir.join(file_name);
fs::copy(&path, &target_path)
.unwrap_or_else(|e| panic!("Failed to copy {:?}: {}", file_name, e));
}
}
}
}
}
// Tell Cargo to rerun this if any of these change
println!("cargo:rerun-if-changed=src");
println!("cargo:rerun-if-env-changed=CARGO_PUBLISH");
println!("cargo:rerun-if-env-changed=TYPES_OUT_DIR");
}
Also add the following to your Cargo.toml:
[package]
build = "build.rs"