Added doc_comment to properly doc functions

master
Aaron Power 6 years ago
parent 142d8e7572
commit 87a0c103e6
  1. 3
      Cargo.toml
  2. 189
      src/lib.rs

@ -11,10 +11,11 @@ keywords = ["api", "web", "social", "mastodon", "wrapper"]
categories = ["web-programming", "http-client"] categories = ["web-programming", "http-client"]
[dependencies] [dependencies]
doc-comment = "0.1"
reqwest = "0.8" reqwest = "0.8"
serde = "1" serde = "1"
serde_json = "1"
serde_derive = "1" serde_derive = "1"
serde_json = "1"
url = "1" url = "1"
[dependencies.chrono] [dependencies.chrono]

@ -36,6 +36,7 @@
#![cfg_attr(test, deny(missing_docs))] #![cfg_attr(test, deny(missing_docs))]
#[macro_use] extern crate serde_derive; #[macro_use] extern crate serde_derive;
#[macro_use] extern crate doc_comment;
#[macro_use] extern crate serde_json as json; #[macro_use] extern crate serde_json as json;
extern crate chrono; extern crate chrono;
extern crate reqwest; extern crate reqwest;
@ -92,18 +93,20 @@ macro_rules! methods {
macro_rules! paged_routes { macro_rules! paged_routes {
(($method:ident) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => { (($method:ident) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => {
/// Equivalent to /api/v1/ doc_comment! {
#[doc = $url] concat!(
/// "Equivalent to `/api/v1/",
#[doc = "# Errors"] $url,
/// If `access_token` is not set. "`\n# Errors\nIf `access_token` is not set."),
pub fn $name(&self) -> Result<Page<$ret>> { pub fn $name(&self) -> Result<Page<$ret>> {
let url = self.route(concat!("/api/v1/", $url)); let url = self.route(concat!("/api/v1/", $url));
let response = self.client.$method(&url) let response = self.client.$method(&url)
.headers(self.headers.clone()) .headers(self.headers.clone())
.send()?; .send()?;
Page::new(self, response) Page::new(self, response)
}
} }
paged_routes!{$($rest)*} paged_routes!{$($rest)*}
@ -115,79 +118,83 @@ macro_rules! paged_routes {
macro_rules! route { macro_rules! route {
((post multipart ($($param:ident: $typ:ty,)*)) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => { ((post multipart ($($param:ident: $typ:ty,)*)) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => {
/// Equivalent to /api/v1/ doc_comment! {
#[doc = $url] concat!(
/// "Equivalent to `/api/v1/",
#[doc = "# Errors"] $url,
/// If `access_token` is not set. "`\n# Errors\nIf `access_token` is not set."),
pub fn $name(&self, $($param: $typ,)*) -> Result<$ret> { pub fn $name(&self, $($param: $typ,)*) -> Result<$ret> {
use reqwest::multipart::Form; use reqwest::multipart::Form;
let form_data = Form::new() let form_data = Form::new()
$( $(
.file(stringify!($param), $param.as_ref())? .file(stringify!($param), $param.as_ref())?
)*; )*;
let response = self.client.post(&self.route(concat!("/api/v1/", $url))) let response = self.client.post(&self.route(concat!("/api/v1/", $url)))
.headers(self.headers.clone()) .headers(self.headers.clone())
.multipart(form_data) .multipart(form_data)
.send()?; .send()?;
let status = response.status().clone(); let status = response.status().clone();
if status.is_client_error() { if status.is_client_error() {
return Err(Error::Client(status)); return Err(Error::Client(status));
} else if status.is_server_error() { } else if status.is_server_error() {
return Err(Error::Server(status)); return Err(Error::Server(status));
} }
deserialise(response) deserialise(response)
}
} }
route!{$($rest)*} route!{$($rest)*}
}; };
(($method:ident ($($param:ident: $typ:ty,)*)) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => { (($method:ident ($($param:ident: $typ:ty,)*)) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => {
/// Equivalent to /api/v1/ doc_comment! {
#[doc = $url] concat!(
/// "Equivalent to `/api/v1/",
#[doc = "# Errors"] $url,
/// If `access_token` is not set. "`\n# Errors\nIf `access_token` is not set."),
pub fn $name(&self, $($param: $typ,)*) -> Result<$ret> {
pub fn $name(&self, $($param: $typ,)*) -> Result<$ret> {
let form_data = json!({
$(
stringify!($param): $param,
)*
});
let response = self.client.$method(&self.route(concat!("/api/v1/", $url)))
.headers(self.headers.clone())
.json(&form_data)
.send()?;
let status = response.status().clone();
if status.is_client_error() {
return Err(Error::Client(status));
} else if status.is_server_error() {
return Err(Error::Server(status));
}
deserialise(response) let form_data = json!({
$(
stringify!($param): $param,
)*
});
let response = self.client.$method(&self.route(concat!("/api/v1/", $url)))
.headers(self.headers.clone())
.json(&form_data)
.send()?;
let status = response.status().clone();
if status.is_client_error() {
return Err(Error::Client(status));
} else if status.is_server_error() {
return Err(Error::Server(status));
}
deserialise(response)
}
} }
route!{$($rest)*} route!{$($rest)*}
}; };
(($method:ident) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => { (($method:ident) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => {
/// Equivalent to /api/v1/ doc_comment! {
#[doc = $url] concat!(
/// "Equivalent to `/api/v1/",
#[doc = "# Errors"] $url,
/// If `access_token` is not set. "`\n# Errors\nIf `access_token` is not set."),
pub fn $name(&self) -> Result<$ret> { pub fn $name(&self) -> Result<$ret> {
self.$method(self.route(concat!("/api/v1/", $url))) self.$method(self.route(concat!("/api/v1/", $url)))
}
} }
route!{$($rest)*} route!{$($rest)*}
@ -200,13 +207,14 @@ macro_rules! route_id {
($(($method:ident) $name:ident: $url:expr => $ret:ty,)*) => { ($(($method:ident) $name:ident: $url:expr => $ret:ty,)*) => {
$( $(
/// Equivalent to /api/v1/ doc_comment! {
#[doc = $url] concat!(
/// "Equivalent to `/api/v1/",
#[doc = "# Errors"] $url,
/// If `access_token` is not set. "`\n# Errors\nIf `access_token` is not set."),
pub fn $name(&self, id: u64) -> Result<$ret> { pub fn $name(&self, id: u64) -> Result<$ret> {
self.$method(self.route(&format!(concat!("/api/v1/", $url), id))) self.$method(self.route(&format!(concat!("/api/v1/", $url), id)))
}
} }
)* )*
} }
@ -215,18 +223,19 @@ macro_rules! route_id {
macro_rules! paged_routes_with_id { macro_rules! paged_routes_with_id {
(($method:ident) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => { (($method:ident) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => {
/// Equivalent to /api/v1/ doc_comment! {
#[doc = $url] concat!(
/// "Equivalent to `/api/v1/",
#[doc = "# Errors"] $url,
/// If `access_token` is not set. "`\n# Errors\nIf `access_token` is not set."),
pub fn $name(&self, id: &str) -> Result<Page<$ret>> { pub fn $name(&self, id: &str) -> Result<Page<$ret>> {
let url = self.route(&format!(concat!("/api/v1/", $url), id)); let url = self.route(&format!(concat!("/api/v1/", $url), id));
let response = self.client.$method(&url) let response = self.client.$method(&url)
.headers(self.headers.clone()) .headers(self.headers.clone())
.send()?; .send()?;
Page::new(self, response) Page::new(self, response)
}
} }
route!{$($rest)*} route!{$($rest)*}

Loading…
Cancel
Save