booksniff, and one place for the parts every tool shares

This commit is contained in:
funkpower
2026-09-19 12:40:20 -04:00
parent 36afe3350b
commit 1071f0bed3
32 changed files with 1426 additions and 237 deletions
+9 -37
View File
@@ -1,6 +1,5 @@
//! Packs cues into passages worth embedding.
use std::io::{BufWriter, Read, Write};
use std::path::PathBuf;
use std::process::ExitCode;
@@ -38,7 +37,10 @@ struct Args {
}
fn main() -> ExitCode {
let args = Args::parse();
let args: Args = match catena_cli::parse() {
Ok(args) => args,
Err(code) => return code,
};
let budgets = Budgets {
seconds: args.seconds,
max_tokens: args.max_tokens,
@@ -48,11 +50,11 @@ fn main() -> ExitCode {
return ExitCode::from(exit::USAGE);
}
let content = match read(args.path.as_deref()) {
let content = match catena_cli::read_lossy(args.path.as_deref()) {
Ok(content) => content,
Err(e) => {
eprintln!("cuepack: {e}");
return ExitCode::from(exit::IO);
Err((code, why)) => {
eprintln!("cuepack: {why}");
return ExitCode::from(code);
}
};
@@ -64,25 +66,7 @@ fn main() -> ExitCode {
}
};
let mut out = BufWriter::new(std::io::stdout().lock());
for passage in cuepack::pack(&cues, &budgets) {
if let Err(e) = ndjson::write_line(&mut out, &passage) {
// A consumer that stopped reading is not a failure.
if e.is_broken_pipe() {
return ExitCode::from(exit::OK);
}
eprintln!("cuepack: {e}");
return ExitCode::from(exit::IO);
}
}
match out.flush() {
Ok(()) => ExitCode::from(exit::OK),
Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => ExitCode::from(exit::OK),
Err(e) => {
eprintln!("cuepack: {e}");
ExitCode::from(exit::IO)
}
}
catena_cli::stream("cuepack", &cuepack::pack(&cues, &budgets))
}
/// Read cues from either encoding: a cue stream begins with a JSON object
@@ -97,15 +81,3 @@ fn cues(content: &str) -> Result<Vec<Cue>, ndjson::Error> {
}
Ok(cuekit::parse(content))
}
fn read(path: Option<&std::path::Path>) -> std::io::Result<String> {
let bytes = match path {
Some(path) => std::fs::read(path)?,
None => {
let mut bytes = Vec::new();
std::io::stdin().read_to_end(&mut bytes)?;
bytes
}
};
Ok(String::from_utf8_lossy(&bytes).into_owned())
}