Store canonical autoTypes names so accepted spellings take effect (#95) (#156)

This commit is contained in:
MarekSpodBiedry authored and GitHub committed 2026-10-10 12:13:53 -04:00
1 parent 0af1528f4c
commit 947535158a
2 files changed
+23 -2

No files matched your search

+2 -2
View File
@@ -783,7 +783,7 @@ fn run_autoconstrain(s: &mut Session, p: &Value) -> Result<Value> {
o.distance_tolerance = f64_or(p, "tolerance", o.distance_tolerance);
o.angle_tolerance = f64_or(p, "angleTolerance", o.angle_tolerance);
if let Some(t) = p.get("types").and_then(Value::as_array) {
o.types = t.iter().filter_map(Value::as_str).map(str::to_string).collect();
o.types = t.iter().filter_map(Value::as_str).map(|n| kind_from_name(n).map_or_else(|| n.to_string(), |k| k.name().to_string())).collect();
}
let mut d = s.doc()?.clone();
let ids = cs::autoconstrain(&mut d, &hs, &o);
@@ -922,7 +922,7 @@ fn run_constraintsettings(s: &mut Session, p: &Value) -> Result<Value> {
set.bar_transparency = t.clamp(0.0, 90.0) as u8;
}
if let Some(t) = p.get("autoTypes").and_then(Value::as_array) {
set.auto_types = t.iter().filter_map(Value::as_str).filter(|n| kind_from_name(n).is_some()).map(str::to_string).take(32).collect();
set.auto_types = t.iter().filter_map(Value::as_str).filter_map(|n| kind_from_name(n).map(|k| k.name().to_string())).take(32).collect();
}
if set != cur {
s.doc_mut()?.parametric.settings = set.clone();
@@ -142,6 +142,27 @@ fn bars_settings_and_autoconstrain_json() {
assert_eq!(s.doc().unwrap().constraints[0].kind, ConstraintKind::Horizontal);
}
#[test]
fn auto_types_spellings_are_stored_canonically() {
for spelling in ["gcParallel", " Parallel ", "parallel"] {
let mut s = Session::new();
let a = line(&mut s, [0.0, 0.0], [10.0, 10.0]);
let b = line(&mut s, [0.0, 3.0], [10.0, 13.01]);
let r = s.execute("constraintsettings", &json!({ "autoTypes": [spelling], "angleTolerance": 1 })).unwrap();
assert_eq!(r["autoTypes"], json!(["Parallel"]));
let r = s.execute("autoconstrain", &json!({ "handles": [a, b] })).unwrap();
assert_eq!(r["added"], 1, "{spelling}");
assert_eq!(s.doc().unwrap().constraints[0].kind, ConstraintKind::Parallel);
// The per-command `types` override accepts the same spellings.
let mut s = Session::new();
let a = line(&mut s, [0.0, 0.0], [10.0, 10.0]);
let b = line(&mut s, [0.0, 3.0], [10.0, 13.01]);
let r = s.execute("autoconstrain", &json!({ "handles": [a, b], "types": [spelling], "angleTolerance": 1 })).unwrap();
assert_eq!(r["added"], 1, "{spelling}");
assert_eq!(s.doc().unwrap().constraints[0].kind, ConstraintKind::Parallel);
}
}
#[test]
fn hostile_constraint_params_never_panic() {
let mut s = Session::new();