From 7deafd0f8139285b1f27a397910bbdbba887b577 Mon Sep 17 00:00:00 2001 From: Maverick Liu Date: Sun, 23 Feb 2025 17:50:20 +0800 Subject: [PATCH] adjust api --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 19 ++++++++----------- src/main.rs | 6 +++--- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 085b8a0..ed93dd9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -192,7 +192,7 @@ dependencies = [ [[package]] name = "epub2mdbook" -version = "0.11.0" +version = "0.12.0" dependencies = [ "clap", "epub", diff --git a/Cargo.toml b/Cargo.toml index dd85cde..218cda3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "epub2mdbook" -version = "0.11.0" +version = "0.12.0" edition = "2024" description = "A tool to convert EPUB files to MDBook format" authors = ["Maverick Liu "] diff --git a/src/lib.rs b/src/lib.rs index a93d772..3b883c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,15 +15,15 @@ use std::{fs, io}; /// # Arguments /// /// * `epub_path` - The path to the EPUB file -/// * `output_dir` - The path to the output directory, working directory by default +/// * `output_dir` - The path to the output directory /// * `with_file_name` - Whether to use the file name as the output directory -/// pub fn convert_epub_to_mdbook( epub_path: impl AsRef, - output_dir: Option>, + output_dir: impl AsRef, with_file_name: bool, ) -> Result<(), Error> { let epub_path = epub_path.as_ref(); + let output_dir = output_dir.as_ref(); if !epub_path.is_file() { return Err(Error::NotAFile(epub_path.display().to_string())); } @@ -33,13 +33,11 @@ pub fn convert_epub_to_mdbook( .expect("unreachable") .to_string_lossy() .to_string(); - let mut output_dir = match output_dir { - Some(output_dir) => output_dir.as_ref().to_owned(), - None => PathBuf::from("."), + let output_dir = if with_file_name { + output_dir.join(&book_name) + } else { + output_dir.to_owned() }; - if with_file_name { - output_dir.push(&book_name); - } fs::create_dir_all(output_dir.join("src"))?; let mut epub_doc = EpubDoc::new(epub_path)?; @@ -118,8 +116,7 @@ fn extract_chapters_and_resources( .iter() .filter_map(|(k, v)| Some((k.file_name()?, v.file_name()?))) .collect::>(); - let output_dir = output_dir.as_ref(); - let src_dir = output_dir.join("src"); + let src_dir = output_dir.as_ref().join("src"); for (_, (path, _)) in epub_doc.resources.clone().into_iter() { let mut content = match epub_doc.get_resource_by_path(&path) { Some(content) => content, diff --git a/src/main.rs b/src/main.rs index 0de6e25..d4b009b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,8 +9,8 @@ struct Args { #[clap(short, long)] input_epub: PathBuf, /// The path to the output directory, working directory by default - #[clap(short, long)] - output_dir: Option, + #[clap(short, long, default_value = ".")] + output_dir: PathBuf, } fn main() -> Result<(), Error> { @@ -18,4 +18,4 @@ fn main() -> Result<(), Error> { convert_epub_to_mdbook(args.input_epub, args.output_dir, true)?; println!("Conversion completed successfully!"); Ok(()) -} \ No newline at end of file +}