From 5c764185bb2118942bb0e52c66ef2872d3fd23d3 Mon Sep 17 00:00:00 2001 From: "D. Scott Boggs" Date: Wed, 28 Dec 2022 09:34:35 -0500 Subject: [PATCH] Rewrite awkward code --- examples/follows_me.rs | 2 +- examples/get_statuses.rs | 32 +++++++++++++++++++++ examples/post_status.rs | 2 +- src/entities/itemsiter.rs | 2 +- src/lib.rs | 6 +--- src/mastodon.rs | 15 +++++----- src/page.rs | 10 ++----- src/requests/statuses.rs | 59 +++++++++++++++++---------------------- 8 files changed, 71 insertions(+), 57 deletions(-) create mode 100644 examples/get_statuses.rs diff --git a/examples/follows_me.rs b/examples/follows_me.rs index be4b100..57c4463 100644 --- a/examples/follows_me.rs +++ b/examples/follows_me.rs @@ -25,6 +25,6 @@ async fn main() -> Result<()> { fn main() { println!( "examples require the `toml` feature, run this command for this example:\n\ncargo run \ - --example print_your_profile --features toml\n" + --example follows_me --features toml\n" ); } diff --git a/examples/get_statuses.rs b/examples/get_statuses.rs new file mode 100644 index 0000000..916fd44 --- /dev/null +++ b/examples/get_statuses.rs @@ -0,0 +1,32 @@ +#![cfg_attr(not(feature = "toml"), allow(dead_code))] +#![cfg_attr(not(feature = "toml"), allow(unused_imports))] +mod register; +use mastodon_async::Result; + +#[cfg(feature = "toml")] +#[tokio::main] +async fn main() -> Result<()> { + use futures_util::StreamExt; + use mastodon_async::StatusesRequest; + + let mut filters = StatusesRequest::new(); + filters.limit(3); + let mastodon = register::get_mastodon_data().await?; + let you = mastodon.verify_credentials().await?; + + mastodon + .statuses(&you.id, filters) + .await? + .items_iter() + .for_each(|status| async move { println!("{status:?}") }) + .await; + Ok(()) +} + +#[cfg(not(feature = "toml"))] +fn main() { + println!( + "examples require the `toml` feature, run this command for this example:\n\ncargo run \ + --example get_statuses --features toml\n" + ); +} diff --git a/examples/post_status.rs b/examples/post_status.rs index 187d143..43e1b28 100644 --- a/examples/post_status.rs +++ b/examples/post_status.rs @@ -12,7 +12,7 @@ async fn main() -> Result<()> { .status(register::read_line( "Enter a status to post privately (enter to send): ", )?) - .visibility(Visibility::Private) + .visibility(Visibility::Unlisted) .language(Language::Eng) .build()?; diff --git a/src/entities/itemsiter.rs b/src/entities/itemsiter.rs index 9769762..3511c43 100644 --- a/src/entities/itemsiter.rs +++ b/src/entities/itemsiter.rs @@ -14,7 +14,7 @@ use serde::{Deserialize, Serialize}; /// tokio_test::block_on(async { /// let data = Data::default(); /// let client = Mastodon::from(data); -/// let statuses = client.statuses("user-id", None).await.unwrap().items_iter(); +/// let statuses = client.statuses("user-id", Default::default()).await.unwrap().items_iter(); /// statuses.for_each(|status| async move { /// // Do something with the status /// }).await; diff --git a/src/lib.rs b/src/lib.rs index 76ea064..42ac7c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -94,11 +94,7 @@ pub use mastodon::{Mastodon, MastodonUnauthenticated}; // pub use mastodon_client::{MastodonClient, MastodonUnauthenticated}; pub use registration::Registration; pub use requests::{ - AddFilterRequest, - AddPushRequest, - StatusesRequest, - UpdateCredsRequest, - UpdatePushRequest, + AddFilterRequest, AddPushRequest, StatusesRequest, UpdateCredsRequest, UpdatePushRequest, }; pub use status_builder::{NewStatus, StatusBuilder, Visibility}; diff --git a/src/mastodon.rs b/src/mastodon.rs index a437867..68d9b02 100644 --- a/src/mastodon.rs +++ b/src/mastodon.rs @@ -225,7 +225,7 @@ impl Mastodon { /// tokio_test::block_on(async { /// let data = Data::default(); /// let client = Mastodon::from(data); - /// let statuses = client.statuses("user-id", None).await.unwrap(); + /// let statuses = client.statuses("user-id", Default::default()).await.unwrap(); /// }); /// ``` /// @@ -239,16 +239,15 @@ impl Mastodon { /// let statuses = client.statuses("user-id", request).await.unwrap(); /// }); /// ``` - pub async fn statuses<'a, 'b: 'a, S>(&'b self, id: &'b str, request: S) -> Result> - where - S: Into>>, - { + pub async fn statuses<'a, 'b: 'a>( + &'b self, + id: &'b str, + request: StatusesRequest<'a>, + ) -> Result> { let call_id = Uuid::new_v4(); let mut url = format!("{}/api/v1/accounts/{}/statuses", self.data.base, id); - if let Some(request) = request.into() { - url = format!("{}{}", url, request.to_querystring()?); - } + url += request.to_query_string()?.as_str(); debug!(url = url, method = stringify!($method), call_id = as_debug!(call_id); "making API request"); let response = self.client.get(&url).send().await?; diff --git a/src/page.rs b/src/page.rs index 755dfd4..82348c4 100644 --- a/src/page.rs +++ b/src/page.rs @@ -1,9 +1,6 @@ use super::{Mastodon, Result}; use crate::{ - entities::itemsiter::ItemsIter, - format_err, - helpers::read_response::read_response, - Error, + entities::itemsiter::ItemsIter, format_err, helpers::read_response::read_response, Error, }; use futures::Stream; use hyper_old_types::header::{parsing, Link, RelationType}; @@ -130,10 +127,7 @@ impl<'a, T: for<'de> Deserialize<'de> + Serialize> Page { }) } else { let response = response.json().await?; - Err(Error::Api { - status, - response, - }) + Err(Error::Api { status, response }) } } } diff --git a/src/requests/statuses.rs b/src/requests/statuses.rs index 61d6a8b..334a7b7 100644 --- a/src/requests/statuses.rs +++ b/src/requests/statuses.rs @@ -27,7 +27,7 @@ mod bool_qs_serialize { /// use mastodon_async::requests::StatusesRequest; /// let mut request = StatusesRequest::new(); /// request.only_media().pinned().since_id("foo"); -/// assert_eq!(&request.to_querystring().expect("Couldn't serialize qs")[..], "?only_media=1&pinned=1&since_id=foo"); +/// assert_eq!(&request.to_query_string().expect("Couldn't serialize qs")[..], "?only_media=1&pinned=1&since_id=foo"); /// ``` #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)] pub struct StatusesRequest<'a> { @@ -75,8 +75,9 @@ impl<'a> StatusesRequest<'a> { /// // Example /// /// ``` - /// let mut request = mastodon_async::requests::StatusesRequest::new(); - /// assert_eq!(&request.only_media().to_querystring().expect("Couldn't serialize qs"), "?only_media=1"); + /// use mastodon_async::requests::StatusesRequest; + /// let mut request = StatusesRequest::new(); + /// assert_eq!(&request.only_media().to_query_string().expect("Couldn't serialize qs"), "?only_media=1"); /// ``` pub fn only_media(&mut self) -> &mut Self { self.only_media = true; @@ -88,11 +89,12 @@ impl<'a> StatusesRequest<'a> { /// // Example /// /// ``` - /// let mut request = mastodon_async::requests::StatusesRequest::new(); + /// use mastodon_async::requests::StatusesRequest; + /// let mut request = StatusesRequest::new(); /// assert_eq!( /// &request /// .exclude_replies() - /// .to_querystring() + /// .to_query_string() /// .expect("Couldn't serialize qs"), /// "?exclude_replies=1" /// ); @@ -107,11 +109,12 @@ impl<'a> StatusesRequest<'a> { /// // Example /// /// ``` - /// let mut request = mastodon_async::requests::StatusesRequest::new(); + /// use mastodon_async::requests::StatusesRequest; + /// let mut request = StatusesRequest::new(); /// assert_eq!( /// &request /// .pinned() - /// .to_querystring() + /// .to_query_string() /// .expect("Couldn't serialize qs"), /// "?pinned=1" /// ); @@ -126,11 +129,12 @@ impl<'a> StatusesRequest<'a> { /// // Example /// /// ``` - /// let mut request = mastodon_async::requests::StatusesRequest::new(); + /// use mastodon_async::requests::StatusesRequest; + /// let mut request = StatusesRequest::new(); /// assert_eq!( /// &request /// .max_id("foo") - /// .to_querystring() + /// .to_query_string() /// .expect("Couldn't serialize qs"), /// "?max_id=foo" /// ); @@ -145,11 +149,12 @@ impl<'a> StatusesRequest<'a> { /// // Example /// /// ``` - /// let mut request = mastodon_async::requests::StatusesRequest::new(); + /// use mastodon_async::requests::StatusesRequest; + /// let mut request = StatusesRequest::new(); /// assert_eq!( /// &request /// .since_id("foo") - /// .to_querystring() + /// .to_query_string() /// .expect("Couldn't serialize qs"), /// "?since_id=foo" /// ); @@ -164,11 +169,12 @@ impl<'a> StatusesRequest<'a> { /// // Example /// /// ``` - /// let mut request = mastodon_async::requests::StatusesRequest::new(); + /// use mastodon_async::requests::StatusesRequest; + /// let mut request = StatusesRequest::new(); /// assert_eq!( /// &request /// .limit(10) - /// .to_querystring() + /// .to_query_string() /// .expect("Couldn't serialize qs"), /// "?limit=10" /// ); @@ -183,11 +189,12 @@ impl<'a> StatusesRequest<'a> { /// // Example /// /// ``` - /// let mut request = mastodon_async::requests::StatusesRequest::new(); + /// use mastodon_async::requests::StatusesRequest; + /// let mut request = StatusesRequest::new(); /// assert_eq!( /// &request /// .min_id("foobar") - /// .to_querystring() + /// .to_query_string() /// .expect("Couldn't serialize qs"), /// "?min_id=foobar" /// ); @@ -197,22 +204,8 @@ impl<'a> StatusesRequest<'a> { self } - /// Turns this builder into a querystring - /// - /// // Example - /// - /// ``` - /// let mut request = mastodon_async::requests::StatusesRequest::new(); - /// assert_eq!( - /// &request - /// .limit(10) - /// .pinned() - /// .to_querystring() - /// .expect("Couldn't serialize qs"), - /// "?pinned=1&limit=10" - /// ); - /// ``` - pub fn to_querystring(&self) -> Result { + /// Serialize into a query string + pub fn to_query_string(&self) -> Result { Ok(format!("?{}", serde_qs::to_string(&self)?)) } } @@ -359,13 +352,13 @@ mod tests { ); } #[test] - fn test_to_querystring() { + fn test_to_query_string() { macro_rules! qs_test { (|$r:ident| $b:block, $expected:expr) => { { let mut $r = StatusesRequest::new(); $b - let qs = $r.to_querystring().expect("Failed to serialize querystring"); + let qs = $r.to_query_string().expect("Failed to serialize querystring"); assert_eq!(&qs, $expected); } }