Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

LR-EXP-BITDEPTH: savePreset from the previous export drops a stored depth its format lacks

Marco Arnulfo3ac21d18a54b
diff --git a/crates/engine/src/cmd/export.rs b/crates/engine/src/cmd/export.rs
index 472d5e7bd..949c08f1b 100644
--- a/crates/engine/src/cmd/export.rs
+++ b/crates/engine/src/cmd/export.rs
@@ -61,7 +61,17 @@ fn save(s: &mut Session, p: &Value) -> Result<Value> {
let mut params = match p.get("params") {
Some(v @ Value::Object(_)) => v.clone(),
Some(_) => return Err(bad(ID, "`params` must be an object of app.export params")),
- None => s.last_export.clone().ok_or_else(|| bad(ID, "no `params` and no previous export to save"))?,
+ // a stored setting, not the caller's typo: a depth it holds that its `format` lacks was
+ // written before #1952 and is dropped, as Export with Previous does; without `format`
+ // it is judged when the preset exports
+ None => {
+ let last = s.last_export.as_ref().ok_or_else(|| bad(ID, "no `params` and no previous export to save"))?;
+ let mut last = crate::export::ExportOptions::known_keys(last);
+ if last.get("format").is_some_and(Value::is_string) {
+ crate::export::ExportOptions::drop_unfit_depth(&mut last, Default::default());
+ }
+ last
+ }
};
if let Some(o) = params.as_object_mut() {
o.retain(|k, _| !NOT_SETTINGS.contains(&k.as_str()));
diff --git a/crates/engine/src/tests_export.rs b/crates/engine/src/tests_export.rs
index 8814f7581..fccfbd533 100644
--- a/crates/engine/src/tests_export.rs
+++ b/crates/engine/src/tests_export.rs
@@ -120,6 +120,21 @@ fn a_preset_depth_is_judged_against_the_format_written() {
assert!(s.execute("export.savePreset", &json!({"name": "Deep 3", "params": {"format": "jpeg", "bitDepth": 16}})).is_err());
}
+/// Review of #1966: savePreset without `params` saves the previous export, a stored setting. A
+/// depth written before #1952 at the format's own depth is dropped, as Export with Previous does,
+/// instead of refusing the save; without `format`, the depth waits for the format written.
+#[test]
+fn a_preset_saved_from_the_previous_export_drops_its_unfit_depth() {
+ let mut s = Session::with_demo();
+ s.last_export = Some(json!({"format": "jpeg", "bitDepth": 16, "quality": 80, "dir": "/out"}));
+ s.execute("export.savePreset", &json!({"name": "Old"})).unwrap();
+ assert_eq!(s.export_presets[0].params, json!({"format": "jpeg", "quality": 80}));
+ s.last_export = Some(json!({"bitDepth": 16, "dir": "/out"}));
+ s.execute("export.savePreset", &json!({"name": "Deep"})).unwrap();
+ let p = s.export_params(&json!({"preset": "Deep", "path": "/out/a.png"})).unwrap();
+ assert_eq!(ExportOptions::from_params(&p).unwrap().bit_depth, Some(16));
+}
+
#[test]
fn presets_conflicts_and_subfolders() {
use crate::export::{Destination, export_batch};