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:
+12
@@ -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
@@ -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,
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
pub static POSSIBLE_PARTS_OF_SPEECH: &[&str] = &[
|
||||
"noun",
|
||||
"verb",
|
||||
"adjective",
|
||||
"adverb",
|
||||
"pronoun",
|
||||
"preposition",
|
||||
"conjunction",
|
||||
"interjection",
|
||||
"exclamation",
|
||||
"determiner",
|
||||
"article",
|
||||
];
|
||||
+6
-57
@@ -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<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
|
||||
}
|
||||
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());
|
||||
}
|
||||
|
||||
@@ -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>,
|
||||
}
|
||||
Reference in New Issue
Block a user