[refactor] extract and centralize output writing logic into write_outputs
function in output.rs
for improved code reuse and maintainability
This commit is contained in:
106
src/main.rs
106
src/main.rs
@@ -10,6 +10,9 @@ use clap::{Parser, Subcommand};
|
||||
use clap_complete::Shell;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
mod output;
|
||||
use output::{write_outputs, OutputFormats};
|
||||
|
||||
use std::sync::mpsc::channel;
|
||||
// whisper-rs is used from the library crate
|
||||
use polyscribe::backend::{BackendKind, select_backend};
|
||||
@@ -123,8 +126,8 @@ struct InputSegment {
|
||||
use polyscribe::{OutputEntry, date_prefix, models_dir_path, normalize_lang_code, render_srt};
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct OutputRoot {
|
||||
items: Vec<OutputEntry>,
|
||||
pub struct OutputRoot {
|
||||
pub items: Vec<OutputEntry>,
|
||||
}
|
||||
|
||||
fn sanitize_speaker_name(raw: &str) -> String {
|
||||
@@ -530,29 +533,8 @@ fn run() -> Result<()> {
|
||||
.unwrap_or("output");
|
||||
let date = date_prefix();
|
||||
let base_name = format!("{date}_{stem}");
|
||||
let json_path = out_dir.join(format!("{}.json", &base_name));
|
||||
let toml_path = out_dir.join(format!("{}.toml", &base_name));
|
||||
let srt_path = out_dir.join(format!("{}.srt", &base_name));
|
||||
|
||||
let mut json_file = File::create(&json_path).with_context(|| {
|
||||
format!("Failed to create output file: {}", json_path.display())
|
||||
})?;
|
||||
serde_json::to_writer_pretty(&mut json_file, &out)?;
|
||||
writeln!(&mut json_file)?;
|
||||
|
||||
let toml_str = toml::to_string_pretty(&out)?;
|
||||
let mut toml_file = File::create(&toml_path).with_context(|| {
|
||||
format!("Failed to create output file: {}", toml_path.display())
|
||||
})?;
|
||||
toml_file.write_all(toml_str.as_bytes())?;
|
||||
if !toml_str.ends_with('\n') {
|
||||
writeln!(&mut toml_file)?;
|
||||
}
|
||||
|
||||
let srt_str = render_srt(&out.items);
|
||||
let mut srt_file = File::create(&srt_path)
|
||||
.with_context(|| format!("Failed to create output file: {}", srt_path.display()))?;
|
||||
srt_file.write_all(srt_str.as_bytes())?;
|
||||
let base_path = out_dir.join(&base_name);
|
||||
write_outputs(&base_path, &out, &OutputFormats::all())?;
|
||||
|
||||
// Extend merged with per-file entries
|
||||
merged_entries.extend(out.items.into_iter());
|
||||
@@ -586,27 +568,8 @@ fn run() -> Result<()> {
|
||||
|
||||
let date = date_prefix();
|
||||
let merged_base = format!("{date}_merged");
|
||||
let m_json = out_dir.join(format!("{}.json", &merged_base));
|
||||
let m_toml = out_dir.join(format!("{}.toml", &merged_base));
|
||||
let m_srt = out_dir.join(format!("{}.srt", &merged_base));
|
||||
|
||||
let mut mj = File::create(&m_json)
|
||||
.with_context(|| format!("Failed to create output file: {}", m_json.display()))?;
|
||||
serde_json::to_writer_pretty(&mut mj, &merged_out)?;
|
||||
writeln!(&mut mj)?;
|
||||
|
||||
let m_toml_str = toml::to_string_pretty(&merged_out)?;
|
||||
let mut mt = File::create(&m_toml)
|
||||
.with_context(|| format!("Failed to create output file: {}", m_toml.display()))?;
|
||||
mt.write_all(m_toml_str.as_bytes())?;
|
||||
if !m_toml_str.ends_with('\n') {
|
||||
writeln!(&mut mt)?;
|
||||
}
|
||||
|
||||
let m_srt_str = render_srt(&merged_out.items);
|
||||
let mut ms = File::create(&m_srt)
|
||||
.with_context(|| format!("Failed to create output file: {}", m_srt.display()))?;
|
||||
ms.write_all(m_srt_str.as_bytes())?;
|
||||
let base_path = out_dir.join(&merged_base);
|
||||
write_outputs(&base_path, &merged_out, &OutputFormats::all())?;
|
||||
|
||||
// Final concise summary table to stderr (below progress bars)
|
||||
if !args.quiet && !summary.is_empty() {
|
||||
@@ -775,29 +738,8 @@ fn run() -> Result<()> {
|
||||
let date = date_prefix();
|
||||
let base_name = format!("{date}_{stem}");
|
||||
let dir = parent_opt.unwrap_or(Path::new(""));
|
||||
let json_path = dir.join(format!("{}.json", &base_name));
|
||||
let toml_path = dir.join(format!("{}.toml", &base_name));
|
||||
let srt_path = dir.join(format!("{}.srt", &base_name));
|
||||
|
||||
let mut json_file = File::create(&json_path).with_context(|| {
|
||||
format!("Failed to create output file: {}", json_path.display())
|
||||
})?;
|
||||
serde_json::to_writer_pretty(&mut json_file, &out)?;
|
||||
writeln!(&mut json_file)?;
|
||||
|
||||
let toml_str = toml::to_string_pretty(&out)?;
|
||||
let mut toml_file = File::create(&toml_path).with_context(|| {
|
||||
format!("Failed to create output file: {}", toml_path.display())
|
||||
})?;
|
||||
toml_file.write_all(toml_str.as_bytes())?;
|
||||
if !toml_str.ends_with('\n') {
|
||||
writeln!(&mut toml_file)?;
|
||||
}
|
||||
|
||||
let srt_str = render_srt(&out.items);
|
||||
let mut srt_file = File::create(&srt_path)
|
||||
.with_context(|| format!("Failed to create output file: {}", srt_path.display()))?;
|
||||
srt_file.write_all(srt_str.as_bytes())?;
|
||||
let base_path = dir.join(&base_name);
|
||||
write_outputs(&base_path, &out, &OutputFormats::all())?;
|
||||
} else {
|
||||
let stdout = io::stdout();
|
||||
let mut handle = stdout.lock();
|
||||
@@ -955,30 +897,8 @@ fn run() -> Result<()> {
|
||||
.unwrap_or("output");
|
||||
let date = date_prefix();
|
||||
let base_name = format!("{date}_{stem}");
|
||||
let json_path = dir.join(format!("{}.json", &base_name));
|
||||
let toml_path = dir.join(format!("{}.toml", &base_name));
|
||||
let srt_path = dir.join(format!("{}.srt", &base_name));
|
||||
|
||||
let mut json_file = File::create(&json_path).with_context(|| {
|
||||
format!("Failed to create output file: {}", json_path.display())
|
||||
})?;
|
||||
serde_json::to_writer_pretty(&mut json_file, &out)?;
|
||||
writeln!(&mut json_file)?;
|
||||
|
||||
let toml_str = toml::to_string_pretty(&out)?;
|
||||
let mut toml_file = File::create(&toml_path).with_context(|| {
|
||||
format!("Failed to create output file: {}", toml_path.display())
|
||||
})?;
|
||||
toml_file.write_all(toml_str.as_bytes())?;
|
||||
if !toml_str.ends_with('\n') {
|
||||
writeln!(&mut toml_file)?;
|
||||
}
|
||||
|
||||
let srt_str = render_srt(&out.items);
|
||||
let mut srt_file = File::create(&srt_path).with_context(|| {
|
||||
format!("Failed to create output file: {}", srt_path.display())
|
||||
})?;
|
||||
srt_file.write_all(srt_str.as_bytes())?;
|
||||
let base_path = dir.join(&base_name);
|
||||
write_outputs(&base_path, &out, &OutputFormats::all())?;
|
||||
} else {
|
||||
// stdout (only single input reaches here)
|
||||
let stdout = io::stdout();
|
||||
|
Reference in New Issue
Block a user