From 93a52a448bfd71f2e9ae2aef0731d76639463aef Mon Sep 17 00:00:00 2001 From: Joseph Holtkamp Date: Mon, 13 Mar 2023 15:13:35 -0500 Subject: [PATCH] Moved display logic to impl block and removed serde_json as a dependency --- Cargo.lock | 1 - Cargo.toml | 1 - src/main.rs | 42 +++++++++++++++++++++++++++++++----------- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0953c66..e1ab925 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1093,6 +1093,5 @@ version = "0.1.0" dependencies = [ "reqwest", "serde", - "serde_json", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index c906361..3036bd3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,4 @@ edition = "2021" [dependencies] reqwest = { version = "0.11", features = ["json"] } serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0.94" tokio = { version = "1.0", features = ["full"] } diff --git a/src/main.rs b/src/main.rs index 5c19071..7f08a0f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,24 +1,19 @@ use reqwest::Result; -use serde::{Deserialize, Serialize}; +use serde::Deserialize; -#[derive(Debug, Deserialize, Serialize)] +#[derive(Deserialize)] struct Definition { - source: String, - text: String, - note: Option, #[serde(rename(deserialize = "partOfSpeech"))] part_of_speech: String, + text: String, } -#[derive(Debug, Deserialize, Serialize)] +#[derive(Deserialize)] struct ExampleUsage { - url: String, - title: String, text: String, - id: i32, } -#[derive(Debug, Deserialize, Serialize)] +#[derive(Deserialize)] struct WordOfTheDay { definitions: Vec, examples: Vec, @@ -26,12 +21,37 @@ struct WordOfTheDay { word: String, } +impl WordOfTheDay { + fn display(&self) { + println!("Word of the Day:"); + println!(" {}\n", self.word); + + println!("Definitions:"); + for definition in self.definitions.iter() { + println!(" Part of speech: {}", definition.part_of_speech); + println!(" Definition:"); + println!(" {}\n", definition.text); + } + + println!("Examples:"); + for example in self.examples.iter() { + println!(" Example:"); + println!(" {}\n", example.text); + } + + println!("Note:"); + println!(" {}", self.note); + } +} + #[tokio::main] async fn main() -> Result<()> { let api_key = std::env::var("WORDNIK_API_KEY").unwrap(); let url = "https://api.wordnik.com/v4/words.json/wordOfTheDay?api_key=".to_owned() + &api_key; let word_of_the_day: WordOfTheDay = reqwest::Client::new().get(url).send().await?.json().await?; - println!("{:#?}", word_of_the_day); + + word_of_the_day.display(); + Ok(()) }