diff --git a/src/entities/mod.rs b/src/entities/mod.rs index fc47b3e..ec060ff 100644 --- a/src/entities/mod.rs +++ b/src/entities/mod.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, }; diff --git a/src/entities/search_result.rs b/src/entities/search_result.rs index d357488..db18fff 100644 --- a/src/entities/search_result.rs +++ b/src/entities/search_result.rs @@ -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, } + +/// 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, + /// An array of matched Statuses. + pub statuses: Vec, + /// An array of matched hashtags, as `Tag` objects. + pub hashtags: Vec, +} diff --git a/src/lib.rs b/src/lib.rs index 5e89975..c159f38 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -193,6 +193,10 @@ impl MastodonClient for Mastodon { (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, diff --git a/src/macros.rs b/src/macros.rs index 26e6ae3..1e743ee 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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", diff --git a/src/mastodon_client.rs b/src/mastodon_client.rs index b33dace..a7e96a8 100644 --- a/src/mastodon_client.rs +++ b/src/mastodon_client.rs @@ -95,6 +95,10 @@ pub trait MastodonClient { fn search<'a>(&self, q: &'a str, resolve: bool) -> Result { unimplemented!("This method was not implemented"); } + /// GET /api/v2/search + fn search_v2<'a>(&self, q: &'a str, resolve: bool) -> Result { + unimplemented!("This method was not implemented"); + } /// POST /api/v1/follows fn follows(&self, uri: Cow<'static, str>) -> Result { unimplemented!("This method was not implemented");