Crate vamo_macros

Crate vamo_macros 

Source
Expand description

§Vamo Macros

This crate provides procedural macros for the vamo HTTP client, which is a higher-level abstraction over deboa. It includes a derive macro for automatically implementing the Resource trait, making it easy to work with RESTful resources.

§Features

  • Resource Derive Macro: Automatically implement RESTful operations for your types
  • Attribute-based Configuration: Configure resource endpoints using attributes
  • Type-safe Serialization: Seamless integration with serde for request/response bodies
  • Async Support: Built for async/await workflows

§Usage

Add this to your Cargo.toml:

[dependencies]
vamo-macros = { path = "../vamo-macros" }
vamo = { path = "../vamo" }
deboa-extras = { path = "../deboa-extras" }
serde = { version = "1.0", features = ["derive"] }

§Examples

§Basic Resource

use serde::{Deserialize, Serialize};
use vamo_macros::Resource;
use deboa_extras::http::serde::json::JsonBody;

#[derive(Debug, Serialize, Deserialize, Resource)]
#[name("posts")]
#[body_type(JsonBody)]
struct Post {
    #[rid]
    id: Option<u64>,
    title: String,
    body: String,
    user_id: u64,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut vamo = vamo::Vamo::new("https://jsonplaceholder.typicode.com")?;

    // Create a new post
    let new_post = Post {
        id: None,
        title: "Hello World".into(),
        body: "This is a test post".into(),
        user_id: 1,
    };
    let created: Post = vamo.create(&new_post).await?;
    println!("Created post with ID: {}", created.id.unwrap());

    Ok(())
}

§Available Attributes

§Struct Attributes

  • #[name("path")]: Specify the resource name, rest endpoint (e.g., posts, users)
  • #[body_type(Type)]: Specify the request/response body type (e.g., JsonBody, XmlBody)

§Field Attributes

  • #[rid]: Mark a field as the resource identifier (must be Option<T> where T is a primitive type)

§Note

The Resource derive macro automatically implements the following methods:

  • new(base_path, vamo): Create a new resource client
  • id(): Get the resource identifier
  • name(): Get the resource name
  • body_type(): Get the resource body type

Attribute Macros§

bora
The bora attribute macro is used to generate a Deboa client. With this macro you can define the API endpoints and their methods. You can define multiple endpoints and methods in the same macro.

Derive Macros§

Resource
Derive macro for the Resource trait.