From c6885ae2312a1be589e945129bdd9d268f14c370 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sun, 21 Jun 2026 22:45:28 +0800 Subject: feat: replace `--pinyin` flag with generic `--post="+pinyin"` option --- src/post_proc/pinyin.rs | 31 +++++++++++++++++++++++++++++++ src/post_proc/trait.rs | 12 ++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/post_proc/pinyin.rs create mode 100644 src/post_proc/trait.rs (limited to 'src/post_proc') diff --git a/src/post_proc/pinyin.rs b/src/post_proc/pinyin.rs new file mode 100644 index 0000000..c06b040 --- /dev/null +++ b/src/post_proc/pinyin.rs @@ -0,0 +1,31 @@ +use crate::post_proc::TextPostProcesser; + +/// Converts Chinese characters to pinyin (romanized phonetic representation). +/// +/// # Styles +/// - `plain` — ni hao (default) +/// - `tone` — nǐ hǎo (with tone marks, requires `with_tone` feature) +/// - `tone-num` — ni3 hao3 (tone number at end) +/// - `first-letter` — n h (first letter only) +pub struct PinyinPostProcesser; + +impl TextPostProcesser for PinyinPostProcesser { + fn processer_name() -> &'static str { + "pinyin" + } + + fn process(param: &[&str], text: &str) -> String { + let style = param.first().copied().unwrap_or("plain"); + let result: Vec<&'static str> = match style { + "tone" => pinyin::to_pinyin_vec(text, pinyin::Pinyin::with_tone), + "tone-num" | "tone_num" => { + pinyin::to_pinyin_vec(text, pinyin::Pinyin::with_tone_num_end) + } + "first-letter" | "first_letter" => { + pinyin::to_pinyin_vec(text, pinyin::Pinyin::first_letter) + } + _ => pinyin::to_pinyin_vec(text, pinyin::Pinyin::plain), + }; + result.join(" ") + } +} diff --git a/src/post_proc/trait.rs b/src/post_proc/trait.rs new file mode 100644 index 0000000..7fced5b --- /dev/null +++ b/src/post_proc/trait.rs @@ -0,0 +1,12 @@ +/// A text post-processor that transforms transcribed text. +pub trait TextPostProcesser { + /// Name used in `--post="+name(args)"`. + fn processer_name() -> &'static str; + + /// Execute the post-processing. + /// + /// # Arguments + /// * `param` — arguments parsed from `--post="+name(arg1, arg2)"` + /// * `text` — the transcribed text to process + fn process(param: &[&str], text: &str) -> String; +} -- cgit