mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #16449 from aiongg/engine-precheck-params
Precheck reports the same parameter errors as dispatch
This commit is contained in:
3 files changed
+26
-7
No files matched your search
@@ -1781,11 +1781,7 @@ impl Session {
|
||||
}
|
||||
return self.dispatch(&filter, p, background);
|
||||
}
|
||||
if id == "file.new" {
|
||||
crate::document_preset_cmds::validate_new_params(self, ¶ms)?;
|
||||
}
|
||||
crate::param_check::check(spec, ¶ms)?;
|
||||
crate::filters::validate_params(id, ¶ms)?;
|
||||
self.validate_params(id, spec, ¶ms)?;
|
||||
// A command that may not run leaves a floating selection floating (#13169): it is refused
|
||||
// before the piece drops, without a History step. One that may run is checked again
|
||||
// below, once the piece has dropped, on the pixels and selection it will change.
|
||||
|
||||
@@ -59,6 +59,15 @@ impl Session {
|
||||
self.live_commit = None;
|
||||
}
|
||||
|
||||
/// The per-command value checks `dispatch` and `precheck` share, in the order `dispatch` runs them.
|
||||
pub(crate) fn validate_params(&self, id: &str, spec: &crate::registry::CommandSpec, params: &Value) -> Result<()> {
|
||||
if id == "file.new" {
|
||||
crate::document_preset_cmds::validate_new_params(self, params)?;
|
||||
}
|
||||
crate::param_check::check(spec, params)?;
|
||||
crate::filters::validate_params(id, params)
|
||||
}
|
||||
|
||||
/// Check a call without running it: the command exists, its params have the shape
|
||||
/// [`Self::execute`] requires (New Layer's fully), and it can run now. For a caller that does
|
||||
/// something first only when the command will go through: Free Transform applies its box
|
||||
@@ -71,8 +80,7 @@ impl Session {
|
||||
if !(params.is_object() || params.is_null()) {
|
||||
return Err(EngineError::BadParams { cmd: id.to_string(), msg: "params must be a JSON object".into() });
|
||||
}
|
||||
crate::param_check::check(spec, params)?;
|
||||
crate::filters::validate_params(id, params)?;
|
||||
self.validate_params(id, spec, params)?;
|
||||
if id == "layer.new.layer" {
|
||||
crate::new_layer::parse(params)?;
|
||||
}
|
||||
|
||||
@@ -80,3 +80,18 @@ fn precheck_agrees_with_execute() {
|
||||
}
|
||||
assert!(disagreements.is_empty(), "{disagreements:#?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn precheck_reports_file_new_value_errors_like_execute() {
|
||||
for p in
|
||||
[json!({"width": 0}), json!({"width": 300_001}), json!({"height": "x"}), json!({"mode": "Bogus"}), json!({"width": 0, "name": 5}), json!({"name": 5})]
|
||||
{
|
||||
let pre = Session::new().precheck("file.new", &p).unwrap_err().to_string();
|
||||
let ran = Session::new().execute("file.new", p.clone()).unwrap_err().to_string();
|
||||
assert_eq!(pre, ran, "{p}");
|
||||
}
|
||||
// Where both checks fail, the value check's message (not the parameter check's) wins, as in dispatch.
|
||||
let both = Session::new().precheck("file.new", &json!({"width": 0, "name": 5})).unwrap_err().to_string();
|
||||
assert_eq!(both, "invalid parameters for `file.new`: `name` must be a string");
|
||||
Session::new().precheck("file.new", &json!({"width": 64, "height": 32})).unwrap();
|
||||
}
|
||||
Reference in new issue
Block a user