| Crates.io | armature-macros-utils |
| lib.rs | armature-macros-utils |
| version | 0.1.0 |
| created_at | 2025-12-26 21:44:00.791388+00 |
| updated_at | 2025-12-26 21:44:00.791388+00 |
| description | Utility functions for Armature macros |
| homepage | https://pegasusheavy.github.io/armature |
| repository | https://github.com/pegasusheavy/armature |
| max_upload_size | |
| id | 2006305 |
| size | 19,395 |
Procedural utility macros for the Armature framework.
[dependencies]
armature-macros-utils = { path = "../armature-macros-utils" }
use armature_macros_utils::{json, html, text, redirect};
// JSON response
json!({ "message": "Hello" })
// HTML response
html!("<h1>Welcome</h1>")
// Text response
text!("Hello, world!")
// Redirect
redirect!("/new-location")
use armature_macros_utils::{validate, validate_required, validate_email};
// Conditional validation
validate!(age >= 18, "Must be 18 or older");
// Required field validation
validate_required!(name);
// Email validation
validate_email!(user_email);
use armature_macros_utils::{bail, ensure};
// Return early with error
if !found {
bail!("User not found");
}
// Ensure condition or error
ensure!(user.is_active(), "User account is inactive");
use armature_macros_utils::{Model, ApiModel, Resource};
use serde::{Serialize, Deserialize};
// Basic model
#[derive(Model, Serialize, Deserialize)]
pub struct User {
pub id: i64,
pub name: String,
}
// API model with field visibility
#[derive(ApiModel, Serialize, Deserialize)]
pub struct UserResponse {
pub id: i64,
pub name: String,
#[api(skip)]
pub password_hash: String, // Excluded from API
}
// Resource model for database
#[derive(Resource, Serialize, Deserialize)]
#[resource(table = "users")]
pub struct UserEntity {
#[resource(primary_key)]
pub id: i64,
pub name: String,
}
use armature_macros_utils::{test_request, assert_json, assert_status};
#[tokio::test]
async fn test_endpoint() {
let req = test_request!(GET "/users/1");
let resp = handler(req).await.unwrap();
assert_status!(resp, 200);
assert_json!(resp, { "id": 1, "name": "Alice" });
}
These crates work together:
// Declarative macros
use armature_macros::prelude::*;
// Procedural macros
use armature_macros_utils::*;
#[get("/users/:id")]
async fn get_user(req: HttpRequest) -> Result<HttpResponse, Error> {
let id: i64 = path_param!(req, "id")?; // From armature-macros
match db.find(id).await {
Some(user) => ok_json!(user), // From armature-macros
None => not_found!("User not found"), // From armature-macros
}
}
See the Macros Guide for comprehensive documentation.
MIT OR Apache-2.0