Skip to content

Commit 448a6a6

Browse files
committed
refactor: 重构 convert_with_cache 方法,使用结构体封装参数
- 新增 ConvertWithCacheOptions 结构体封装转换参数 - 解决 clippy::too_many_arguments 警告 - 改进代码可维护性和可读性 - 导出 ConvertWithCacheOptions 以便外部使用
1 parent cb07079 commit 448a6a6

3 files changed

Lines changed: 50 additions & 36 deletions

File tree

src/converter.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ use std::collections::HashMap;
77
use std::path::Path;
88
use std::sync::Arc;
99

10+
/// 带缓存转换的参数
11+
pub struct ConvertWithCacheOptions<'a> {
12+
pub input_path: &'a Path,
13+
pub input_identifier: &'a str,
14+
pub output_dir: &'a Path,
15+
pub config: &'a dyn ProviderConfig,
16+
pub provider_name: &'a str,
17+
pub page_ranges: Option<Vec<(u32, u32)>>,
18+
}
19+
1020
pub struct Converter {
1121
provider: Arc<dyn DocumentProvider>,
1222
cache_manager: Option<CacheManager>,
@@ -22,14 +32,17 @@ impl Converter {
2232
/// 带缓存的转换方法
2333
pub async fn convert_with_cache(
2434
&self,
25-
input_path: &Path,
26-
input_identifier: &str, // 原始输入路径或 URL
27-
output_dir: &Path,
28-
config: &dyn ProviderConfig,
29-
provider_name: &str,
30-
page_ranges: Option<Vec<(u32, u32)>>,
35+
options: ConvertWithCacheOptions<'_>,
3136
progress_cb: impl FnMut(ProgressUpdate) + Send + 'static,
3237
) -> Result<ParseResult> {
38+
let ConvertWithCacheOptions {
39+
input_path,
40+
input_identifier,
41+
output_dir,
42+
config,
43+
provider_name,
44+
page_ranges,
45+
} = options;
3346
// 首先尝试从缓存获取
3447
if let Some(cache_manager) = &self.cache_manager {
3548
// 计算哈希
@@ -64,7 +77,7 @@ impl Converter {
6477

6578
// 更新 Markdown 中的图片引用
6679
let mut markdown = result.markdown.clone();
67-
for (img_name, _) in &result.images {
80+
for img_name in result.images.keys() {
6881
let relative_path = format!("images/{}", img_name);
6982
let original_ref = img_name;
7083
markdown = markdown.replace(original_ref, &relative_path);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub mod provider;
1515
pub mod utils;
1616

1717
pub use cache::{CacheManager, CACHE_DISABLE_ENV_VAR};
18-
pub use converter::Converter;
18+
pub use converter::{ConvertWithCacheOptions, Converter};
1919
pub use error::{anyhow, Result};
2020
pub use provider::{DocumentProvider, ParseResult, ProgressUpdate, ProviderType, ZhipuModel};
2121
pub use utils::{download_pdf, is_url, normalize_arxiv_url, PdfMetadata};

src/main.rs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use clap::Parser;
44
use colored::Colorize;
55
use indicatif::{ProgressBar, ProgressStyle};
66
use pdf_to_markdown::cache::{CacheManager, CACHE_DISABLE_ENV_VAR};
7+
use pdf_to_markdown::converter::ConvertWithCacheOptions;
78
use pdf_to_markdown::error::{anyhow, Result};
89
use pdf_to_markdown::provider::traits::*;
910
use pdf_to_markdown::provider::ProviderType;
@@ -630,23 +631,23 @@ async fn handle_parse(
630631
poll_interval_secs: 3,
631632
page_ranges: page_ranges_clone.clone(),
632633
};
634+
let options = ConvertWithCacheOptions {
635+
input_path: &pdf_path,
636+
input_identifier: input,
637+
output_dir: &output_dir,
638+
config: &config,
639+
provider_name: &provider_str,
640+
page_ranges,
641+
};
633642
converter
634-
.convert_with_cache(
635-
&pdf_path,
636-
input,
637-
&output_dir,
638-
&config,
639-
&provider_str,
640-
page_ranges,
641-
move |update| {
642-
let pb = pb_clone.lock().unwrap();
643-
pb.set_message(update.message);
644-
if let Some(total) = update.total {
645-
pb.set_length(total);
646-
}
647-
pb.set_position(update.current);
648-
},
649-
)
643+
.convert_with_cache(options, move |update| {
644+
let pb = pb_clone.lock().unwrap();
645+
pb.set_message(update.message);
646+
if let Some(total) = update.total {
647+
pb.set_length(total);
648+
}
649+
pb.set_position(update.current);
650+
})
650651
.await
651652
}
652653
ProviderType::PaddleOcr => {
@@ -655,19 +656,19 @@ async fn handle_parse(
655656
// 使用默认配置:打开布局检查,关闭图片方向矫正和扭曲矫正
656657
..Default::default()
657658
};
659+
let options = ConvertWithCacheOptions {
660+
input_path: &pdf_path,
661+
input_identifier: input,
662+
output_dir: &output_dir,
663+
config: &config,
664+
provider_name: &provider_str,
665+
page_ranges,
666+
};
658667
converter
659-
.convert_with_cache(
660-
&pdf_path,
661-
input,
662-
&output_dir,
663-
&config,
664-
&provider_str,
665-
page_ranges,
666-
move |update| {
667-
let pb = pb_clone.lock().unwrap();
668-
pb.set_message(update.message);
669-
},
670-
)
668+
.convert_with_cache(options, move |update| {
669+
let pb = pb_clone.lock().unwrap();
670+
pb.set_message(update.message);
671+
})
671672
.await
672673
}
673674
}?;

0 commit comments

Comments
 (0)