Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

M6: form_fields and form_calculation_order name product and average as form_set_props takes them

Grzegorz Martens36e16de47005
diff --git a/crates/automation/src/forms.rs b/crates/automation/src/forms.rs
index 3399505ec..649366de5 100644
--- a/crates/automation/src/forms.rs
+++ b/crates/automation/src/forms.rs
@@ -121,12 +121,11 @@ fn calculate_arg(v: &Value) -> Result<Calculate> {
return Ok(Calculate::Notation(n.to_string()));
}
let op = match o.get("op").and_then(Value::as_str).unwrap_or("") {
- "sum" => CalcOp::Sum,
- "product" => CalcOp::Product,
- "average" => CalcOp::Average,
- "min" | "minimum" => CalcOp::Minimum,
- "max" | "maximum" => CalcOp::Maximum,
- x => return Err(bad(format!("unknown op {x:?} (sum, product, average, min, max)"))),
+ "minimum" => CalcOp::Minimum,
+ "maximum" => CalcOp::Maximum,
+ x => {
+ CalcOp::ALL.into_iter().find(|&op| op_name(op) == x).ok_or_else(|| bad(format!("unknown op {x:?} (sum, product, average, min, max)")))?
+ }
};
let fields: Vec<String> =
o.get("fields").and_then(Value::as_array).map(|a| a.iter().filter_map(|x| x.as_str().map(str::to_owned)).collect()).unwrap_or_default();
@@ -136,11 +135,22 @@ fn calculate_arg(v: &Value) -> Result<Calculate> {
Ok(Calculate::Simple { op, fields })
}
+/// An operation's name in `calculate`, as `form_set_props` takes it and the listings show it.
+fn op_name(op: CalcOp) -> &'static str {
+ match op {
+ CalcOp::Sum => "sum",
+ CalcOp::Product => "product",
+ CalcOp::Average => "average",
+ CalcOp::Minimum => "min",
+ CalcOp::Maximum => "max",
+ }
+}
+
/// A field's calculation as `form_fields` and `form_calculation_order` show it, in
/// `form_set_props`'s shape, or `{"script": …}` for a custom Calculate script; `None` without one.
fn calculate_json(f: &FormField) -> Option<Value> {
match (&f.actions.calculate, &f.actions.scripts.calculate) {
- (Calculate::Simple { op, fields }, _) => Some(json!({ "op": op.code().to_lowercase(), "fields": fields })),
+ (Calculate::Simple { op, fields }, _) => Some(json!({ "op": op_name(*op), "fields": fields })),
(Calculate::Notation(n), _) => Some(json!({ "notation": n })),
(Calculate::None, Some(js)) => Some(json!({ "script": js })),
(Calculate::None, None) => None,
diff --git a/crates/automation/tests/it/automation.rs b/crates/automation/tests/it/automation.rs
index f46187d3e..7778108cb 100644
--- a/crates/automation/tests/it/automation.rs
+++ b/crates/automation/tests/it/automation.rs
@@ -5131,6 +5131,28 @@ fn the_calculation_order_shows_formulas_and_what_they_cannot_read() {
}
}
+/// Every operation comes back from form_fields and form_calculation_order as form_set_props takes
+/// it, so a formula can be read and given back: product and average came back as prd and avg,
+/// which it refused.
+#[test]
+fn every_calculation_op_can_be_read_and_given_back() {
+ let dir = workdir("calcops");
+ let mut a = auto(&dir);
+ let doc = ok(&mut a, "doc_open", json!({ "path": "a.pdf" }))["doc"].as_u64().unwrap();
+ for (name, y) in [("a", 20), ("b", 60), ("total", 100)] {
+ ok(&mut a, "form_add_field", json!({ "doc": doc, "page": 1, "type": "text", "rect": [20, y, 180, y + 22], "name": name }));
+ }
+ for op in ["sum", "product", "average", "min", "max"] {
+ let calculate = json!({ "op": op, "fields": ["a", "b"] });
+ ok(&mut a, "form_set_props", json!({ "doc": doc, "field": "total", "calculate": calculate }));
+ let fields = ok(&mut a, "form_fields", json!({ "doc": doc }));
+ let shown = fields["fields"].as_array().unwrap().iter().find(|f| f["name"] == "total").unwrap()["calculate"].clone();
+ let order = ok(&mut a, "form_calculation_order", json!({ "doc": doc }));
+ assert_eq!((&shown, &order["fields"][0]["formula"]), (&calculate, &calculate), "{op}");
+ ok(&mut a, "form_set_props", json!({ "doc": doc, "field": "total", "calculate": shown }));
+ }
+}
+
#[test]
fn page_tab_orders_are_read_and_set_per_page_through_tools() {
let dir = workdir("pagetabs");