Crates.io | js-macros |
lib.rs | js-macros |
version | 0.1.5 |
source | src |
created_at | 2021-12-21 06:23:58.789384 |
updated_at | 2021-12-24 02:10:13.547233 |
description | Quickly prototype procedural macros using JavaScript or TypeScript! |
homepage | |
repository | https://github.com/vultix/js-macros |
max_upload_size | |
id | 501026 |
size | 20,620 |
Quickly prototype Rust procedural macros using JavaScript or TypeScript!
Have you ever thought "this would be a great use case for a procedural macro," but didn't want to go through all the effort? This crate is the perfect fix for you!
[dependencies]
js-macros = "0.1"
Setup is just three easy steps!
js-macros
folder at the root of your cargo workspace.js
or .ts
macro file in the folderHelpful tip: These macros are invoked using node
, allowing you to use tools like require()
Example macros can be found in this repository's js-macros folder, with example usage in the examples folder.
Here's all it takes to write a custom derive(Copy)
macro:
//! JS_MACRO: derive(Copy)
const type = /(?:struct|enum) (.*?)\s/.exec(MACRO_INPUT)[1];
MACRO_OUTPUT = `impl Copy for ${type} {}`;
Usage is as simple as #[derive(js_macros::Copy, Clone)]
Any errors thrown by your js code will be captured and turned into a procedural macro error at build time. You can take advantage of this by throwing errors to debug your js macros.
Each macro expansion takes somewhere in the ballpark of 10ms to 40ms, which can add up quickly.
Typescript compilation is cached and should only affect initial builds.
Rust Analyzer: Restart Server
command after changing a js macroCtrl+Shift+A
on Linux/Windows, ⌘⇧A
on macOS)org.rust.cargo.evaluate.build.scripts
and org.rust.macros.proc