-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved display logic to impl block and removed serde_json as a dependency
- Loading branch information
1 parent
da41cfc
commit 93a52a4
Showing
3 changed files
with
31 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |