summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorD. Scott Boggs <scott@tams.tech>2023-09-30 10:30:27 -0400
committerD. Scott Boggs <scott@tams.tech>2023-09-30 10:30:27 -0400
commite834bed3e7b7ffde19bf5270466f421222d8823c (patch)
tree6e703cd172c76abb1911af4f1be4143690fc7a7c
parent70b0e0b4b0f470a4e5b935e2b77be48a2c144e77 (diff)
Revise account search method
-rw-r--r--entities/src/forms/account/mod.rs2
-rw-r--r--entities/src/forms/account/search.rs34
-rw-r--r--src/macros.rs26
-rw-r--r--src/mastodon.rs2
4 files changed, 63 insertions, 1 deletions
diff --git a/entities/src/forms/account/mod.rs b/entities/src/forms/account/mod.rs
index 58a0a9e..62fbad8 100644
--- a/entities/src/forms/account/mod.rs
+++ b/entities/src/forms/account/mod.rs
@@ -1,7 +1,9 @@
mod creation;
mod follow_options;
mod id_list;
+mod search;
pub use creation::Creation;
pub use follow_options::FollowOptions;
pub use id_list::IdList;
+pub use search::Search;
diff --git a/entities/src/forms/account/search.rs b/entities/src/forms/account/search.rs
new file mode 100644
index 0000000..552ff63
--- /dev/null
+++ b/entities/src/forms/account/search.rs
@@ -0,0 +1,34 @@
+use crate::forms::helpers::bool_qs;
+use derive_builder::Builder;
+use serde::Serialize;
+
+#[derive(Debug, Clone, Serialize, Builder)]
+#[builder(
+ custom_constructor,
+ derive(Debug, PartialEq),
+ build_fn(error = "crate::Error", private, name = "try_build")
+)]
+pub struct Search {
+ #[builder(private)]
+ q: String,
+ #[serde(skip_serializing_if = "bool_qs::is_false")]
+ #[builder(default)]
+ following: bool,
+ #[serde(skip_serializing_if = "bool_qs::is_false")]
+ #[builder(default)]
+ resolve: bool,
+}
+
+impl SearchBuilder {
+ pub fn build(&self) -> Search {
+ self.try_build().expect("required fields should be set")
+ }
+}
+
+impl Search {
+ pub fn builder(q: impl Into<String>) -> SearchBuilder {
+ let mut b = SearchBuilder::create_empty();
+ b.q(q.into());
+ b
+ }
+}
diff --git a/src/macros.rs b/src/macros.rs
index 7861d0f..a823add 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -64,6 +64,32 @@ macro_rules! paged_routes {
paged_routes!{$($rest)*}
};
+ ((get::<$form_type:ty>) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => {
+ doc_comment! {
+ concat!(
+ "Equivalent to `get /api/v1/",
+ $url,
+ "`\n# Errors\nIf `access_token` is not set."
+ ),
+ pub async fn $name<'a>(&self, form: &$form_type) -> Result<Page<$ret>> {
+ use tracing::debug;
+
+ let call_id = uuid::Uuid::new_v4();
+
+ let qs = serde_qs::to_string(&form)?;
+
+ let url = format!(concat!("/api/v1/", $url, "?{}"), &qs);
+ let url = self.route(url);
+
+ debug!(url, method = "get", ?call_id, "making API request");
+
+ let response = self.authenticated(self.client.get(&url)).header("Accept", "application/json").send().await?;
+
+ Page::new(self.clone(), response, call_id).await
+ }
+ }
+ paged_routes!{$($rest)*}
+ };
((get ($($(#[$m:meta])* $param:ident: $typ:ty,)*)) $name:ident: $url:expr => $ret:ty, $($rest:tt)*) => {
doc_comment! {
concat!(
diff --git a/src/mastodon.rs b/src/mastodon.rs
index a689dc1..5187516 100644
--- a/src/mastodon.rs
+++ b/src/mastodon.rs
@@ -66,7 +66,7 @@ impl Mastodon {
(get) instance_activity: "instance/activity" => instance::Activity,
(get) instance_rules: "instance/rules" => instance::Rule,
(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,
+ (get::<forms::account::Search>) search_accounts: "accounts/search" => Account,
(get) get_endorsements: "endorsements" => Account,
}