Js-macros - Quickly prototype Rust procedural macros using JavaScript or TypeScript!

Overview

js-macros

Crates.io MIT/Apache 2.0 CI Crates.io

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"

How to use

Setup is just three easy steps!

  1. Create a js-macros folder at the root of your cargo workspace
  2. Create a new .js or .ts macro file in the folder
  3. Import your new macro and use it anywhere in your project!

Helpful tip: These macros are invoked using node, allowing you to use tools like require()

Examples

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)]

Debugging Macros

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.

Build Time Impact

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.

IDE Support

  • rust-analyzer - works very well with js-macros, you'll just need to run the Rust Analyzer: Restart Server command after changing a js macro
  • IntelliJ-Rust - More finicky, but still picks up js-macro generated items
    • First, you'll need to enable the experimental build script and procedural macro features:
      • Call Help | Find Action (Ctrl+Shift+A on Linux/Windows, ⌘⇧A on macOS)
      • Search for Experimental Features
      • Enable org.rust.cargo.evaluate.build.scripts and org.rust.macros.proc
    • After making a change to a js macro, you'll want to run the "Refresh Cargo Projects" command.
      • Your old macro output still seems to be cached after this, so you'll need to change the body of the macro as well
You might also like...
Prototype for a CLI/Libary designed for interacting with NASA Open APIs with Rust.

Overview Voyager is a swiss army knife library for the NASA Open APIs. It is designed to bundle all the NASA APIs into a single package. Voyager can b

A creator library for procedural 2D noises and patterns in Rust.
A creator library for procedural 2D noises and patterns in Rust.

A curated list of common 2D noises and patterns in computer graphics. Mostly taken from implementations on Shadertoy. All implementations are under th

niwl - a prototype system for open, decentralized, metadata resistant communication

niwl - a prototype system for open, decentralized, metadata resistant communication niwl (/nɪu̯l/) - fog, mist or haze (Welsh). niwl is an experimenta

A procedural macro that copy-pastes match arms for new type variant enums.

All the same! If you ever had code that looks like this: use std::io; use std::pin::Pin; use std::task::{Context, Poll}; use tokio::io::AsyncWrite; us

Demo app duplicated in 5 languages (Go/JavaScript/Python/Ruby/Rust) showing how to go from source code to container image using melange+apko

hello-melange-apko 💫 This repo contains an example app duplicated across 5 languages showing how to: Package source code into APKs using melange Buil

Build terminal dashboards using ascii/ansi art and javascript
Build terminal dashboards using ascii/ansi art and javascript

blessed-contrib Build dashboards (or any other application) using ascii/ansi art and javascript. Friendly to terminals, ssh and developers.

A Typescript interpreter in Rust.

typescript This is a Rust native implementation of a Typescript Parser and a JIT execution engine. This project comes in two crates: [typescript-ast]:

Pure Rust implementation of Javascript Object Signing and Encryption (JOSE)

RustCrypto: JOSE Pure Rust implementation of Javascript Object Signing and Encryption (JOSE) License All crates licensed under either of Apache Licens

High-performance Javascript color gradient library powered by Rust + WebAssembly

colorgrad-js High-performance Javascript color gradient library powered by Rust + WebAssembly. No dependencies. Faster than d3-scale, chroma-js, culor

Owner
null
auto-rust is an experimental project that aims to automatically generate Rust code with LLM (Large Language Models) during compilation, utilizing procedural macros.

Auto Rust auto-rust is an experimental project that aims to automatically generate Rust code with LLM (Large Language Models) during compilation, util

Minsky 6 May 14, 2023
Tools - The Rome Toolchain. A linter, compiler, bundler, and more for JavaScript, TypeScript, HTML, Markdown, and CSS.

Rome is currently being rewritten in Rust. Read more about it in our latest blog post. The documentation below is out of date and available for poster

Rome 22k Jan 3, 2023
A Faster(⚡) formatter, linter, bundler, and more for JavaScript, TypeScript, JSON, HTML, Markdown, and CSS Lapce Plugin

Lapce Plugin for Rome Lapce-rome is a Lapce plugin for rome, The Rome is faster ⚡ , A formatter, linter, compiler, bundler, and more for JavaScript, T

xiaoxin 7 Dec 16, 2022
compile TypeScript or JavaScript to binaries

the powr project Development is paused until 2023. ?? powr aims to be a javascript/typescript engine to power serverless functions over the web. the j

powr.js 18 Dec 29, 2022
JavaScript/TypeScript runtime Deno was meant to be.

Project Bueno (temporary name) Bueno is a web-standards focused JS/TS runtime with as much tooling built-in as possible. It is meant to be both a lear

Bueno 22 Sep 3, 2023
CLI tool to quickly create React + Typescript + Tailwind projects

QUICK INIT CLI Tool to quickly create React projects with Tailwind automatically configured for use. Typescript is selected as default. If JS is to be

Oscar 5 Nov 3, 2022
Prototype of the `std::io::ensure!` family of macros

io-ensure Prototype of the `std::io::ensure` family of macros API Docs | Releases | Contributing Installation $ cargo add io-ensure Safety This crate

Yosh 4 Feb 27, 2023
Valq - macros for querying and extracting value from structured data by JavaScript-like syntax

valq   valq provides a macro for querying and extracting value from structured data in very concise manner, like the JavaScript syntax. Look & Feel: u

Takumi Fujiwara 24 Dec 21, 2022
Write CI/CD pipelines using TypeScript

Katoa Katoa is a community fork of Cidada, a tool created by Fig which was sunset in late 2023 following acquisition by AWS. This fork and the underly

Katoa 47 Oct 6, 2023
Rust(1) cli prototype

rust-cli rust(1) CLI prototype API Docs | Releases | Contributing Installation $ cargo add rust-cli Safety This crate uses #![deny(unsafe_code)] to en

Yosh 16 Jan 4, 2022