gluer
A wrapper for Rust frameworks that eliminates redundant type and function definitions between the frontend and backend. Currently, it supports only the axum framework.
Origin of the Name
The name "gluer" is inspired by the tool's primary function, gluing together different parts of a Rust-based web application. Just as glue binds different materials together to form a cohesive whole, gluer integrates various components of the frontend and backend, ensuring they work seamlessly without redundant code.
Installation
Add this to your Cargo.toml:
[]
= "0.7.0"
Features
- Define routing and API generation as outlined in How to use.
- Everything is done on macro expansion (compile time), even the generating of the TypeScript file.
- Infer input and output types of functions.
- Support
axum's types completely. - Convert Rust structs to TypeScript interfaces.
- Via the
[#metadata]attribute macro with the#[meta(...)]attribute
- Via the
- Generate a TypeScript file with:
- Functions to access the api
- Supports a custom base URL
- Structs as Interfaces
- Enums as Types, enums with values are not supported, because of the lack of that feature in TypeScript
- Tuples as the TypeScript equivalent, also supports tuples in
axum's path - Types as the TypeScript equivalent
- Supports converting rust specific types as
Resultusing thecustom = [Type, *]attribute as custom ones - Generics, even multiple and nested ones, look for that here
- Using no extra dependencies in the generated TypeScript file.
How to use
gluer generates an api endpoint .ts file. To use it, follow these steps:
Step 1: Define Structs and Functions
To define your structs, functions and enums, use the #[metadata] macro along with the #[meta(...)] attribute. This macro enables gluer to generate metadata for these structures, functions and enums. It does so by implementing the metadata function on structs and enums or by creating a struct that implements both the metadata function and the handler-specific function.
use ;
use metadata;
use ;
// Define a struct with the metadata macro
// Everything you want to use, even if it's just a
// dependency of struct or type, needs to be declared
// with the `#[metadata]` macro
type Result<T> = Result;
// Define an enum with the metadata macro
// Note: Enums with values are not supported
// Define the functions with the metadata macro
async
// Supports axum's input types
async
// Also tuples in paths
async
// Supports enums
async
Step 2: Add Routes
Use the route! macro with axum's Router to add routes. This enables the generate macro to identify the route and generate corresponding functions, structs, types, and enums. Note that inline functions cannot be used because the function names in the generated TypeScript file are inferred from the handler function names.
use ;
use ;
// without `#[metadata]`, it's non-API-important
async
// done like above
async
let mut app: = new
// Add non-API-important directly on the router
.route;
// Add API-important routes with the route macro
route!;
Step 3: Generate API
Generate the API file using the generate macro. This generates the TypeScript file on macro expansion (compile time). You have to specify the root directory of your current project, normally src, a path, where the file should be generated to and with what name, and a different base, "" means no different base.
use generate;
// Make sure to change "tests" to "src" when copying this example
generate!;
And now you can just simply use the router to start your server or do different things, the API should be already generated by your LSP!
Complete Example
Below is a complete example demonstrating the use of gluer with axum:
use ;
use ;
use ;
use HashMap;
async
// Generics are supported, multiple even
// Even deep nested generics are supported and tagging default rust types as Custom
async
// Even tuples are supported
async
// And types?!?
type Result<T> = Result;
type S = String;
async