Skip to content

Commit

Permalink
Moved struct logic into its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
holtkampjs committed Mar 15, 2023
1 parent 93a52a4 commit 31e44ff
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 43 deletions.
45 changes: 2 additions & 43 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,7 @@
use reqwest::Result;
use serde::Deserialize;
use word_of_the_day::WordOfTheDay;

#[derive(Deserialize)]
struct Definition {
#[serde(rename(deserialize = "partOfSpeech"))]
part_of_speech: String,
text: String,
}

#[derive(Deserialize)]
struct ExampleUsage {
text: String,
}

#[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);
}
}
pub mod word_of_the_day;

#[tokio::main]
async fn main() -> Result<()> {
Expand Down
44 changes: 44 additions & 0 deletions src/word_of_the_day.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use serde::Deserialize;

#[derive(Deserialize)]
struct Definition {
#[serde(rename(deserialize = "partOfSpeech"))]
part_of_speech: String,
text: String,
}

#[derive(Deserialize)]
struct ExampleUsage {
text: String,
}

#[derive(Deserialize)]
pub(crate) struct WordOfTheDay {
definitions: Vec<Definition>,
examples: Vec<ExampleUsage>,
note: String,
word: String,
}

impl WordOfTheDay {
pub 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);
}
}

0 comments on commit 31e44ff

Please sign in to comment.