Crates.io | export-type |
lib.rs | export-type |
version | |
source | src |
created_at | 2024-12-03 07:14:32.886869 |
updated_at | 2024-12-04 23:22:39.024967 |
description | Export Rust types to other languages |
homepage | https://github.com/auser/traefikctl |
repository | https://github.com/auser/traefikctl |
max_upload_size | |
id | 1469742 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
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"