diff options
author | Paul Woolcock <paul@woolcock.us> | 2018-09-06 23:04:08 -0400 |
---|---|---|
committer | Paul Woolcock <paul@woolcock.us> | 2018-09-07 10:21:37 -0400 |
commit | 67242c8f4b7a18165d57401e7c05eca763f6aa50 (patch) | |
tree | 662497d99723d11aaba4f5ef581a1c7dd8985055 | |
parent | 34e2c008665fc0591727a8c601ca6bdc21ad890a (diff) |
Change `search_accounts` to use a macro
-rw-r--r-- | src/lib.rs | 23 | ||||
-rw-r--r-- | src/macros.rs | 44 |
2 files changed, 45 insertions, 22 deletions
@@ -169,6 +169,7 @@ impl<H: HttpSend> MastodonClient<H> for Mastodon<H> { (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<u64>, following: bool,)) search_accounts: "accounts/search" => Account, } paged_routes_with_id! { @@ -340,28 +341,6 @@ impl<H: HttpSend> MastodonClient<H> for Mastodon<H> { 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> { 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<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)*} + }; + () => {} } |