mirror of
https://github.com/storytold/deckcraft.git
synced 2026-10-26 19:46:58 -06:00
CLI: convert and run refuse output formats DeckCraft can't write (#715)
convert, run --save and run --export picked the format from the output extension and fell back to native .deckcraft bytes for any extension they didn't know, so out.odp, out.gif, out.svg and out.outline were zips with exit status 0. They now fail before writing anything, with the extension and the supported ones in the error.
This commit is contained in:
@@ -240,9 +240,9 @@ fn render_transition(args: &[String]) -> Result<(), String> {
|
||||
|
||||
fn convert(args: &[String]) -> Result<(), String> {
|
||||
let (Some(input), Some(out)) = (args.first(), args.get(1)) else { return Err("convert needs IN and OUT".into()) };
|
||||
let fmt = output_format(out)?;
|
||||
let mut s = Session::new();
|
||||
open(&mut s, Some(input), false)?;
|
||||
let fmt = deckcraft_engine::cmd::file::format_for_path(out);
|
||||
let r = if matches!(fmt, "png" | "jpeg") {
|
||||
s.execute("file.export", &json!({"path": out, "slide": 0}))
|
||||
} else {
|
||||
@@ -253,6 +253,16 @@ fn convert(args: &[String]) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The format `convert`, `run --save` and `run --export` write to `path`, or an error naming the
|
||||
/// supported extensions when `path`'s extension isn't one (rather than native bytes under that name).
|
||||
fn output_format(path: &str) -> Result<&'static str, String> {
|
||||
use deckcraft_engine::cmd::file::{WRITABLE_EXTENSIONS, writable_format_for_path};
|
||||
writable_format_for_path(path).ok_or_else(|| {
|
||||
let ext = std::path::Path::new(path).extension().map(|e| format!(".{}", e.to_string_lossy())).unwrap_or_else(|| "(none)".into());
|
||||
format!("{path}: unsupported output format {ext} (supported: .{})", WRITABLE_EXTENSIONS.join(", ."))
|
||||
})
|
||||
}
|
||||
|
||||
fn run(args: &[String]) -> Result<(), String> {
|
||||
let mut input = None;
|
||||
let mut sample = false;
|
||||
@@ -272,6 +282,9 @@ fn run(args: &[String]) -> Result<(), String> {
|
||||
other => return Err(format!("unknown option `{other}`")),
|
||||
}
|
||||
}
|
||||
for p in save.iter().chain(&export) {
|
||||
output_format(p)?;
|
||||
}
|
||||
let mut s = Session::new();
|
||||
open(&mut s, input.as_deref(), sample)?;
|
||||
for c in cmds {
|
||||
|
||||
Reference in New Issue
Block a user