From b8bbceb00be3ba01cd14a111f4dfc407880f493e Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Wed, 15 Feb 2023 18:08:23 +0000 Subject: refactor rust into separate files and add proper error handling --- src/error.rs | 29 ++++++++++++++ src/main.rs | 113 ++++++++++++------------------------------------------- src/scrobbles.rs | 55 +++++++++++++++++++++++++++ src/skweets.rs | 10 +++++ 4 files changed, 118 insertions(+), 89 deletions(-) create mode 100644 src/error.rs create mode 100644 src/scrobbles.rs create mode 100644 src/skweets.rs (limited to 'src') diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..38802d3 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,29 @@ +use rocket::Responder; + +#[derive(Responder, Debug)] +pub enum BlossomError { + #[response(status = 500)] + Reqwest(&'static str, #[response(ignore)] reqwest::Error), + #[response(status = 500)] + ListenBrainz(&'static str, #[response(ignore)] listenbrainz::Error), + #[response(status = 500)] + Skinnyverse(&'static str, #[response(ignore)] mastodon_async::Error), +} + +impl From for BlossomError { + fn from(e: reqwest::Error) -> Self { + BlossomError::Reqwest("reqwest error", e) + } +} + +impl From for BlossomError { + fn from(e: listenbrainz::Error) -> Self { + BlossomError::ListenBrainz("listenbrainz error", e) + } +} + +impl From for BlossomError { + fn from(e: mastodon_async::Error) -> Self { + BlossomError::Skinnyverse("skinnyverse error", e) + } +} diff --git a/src/main.rs b/src/main.rs index 1a2aab7..ba7bed0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,82 +1,27 @@ -use std::borrow::Cow; - -use listenbrainz::raw::response::UserPlayingNowResponse; -use mastodon_async::{Data, Mastodon}; use rocket::fs::{relative, FileServer}; use rocket::http::Status; -use rocket::response::Responder; use rocket::Request; +use rocket::State; use rocket_dyn_templates::{context, Template}; -use serde::{Deserialize, Serialize}; +use std::borrow::Cow; -#[macro_use] -extern crate rocket; +mod scrobbles; +mod skweets; -#[derive(Serialize, Deserialize)] -struct ScrobbleData { - is_scrobbling: bool, - song: Option, - artist: Option, +struct Clients { + listenbrainz: listenbrainz::raw::Client, + skinnyverse: mastodon_async::Mastodon, } -impl ScrobbleData { - fn new(playingnow: UserPlayingNowResponse) -> Self { - let is_scrobbling = playingnow.payload.count > 0; - if is_scrobbling { - Self { - is_scrobbling, - song: Some( - playingnow - .payload - .listens - .first() - .unwrap() - .track_metadata - .track_name - .clone(), - ), - artist: Some( - playingnow - .payload - .listens - .first() - .unwrap() - .track_metadata - .artist_name - .clone(), - ), - } - } else { - Self { - is_scrobbling, - song: None, - artist: None, - } - } - } -} +#[macro_use] +extern crate rocket; #[get("/")] -async fn home() -> Template { - let listenbrainz = listenbrainz::raw::Client::new(); - let playingnow = listenbrainz.user_playing_now("celblossom").unwrap(); - let listenbrainz_data = ScrobbleData::new(playingnow); - - let mut skinny_data = Data::default(); - skinny_data.base = Cow::from("https://skinnyver.se"); - let skinny_client = Mastodon::from(skinny_data); - let mut skweets = skinny_client - .statuses("cel", Default::default()) - .await - .unwrap() - .initial_items; - - skweets.truncate(6); - - Template::render( +async fn home(clients: &State) -> Result { + Ok(Template::render( "home", - context! { is_live: false, listenbrainz: listenbrainz_data, skweets: skweets }, - ) + context! { is_live: false, listenbrainz: scrobbles::get_now_playing(&clients.listenbrainz).await?, skweets: skweets::get_recents(&clients.skinnyverse).await? }, + )) } #[get("/contact")] @@ -89,16 +34,8 @@ async fn plants() -> Result { todo!() } -// #[get("/test")] -// async fn test() -> Result { -// let posts = reqwest::get("https://skinnyver.se/api/v1/accounts/cel/statuses") -// .await? -// .json() -// .await?; -// } - #[catch(default)] -fn error(status: Status, req: &Request) -> Template { +fn catcher(status: Status, req: &Request) -> Template { let message; if status.code == 404 { message = "i either haven't built this page yet or it looks like you're a little lost"; @@ -118,12 +55,19 @@ fn error(status: Status, req: &Request) -> Template { #[tokio::main] async fn main() -> Result<(), rocket::Error> { + let mut skinny_data = mastodon_async::Data::default(); + skinny_data.base = Cow::from("https://skinnyver.se"); + let _rocket = rocket::build() + .manage(Clients { + listenbrainz: listenbrainz::raw::Client::new(), + skinnyverse: mastodon_async::Mastodon::from(skinny_data), + }) .attach(Template::custom(|engines| { engines.tera.autoescape_on(vec![]); })) .mount("/", routes![home, contact]) - .register("/", catchers![error]) + .register("/", catchers![catcher]) .mount("/", FileServer::from(relative!("static"))) .launch() .await?; @@ -131,14 +75,5 @@ async fn main() -> Result<(), rocket::Error> { Ok(()) } -#[derive(Responder)] -enum BlossomError { - #[response(status = 500)] - Reqwest(&'static str, #[response(ignore)] reqwest::Error), -} - -impl From for BlossomError { - fn from(e: reqwest::Error) -> Self { - BlossomError::Reqwest("reqwest error", e) - } -} +mod error; +use error::BlossomError; diff --git a/src/scrobbles.rs b/src/scrobbles.rs new file mode 100644 index 0000000..07b1ad1 --- /dev/null +++ b/src/scrobbles.rs @@ -0,0 +1,55 @@ +use listenbrainz::raw::response::UserPlayingNowResponse; +use serde::{Deserialize, Serialize}; + +use crate::error::BlossomError; + +pub async fn get_now_playing( + client: &listenbrainz::raw::Client, +) -> Result { + let playingnow = client.user_playing_now("celblossom")?; + Ok(NowPlayingData::new(playingnow)) +} + +#[derive(Serialize, Deserialize)] +pub struct NowPlayingData { + pub is_scrobbling: bool, + pub song: Option, + pub artist: Option, +} + +impl NowPlayingData { + fn new(playingnow: UserPlayingNowResponse) -> Self { + let is_scrobbling = playingnow.payload.count > 0; + if is_scrobbling { + Self { + is_scrobbling, + song: Some( + playingnow + .payload + .listens + .first() + .unwrap() + .track_metadata + .track_name + .clone(), + ), + artist: Some( + playingnow + .payload + .listens + .first() + .unwrap() + .track_metadata + .artist_name + .clone(), + ), + } + } else { + Self { + is_scrobbling, + song: None, + artist: None, + } + } + } +} diff --git a/src/skweets.rs b/src/skweets.rs new file mode 100644 index 0000000..91369ec --- /dev/null +++ b/src/skweets.rs @@ -0,0 +1,10 @@ +use crate::error::BlossomError; +use mastodon_async::entities::status::Status; +use mastodon_async::prelude::*; + +pub async fn get_recents(client: &Mastodon) -> Result, BlossomError> { + Ok(client + .statuses("cel", Default::default()) + .await? + .initial_items) +} -- cgit