diff --git a/src/api.rs b/src/api.rs new file mode 100644 index 0000000..24d7ff6 --- /dev/null +++ b/src/api.rs @@ -0,0 +1,12 @@ +use crate::types; + +pub async fn define( + word: &str, + language: &str, +) -> Result { + let url = format!( + "https://freedictionaryapi.com/api/v1/entries/{}/{}", + language, word + ); + reqwest::get(url).await?.json().await +} diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..ecdac75 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,15 @@ +use crate::constants; + +use clap::Parser; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +pub struct Args { + pub word: String, + #[arg(short, long, default_value = "en")] + pub language: String, + #[arg(short, long, default_values = constants::POSSIBLE_PARTS_OF_SPEECH)] + pub parts_of_speech: Vec, + #[arg(short, long, action = clap::ArgAction::SetTrue)] + pub examples: bool, +} diff --git a/src/constants.rs b/src/constants.rs new file mode 100644 index 0000000..849ea5b --- /dev/null +++ b/src/constants.rs @@ -0,0 +1,13 @@ +pub static POSSIBLE_PARTS_OF_SPEECH: &[&str] = &[ + "noun", + "verb", + "adjective", + "adverb", + "pronoun", + "preposition", + "conjunction", + "interjection", + "exclamation", + "determiner", + "article", +]; diff --git a/src/main.rs b/src/main.rs index 5a73661..90944ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,66 +1,15 @@ use clap::Parser; use colored::Colorize; -use serde::Deserialize; -static POSSIBLE_PARTS_OF_SPEECH: &[&str] = &[ - "noun", - "verb", - "adjective", - "adverb", - "pronoun", - "preposition", - "conjunction", - "interjection", - "exclamation", - "determiner", - "article", -]; - -#[derive(Parser, Debug)] -#[command(version, about, long_about = None)] -struct Args { - word: String, - #[arg(short, long, default_value = "en")] - language: String, - #[arg(short, long, default_values = POSSIBLE_PARTS_OF_SPEECH)] - parts_of_speech: Vec, - #[arg(short, long, action = clap::ArgAction::SetTrue)] - examples: bool, -} - -#[derive(Debug, Deserialize)] -#[serde(rename_all = "camelCase")] -struct DictionarySearchResult { - word: String, - entries: Vec, -} - -#[derive(Debug, Deserialize)] -#[serde(rename_all = "camelCase")] -struct DictionaryEntry { - part_of_speech: String, - senses: Vec, -} - -#[derive(Debug, Deserialize)] -#[serde(rename_all = "camelCase")] -struct DictionarySense { - definition: String, - examples: Vec, -} - -async fn define(word: &str, language: &str) -> Result { - let url = format!( - "https://freedictionaryapi.com/api/v1/entries/{}/{}", - language, word - ); - reqwest::get(url).await?.json().await -} +mod api; +mod cli; +mod constants; +mod types; #[tokio::main] async fn main() { - let args = Args::parse(); - let result = define(&args.word, &args.language).await; + let args = cli::Args::parse(); + let result = api::define(&args.word, &args.language).await; if result.is_err() { panic!("Error: {}", result.err().unwrap()); } diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 0000000..86a62fa --- /dev/null +++ b/src/types.rs @@ -0,0 +1,22 @@ +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct DictionarySearchResult { + pub word: String, + pub entries: Vec, +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct DictionaryEntry { + pub part_of_speech: String, + pub senses: Vec, +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct DictionarySense { + pub definition: String, + pub examples: Vec, +}