From 67242c8f4b7a18165d57401e7c05eca763f6aa50 Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Thu, 6 Sep 2018 23:04:08 -0400 Subject: [PATCH] Change `search_accounts` to use a macro --- src/lib.rs | 23 +---------------------- src/macros.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 433c9ad..5e89975 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -169,6 +169,7 @@ impl MastodonClient for Mastodon { (get) mutes: "mutes" => Account, (get) notifications: "notifications" => Notification, (get) reports: "reports" => Report, + (get (q: &'a str, #[serde(skip_serializing_if = "Option::is_none")] limit: Option, following: bool,)) search_accounts: "accounts/search" => Account, } paged_routes_with_id! { @@ -340,28 +341,6 @@ impl MastodonClient for Mastodon { 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, - following: bool, - ) -> Result> { - 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 ops::Deref for Mastodon { diff --git a/src/macros.rs b/src/macros.rs index 8f03449..26e6ae3 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -54,6 +54,50 @@ macro_rules! paged_routes { 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> { + 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)*} + }; + () => {} }