Change `search_accounts` to use a macro

master
Paul Woolcock 6 years ago
parent 34e2c00866
commit 67242c8f4b
  1. 23
      src/lib.rs
  2. 44
      src/macros.rs

@ -169,6 +169,7 @@ impl<H: HttpSend> MastodonClient<H> for Mastodon<H> {
(get) mutes: "mutes" => Account, (get) mutes: "mutes" => Account,
(get) notifications: "notifications" => Notification, (get) notifications: "notifications" => Notification,
(get) reports: "reports" => Report, (get) reports: "reports" => Report,
(get (q: &'a str, #[serde(skip_serializing_if = "Option::is_none")] limit: Option<u64>, following: bool,)) search_accounts: "accounts/search" => Account,
} }
paged_routes_with_id! { paged_routes_with_id! {
@ -340,28 +341,6 @@ impl<H: HttpSend> MastodonClient<H> for Mastodon<H> {
Page::new(self, response) Page::new(self, response)
} }
/// Search for accounts by their name.
/// Will lookup an account remotely if the search term is in the
/// `username@domain` format and not yet in the database.
fn search_accounts(
&self,
query: &str,
limit: Option<u64>,
following: bool,
) -> Result<Page<Account, H>> {
let url = format!(
"{}/api/v1/accounts/search?q={}&limit={}&following={}",
self.base,
query,
limit.unwrap_or(40),
following
);
let response = self.send(&mut self.client.get(&url))?;
Page::new(self, response)
}
} }
impl<H: HttpSend> ops::Deref for Mastodon<H> { impl<H: HttpSend> ops::Deref for Mastodon<H> {

@ -54,6 +54,50 @@ macro_rules! paged_routes {
paged_routes!{$($rest)*} paged_routes!{$($rest)*}
}; };
((get ($($(#[$m:meta])* $param:ident: $typ:ty,)*)) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => {
doc_comment! {
concat!(
"Equivalent to `/api/v1/",
$url,
"`\n# Errors\nIf `access_token` is not set."
),
fn $name<'a>(&self, $($param: $typ,)*) -> Result<Page<$ret, H>> {
use serde_urlencoded;
#[derive(Serialize)]
struct Data<'a> {
$(
$(
#[$m]
)*
$param: $typ,
)*
#[serde(skip)]
_marker: ::std::marker::PhantomData<&'a ()>,
}
let qs_data = Data {
$(
$param: $param,
)*
_marker: ::std::marker::PhantomData,
};
let qs = serde_urlencoded::to_string(&qs_data)?;
let url = format!(concat!("/api/v1/", $url, "?{}"), &qs);
let response = self.send(
&mut self.client.get(&url)
)?;
Page::new(self, response)
}
}
paged_routes!{$($rest)*}
};
() => {} () => {}
} }

Loading…
Cancel
Save