Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
shigedangao committed Feb 26, 2024
1 parent 4b330eb commit ba8e39b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 22 deletions.
22 changes: 22 additions & 0 deletions nado/src/cmd/download.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use super::DownloadArgs;
use super::CommandRunner;

const URL: &str = "https://www.mdbg.net/chinese/dictionary?page=cc-cedict#:~:text=cedict_1_0_ts_utf%2D8_mdbg.zip";

pub struct Downloader {
args: DownloadArgs
}

impl Downloader {
fn new(args: DownloadArgs) -> Self {
Downloader { args }
}
}

impl CommandRunner for Downloader {
async fn run(&self) -> anyhow::Result<()> {
let res = reqwest::get(URL).await?;

Ok(())
}
}
22 changes: 15 additions & 7 deletions nado/src/cmd/generate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{CliArgs, CommandRunner, OutputFormat};
use super::{GenerateArgs, CommandRunner, OutputFormat};
use crate::hsk;
use crate::progress::ProgressBuilder;
use crate::{hsk::HSKLevel, util};
Expand All @@ -19,7 +19,9 @@ const CSV_HEADERS: [&str; 8] = [
];

#[derive(Debug)]
pub struct Gen;
pub struct Gen {
args: GenerateArgs
}

#[derive(Debug, Default, Clone, Serialize)]
pub struct CedictItem {
Expand All @@ -31,9 +33,15 @@ pub struct CedictItem {
pub hsk_level: Option<HSKLevel>,
}

impl Gen {
pub fn new(args: GenerateArgs) -> Self {
Self { args }
}
}

impl CommandRunner for Gen {
async fn run(args: &CliArgs) -> Result<()> {
let path = PathBuf::from(&args.file_path);
async fn run(&self) -> Result<()> {
let path = PathBuf::from(&self.args.file_path);

println!("📖 - Loading cedict dictionary");
// Load the Cedict dictionary
Expand Down Expand Up @@ -75,15 +83,15 @@ impl CommandRunner for Gen {

println!(
"🖊️ - Generating target file with the path {}",
args.output_path
self.args.output_path
);

let output = match args.output_format {
let output = match self.args.output_format {
OutputFormat::Json => serde_json::to_string(&items)?,
OutputFormat::Csv => util::as_csv_string(&items, Some(CSV_HEADERS.to_vec()))?,
};

std::fs::write(&args.output_path, output)?;
std::fs::write(&self.args.output_path, output)?;

Ok(())
}
Expand Down
50 changes: 35 additions & 15 deletions nado/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,11 @@ use anyhow::Result;
use clap::{Parser, ValueEnum};

mod generate;
mod download;

/// Command Runner run the command on the given Arguments
trait CommandRunner {
async fn run(args: &CliArgs) -> Result<()>;
}

#[derive(Parser)]
#[command(name = "nomnom")]
enum Command {
Generate(CliArgs),
}

#[derive(Debug, ValueEnum, Clone)]
enum OutputFormat {
Json,
Csv,
async fn run(&self) -> Result<()>;
}

#[derive(clap::Args)]
Expand All @@ -26,7 +16,9 @@ enum OutputFormat {
about = "generate cedict.u8 into your desired format output",
long_about = None
)]
struct CliArgs {

#[derive(Debug)]
struct GenerateArgs {
#[clap(short = 'e', long, value_parser)]
file_path: String,

Expand All @@ -37,12 +29,40 @@ struct CliArgs {
output_format: OutputFormat,
}

#[derive(clap::Args)]
#[command(
author = "shigedangao",
version = "0.3.0",
about = "generate cedict.u8 into your desired format output",
long_about = None
)]

#[derive(Debug)]
struct DownloadArgs {
#[clap(long, value_parser)]
output_path: String
}

#[derive(Parser)]
#[command(name = "nomnom")]
enum Command {
Generate(GenerateArgs),
Download(DownloadArgs)
}

#[derive(Debug, ValueEnum, Clone)]
enum OutputFormat {
Json,
Csv,
}

/// Run the command
pub async fn run() -> Result<()> {
let cmd = Command::parse();

match cmd {
Command::Generate(args) => generate::Gen::run(&args).await?,
Command::Generate(args) => generate::Gen::new(args).run().await?,
Command::Download(args) => {}
};

Ok(())
Expand Down

0 comments on commit ba8e39b

Please sign in to comment.