Skip to content

Commit

Permalink
Moved display logic to impl block and removed serde_json as a dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
holtkampjs committed Mar 13, 2023
1 parent da41cfc commit 93a52a4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
42 changes: 31 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,57 @@
use reqwest::Result;
use serde::{Deserialize, Serialize};
use serde::Deserialize;

#[derive(Debug, Deserialize, Serialize)]
#[derive(Deserialize)]
struct Definition {
source: String,
text: String,
note: Option<String>,
#[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<Definition>,
examples: Vec<ExampleUsage>,
note: String,
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(())
}

0 comments on commit 93a52a4

Please sign in to comment.