boxdot / letterboxd-rs

Letterboxd API for access to data on the Letterboxd.com website in Rust.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Abstract get, post, patch and delete in macro

boxdot opened this issue · comments

Example implementation

macro_rules! GET {
    ($func_name:ident, ($path:expr $(, $arg:ident:$T:ty)*), $ReqT:ty, $RespT:tt) => {
        pub fn $func_name(&self $( , $arg: $T)*, req: &$ReqT) -> $RespT {
            println!(
                concat!("Called `{}` for path `", $path, "`"), stringify!($func_name) $( , $arg)*);
            println!("{:?}", req);
            $RespT { body: String::from("empty") }
        }
    };
    ($func_name:ident, $path:expr, $ReqT:ty, $RespT:tt) => {
        GET!($func_name, ($path), $ReqT, $RespT);
    };
}

#[derive(Debug)]
pub struct Request {
    param: String,
}
pub struct Response {
    body: String,
}

pub struct Client;

impl Client {
    GET!(films, "/film", Request, Response);
    GET!(film, ("/film/{}/title/{}", id: i32, name: &str), Request, Response);
}

fn main() {
    Client.films(&Request { param: String::from("foo") });
    Client.film(1, "foo", &Request { param: String::from("foo") });
}

Done.