diff --git a/src/helper.rs b/src/helper.rs index bfa7c51..b98dbf8 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -1,11 +1,10 @@ use lemmy_api_common::{ - community::GetCommunity, + community::{GetCommunity, GetCommunityResponse}, lemmy_db_schema::newtypes::CommunityId, person::{Login, LoginResponse}, sensitive::Sensitive, }; use reqwest::Client; -use serde_json::Value; use std::fmt; static API_VERSION: i32 = 3; @@ -31,17 +30,13 @@ pub fn api_url(suffix: &str) -> String { } #[derive(Debug)] -pub enum WordleError { - ParseFailed, - LoginFailed, - NoNewPostId, -} +struct LoginFailedError; -impl std::error::Error for WordleError {} +impl std::error::Error for LoginFailedError {} -impl fmt::Display for WordleError { +impl fmt::Display for LoginFailedError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Oh no, something bad went down") + write!(f, "Login failed.") } } @@ -59,19 +54,8 @@ pub async fn get_community_id( .query(¶ms) .send() .await?; - let text = response.text().await?; - - // Deserializing from GetCommunityResponse would fail with missing field `followers_url` in Community - let data = serde_json::from_str::(text.as_str())?; - - if let Some(id) = data["community_view"]["community"] - .get("id") - .and_then(|v| v.as_i64()) - { - return Ok(CommunityId(i32::try_from(id)?)); - } - - Err(Box::new(WordleError::ParseFailed)) + let data = response.json::().await?; + Ok(data.community_view.community.id) } pub async fn lemmy_login(client: &Client) -> Result, Box> { @@ -95,5 +79,5 @@ pub async fn lemmy_login(client: &Client) -> Result, Box Result().await?; println!("{:?}", data); Ok(data) } -async fn post_to_lemmy(data: &WordleData) -> Result<(), Box> { +async fn post_to_lemmy(data: &WordleData, client: &Client, auth: Sensitive) -> Result> { println!("Posting to lemmy: {:?}", data); - let client = Client::new(); - - let auth = lemmy_login(&client).await?; - // Get the community id - let community_id = get_community_id(&client, lemmy_community()).await?; + let community_id = get_community_id(client, lemmy_community()).await?; let now = Utc::now(); @@ -61,7 +55,7 @@ async fn post_to_lemmy(data: &WordleData) -> Result<(), Box Result<(), Box().await?; - - // Again, deserializing using the api struct did not work due to missing fields - if let Some(new_id) = data["post_view"]["post"].get("id") { - println!("New post id: {}", new_id); - } else { - return Err(Box::new(WordleError::NoNewPostId)); - } - Ok(()) + let data = response.json::().await?; + Ok(data.post_view.post.id) } #[tokio::main] async fn main() -> Result<(), Box> { println!("A wordle a day keeps the doctor away!"); + + let client = Client::new(); + let auth = lemmy_login(&client).await?; let data = get_current_nyt_wordle_data().await?; - post_to_lemmy(&data).await?; + let id = post_to_lemmy(&data, &client, auth).await?; + println!("New post ID: {}", id); Ok(()) }