refactor(cli): split into multiple files

moved most code from main.rs to api.rs, cli.rs, constants.rs, and types.rs
This commit is contained in:
2026-08-30 16:00:42 -05:00
parent 5deb84668a
commit 9bb00530aa
5 changed files with 68 additions and 57 deletions
+12
View File
@@ -0,0 +1,12 @@
use crate::types;
pub async fn define(
word: &str,
language: &str,
) -> Result<types::DictionarySearchResult, reqwest::Error> {
let url = format!(
"https://freedictionaryapi.com/api/v1/entries/{}/{}",
language, word
);
reqwest::get(url).await?.json().await
}
+15
View File
@@ -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<String>,
#[arg(short, long, action = clap::ArgAction::SetTrue)]
pub examples: bool,
}
+13
View File
@@ -0,0 +1,13 @@
pub static POSSIBLE_PARTS_OF_SPEECH: &[&str] = &[
"noun",
"verb",
"adjective",
"adverb",
"pronoun",
"preposition",
"conjunction",
"interjection",
"exclamation",
"determiner",
"article",
];
+6 -57
View File
@@ -1,66 +1,15 @@
use clap::Parser; use clap::Parser;
use colored::Colorize; use colored::Colorize;
use serde::Deserialize;
static POSSIBLE_PARTS_OF_SPEECH: &[&str] = &[ mod api;
"noun", mod cli;
"verb", mod constants;
"adjective", mod types;
"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<String>,
#[arg(short, long, action = clap::ArgAction::SetTrue)]
examples: bool,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct DictionarySearchResult {
word: String,
entries: Vec<DictionaryEntry>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct DictionaryEntry {
part_of_speech: String,
senses: Vec<DictionarySense>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct DictionarySense {
definition: String,
examples: Vec<String>,
}
async fn define(word: &str, language: &str) -> Result<DictionarySearchResult, reqwest::Error> {
let url = format!(
"https://freedictionaryapi.com/api/v1/entries/{}/{}",
language, word
);
reqwest::get(url).await?.json().await
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let args = Args::parse(); let args = cli::Args::parse();
let result = define(&args.word, &args.language).await; let result = api::define(&args.word, &args.language).await;
if result.is_err() { if result.is_err() {
panic!("Error: {}", result.err().unwrap()); panic!("Error: {}", result.err().unwrap());
} }
+22
View File
@@ -0,0 +1,22 @@
use serde::Deserialize;
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DictionarySearchResult {
pub word: String,
pub entries: Vec<DictionaryEntry>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DictionaryEntry {
pub part_of_speech: String,
pub senses: Vec<DictionarySense>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DictionarySense {
pub definition: String,
pub examples: Vec<String>,
}