Skip to main content

Crate vamo_macros

Crate vamo_macros 

Source
Expand description

§Vamo Macros

Crates.io downloads crates.io Build Status Crates.io MSRV Documentation MIT licensed Codecov

Vamo macros is a collection of macros to make possible use structs as resources to be sent over vamo as client.

§Features

  • derive macro for resource trait implementation
  • bora attribute macro for quick client creation

§Install

Either run from command line:

cargo add vamo-macros vamo deboa-tokio

Or add to your Cargo.toml:

vamo = "0.0.1"
vamo-macros = "0.0.1"
deboa-tokio = "0.1.0"

§Usage

§Resource macro

use deboa::{Result, serde::RequestBody};
use deboa_extras::serde::json::JsonBody;
use deboa_tokio::Client;
use serde::Serialize;
use vamo::{Vamo, resource::ResourceMethod};
use vamo_macros::Resource;

#[derive(Resource, Serialize)]
#[name("posts")]
#[body_type(JsonBody)]
pub struct User {
    #[rid]
    id: i32,
    name: String,
}

#[tokio::main]
async fn main() -> Result<()> {
    let mut vamo = Vamo::new("https://jsonplaceholder.typicode.com")?;
    vamo.client(Client::default());

    let mut user = User {
        id: 1,
        name: "John Doe".to_string(),
    };

    // post
    let response = vamo
        .create(&mut user)?
        .send()
        .await?;

    // put
    vamo.update(&mut user)?
        .send()
        .await?;

    // patch
    vamo.edit(&mut user)?
        .send()
        .await?;

    // delete
    vamo.remove(&mut user)?
        .send()
        .await?;

    Ok(())
}

§bora macro

use deboa::Result;
use deboa_tokio::Client;
use vamo::Vamo;
use vamo_macros::bora;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct Post {
    pub id: u32,
    pub title: String,
}

#[bora(
    api(
        get(name="get_all", path="/posts", res_body=Vec<Post>, format="json"),
        get(name="get_by_id", path="/posts/<id:i32>", res_body=Post, format="json"),
        get(name="query_by_id", path="/posts?<id:i32>", res_body=Vec<Post>, format="json"),
        get(name="query_by_title", path="/posts?<id:i32>&<title:&str>", res_body=Vec<Post>, format="json")
    )
)]
pub struct PostService;

#[tokio::main]
async fn main() -> Result<()> {
    let mut vamo = Vamo::new("https://jsonplaceholder.typicode.com")?;
    vamo.client(Client::default());
    let mut post_service = PostService::new(vamo);
    let post = post_service.get_by_id(1).await?;

    println!("id...: {}", post.id);
    println!("title: {}", post.title);
    assert_eq!(post.id, 1);

    Ok(())
}

§Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

§License

Licensed under either of

at your option.

§Authors

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.