From 05ade8370d71e325d72c863fdcbbc352531f6518 Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Tue, 13 Feb 2024 23:42:55 +0100 Subject: [PATCH] Implement user score --- youmubot-osu/src/lib.rs | 4 +--- youmubot-osu/src/request.rs | 31 ++++++++++++++++++------------- youmubot-prelude/src/lib.rs | 2 +- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/youmubot-osu/src/lib.rs b/youmubot-osu/src/lib.rs index 7c7fc94..5d97321 100644 --- a/youmubot-osu/src/lib.rs +++ b/youmubot-osu/src/lib.rs @@ -117,8 +117,6 @@ impl Client { ) -> Result, Error> { let mut r = UserScoreRequestBuilder::new(u, user); f(&mut r); - let res: Vec = r.build(self).await?.json().await?; - let res = vec_try_into(res)?; - Ok(res) + r.build(self).await } } diff --git a/youmubot-osu/src/request.rs b/youmubot-osu/src/request.rs index 0ac2f87..dbc1626 100644 --- a/youmubot-osu/src/request.rs +++ b/youmubot-osu/src/request.rs @@ -288,7 +288,7 @@ pub mod builders { r.await } })? - .unwrap_or(vec![]); + .ok_or_else(|| error!("beatmap or user not found"))?; Ok(scores.into_iter().map(|v| v.into()).collect()) } } @@ -325,18 +325,23 @@ pub mod builders { self } - pub(crate) async fn build(&self, client: &Client) -> Result { - Ok(client - .build_request(match self.score_type { - UserScoreType::Best => "https://osu.ppy.sh/api/get_user_best", - UserScoreType::Recent => "https://osu.ppy.sh/api/get_user_recent", - }) - .await? - .query(&self.user.to_query()) - .query(&self.mode.to_query()) - .query(&self.limit.map(|v| ("limit", v.to_string())).to_query()) - .send() - .await?) + pub(crate) async fn build(self, client: &Client) -> Result> { + let scores = handle_not_found({ + let mut r = client.rosu.user_scores(self.user); + r = match self.score_type { + UserScoreType::Recent => r.recent(), + UserScoreType::Best => r.best(), + }; + if let Some(mode) = self.mode { + r = r.mode(mode.into()); + } + if let Some(limit) = self.limit { + r = r.limit(limit as usize); + } + r.await + })? + .ok_or_else(|| error!("user not found"))?; + Ok(scores.into_iter().map(|v| v.into()).collect()) } } } diff --git a/youmubot-prelude/src/lib.rs b/youmubot-prelude/src/lib.rs index 6ea52a6..ea555b1 100644 --- a/youmubot-prelude/src/lib.rs +++ b/youmubot-prelude/src/lib.rs @@ -23,7 +23,7 @@ pub use pagination::{paginate, paginate_fn, paginate_reply, paginate_reply_fn, P pub use async_trait::async_trait; /// Re-export the anyhow errors -pub use anyhow::{Error, Result}; +pub use anyhow::{anyhow as error, bail, Error, Result}; pub use debugging_ok::OkPrint; /// Re-export useful future and stream utils