Implement a method for `GET /api/v2/search`

master
Paul Woolcock 6 years ago
parent 67242c8f4b
commit 28192e1188
  1. 2
      src/entities/mod.rs
  2. 17
      src/entities/search_result.rs
  3. 4
      src/lib.rs
  4. 57
      src/macros.rs
  5. 4
      src/mastodon_client.rs

@ -43,7 +43,7 @@ pub mod prelude {
notification::Notification,
relationship::Relationship,
report::Report,
search_result::SearchResult,
search_result::{SearchResult, SearchResultV2},
status::{Application, Emoji, Status},
Empty,
};

@ -1,6 +1,9 @@
//! A module containing info relating to a search result.
use super::prelude::{Account, Status};
use super::{
prelude::{Account, Status},
status::Tag,
};
/// A struct containing results of a search.
#[derive(Debug, Clone, Deserialize)]
@ -12,3 +15,15 @@ pub struct SearchResult {
/// An array of matched hashtags, as strings.
pub hashtags: Vec<String>,
}
/// A struct containing results of a search, with `Tag` objects in the
/// `hashtags` field
#[derive(Debug, Clone, Deserialize)]
pub struct SearchResultV2 {
/// An array of matched Accounts.
pub accounts: Vec<Account>,
/// An array of matched Statuses.
pub statuses: Vec<Status>,
/// An array of matched hashtags, as `Tag` objects.
pub hashtags: Vec<Tag>,
}

@ -193,6 +193,10 @@ impl<H: HttpSend> MastodonClient<H> for Mastodon<H> {
(post) clear_notifications: "notifications/clear" => Empty,
}
route_v2! {
(get (q: &'a str, resolve: bool,)) search_v2: "search" => SearchResultV2,
}
route_id! {
(get) get_account: "accounts/{}" => Account,
(post) follow: "accounts/{}/follow" => Account,

@ -19,7 +19,7 @@ macro_rules! paged_routes {
(($method:ident) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => {
doc_comment! {
concat!(
"Equivalent to `/api/v1/",
"Equivalent to `", stringify!($method), " /api/v1/",
$url,
"`\n# Errors\nIf `access_token` is not set.",
"\n",
@ -57,7 +57,7 @@ macro_rules! paged_routes {
((get ($($(#[$m:meta])* $param:ident: $typ:ty,)*)) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => {
doc_comment! {
concat!(
"Equivalent to `/api/v1/",
"Equivalent to `get /api/v1/",
$url,
"`\n# Errors\nIf `access_token` is not set."
),
@ -101,12 +101,53 @@ macro_rules! paged_routes {
() => {}
}
macro_rules! route_v2 {
((get ($($param:ident: $typ:ty,)*)) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => {
doc_comment! {
concat!(
"Equivalent to `get /api/v2/",
$url,
"`\n# Errors\nIf `access_token` is not set."
),
fn $name<'a>(&self, $($param: $typ,)*) -> Result<$ret> {
use serde_urlencoded;
#[derive(Serialize)]
struct Data<'a> {
$(
$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/v2/", $url, "?{}"), &qs);
Ok(self.get(self.route(&url))?)
}
}
route_v2!{$($rest)*}
};
() => {}
}
macro_rules! route {
((post multipart ($($param:ident: $typ:ty,)*)) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => {
doc_comment! {
concat!(
"Equivalent to `/api/v1/",
"Equivalent to `post /api/v1/",
$url,
"`\n# Errors\nIf `access_token` is not set."),
fn $name(&self, $($param: $typ,)*) -> Result<$ret> {
@ -141,7 +182,7 @@ macro_rules! route {
((get ($($param:ident: $typ:ty,)*)) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => {
doc_comment! {
concat!(
"Equivalent to `/api/v1/",
"Equivalent to `get /api/v1/",
$url,
"`\n# Errors\nIf `access_token` is not set."
),
@ -178,7 +219,7 @@ macro_rules! route {
(($method:ident ($($param:ident: $typ:ty,)*)) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => {
doc_comment! {
concat!(
"Equivalent to `/api/v1/",
"Equivalent to `", stringify!($method), " /api/v1/",
$url,
"`\n# Errors\nIf `access_token` is not set.",
),
@ -213,7 +254,7 @@ macro_rules! route {
(($method:ident) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => {
doc_comment! {
concat!(
"Equivalent to `/api/v1/",
"Equivalent to `", stringify!($method), " /api/v1/",
$url,
"`\n# Errors\nIf `access_token` is not set.",
"\n",
@ -251,7 +292,7 @@ macro_rules! route_id {
$(
doc_comment! {
concat!(
"Equivalent to `/api/v1/",
"Equivalent to `", stringify!($method), " /api/v1/",
$url,
"`\n# Errors\nIf `access_token` is not set.",
"\n",
@ -285,7 +326,7 @@ macro_rules! paged_routes_with_id {
(($method:ident) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => {
doc_comment! {
concat!(
"Equivalent to `/api/v1/",
"Equivalent to `", stringify!($method), " /api/v1/",
$url,
"`\n# Errors\nIf `access_token` is not set.",
"\n",

@ -95,6 +95,10 @@ pub trait MastodonClient<H: HttpSend = HttpSender> {
fn search<'a>(&self, q: &'a str, resolve: bool) -> Result<SearchResult> {
unimplemented!("This method was not implemented");
}
/// GET /api/v2/search
fn search_v2<'a>(&self, q: &'a str, resolve: bool) -> Result<SearchResultV2> {
unimplemented!("This method was not implemented");
}
/// POST /api/v1/follows
fn follows(&self, uri: Cow<'static, str>) -> Result<Account> {
unimplemented!("This method was not implemented");

Loading…
Cancel
Save