This repository implements JSON interceptors in Rust using Macros. One of the key features is the validate_name
macro.
This macro validates that the name
field of a Person
is at least 5 characters long. If the validation fails, it returns a BadRequest
status.
Here is how you can use it:
#[alphe::validate_name]
#[post("/", format = "json", data = "<person>")]
fn create_person(person: Json<Person>, db: &State<Database>) -> CustomResponse {
// Your function implementation here
}
This macro validates that the lastname
field of a Person
is at least 5 characters long. If the validation fails, it returns a BadRequest
status.
This macro validates that the email
field of a Person
contains an "@" character. If the validation fails, it returns a BadRequest
status.
This macro validates the name
, lastname
, and email
fields of a Person
using the validate_name
, validate_lastname
, and validate_email
macros. If any validation fails, it returns a BadRequest
status with a JSON array of error messages.
This macro adds an updated_at
field to a Person
with a value of "2021-09-01". It then returns the updated Person
.
The validate_name
macro is a procedural macro that validates the name field of a JSON object. It checks if the length of the name is less than 5 characters and returns a BadRequest
status if the condition is met.
The validate_name
macro is implemented using the proc_macro_attribute attribute. It takes two arguments: _attr
and item. _attr
is the attribute input and item
is the annotated item.
The macro first parses the item
as a function. It then defines a block of validation logic that clones the person
object, checks the length of the name, and returns a BadRequest
status if the name is less than 5 characters long.
The macro then prepends the validation logic to the original function body and returns the modified function.
Here's a brief overview of the steps to create a procedural macro:
- Create a new library crate for the macro with
cargo new --lib alphe
. - Navigate into the new crate directory with
cd alphe
. - Add the necessary dependencies for creating procedural macros to the
Cargo.toml
file. This usually includes thesyn
andquote
crates. - Define your procedural macro in the
src/lib.rs
file. Procedural macros are defined as functions with the#[proc_macro]
,#[proc_macro_derive]
, or#[proc_macro_attribute]
attribute. - Use your procedural macro in another crate by adding the macro crate as a dependency and importing the macro with
use
.
- Procedural Macros
- Attribute Macros: These are used to apply metadata to some Rust construct. Here's an example:
#[test] fn it_works() { assert_eq!(2 + 2, 4); }
- The little book of Rust Macros