mirror of
https://github.com/storytold/cadcraft.git
synced 2026-10-24 10:56:51 -06:00
SETVAR keeps text variables such as CLAYER as text, so a layer named 10 can be selected (#484) (#576)
Squash of PR #576 by @MarekSpodBiedry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a5a9e3ef63
commit
3363d4a78c
@@ -297,6 +297,16 @@ pub fn set(s: &mut Session, name: &str, v: &Value) -> Result<()> {
|
||||
d.header.set_i64(&n, i);
|
||||
return Ok(());
|
||||
}
|
||||
// A text variable (CLAYER...) keeps what was typed, so a layer named `10` stays a string.
|
||||
Some(cadcraft_doc::HVal::Str(_)) => {
|
||||
let text = match v {
|
||||
Value::String(t) => t.clone(),
|
||||
Value::Number(x) => x.to_string(),
|
||||
_ => return Err(bad()),
|
||||
};
|
||||
d.header.set(&n, cadcraft_doc::HVal::Str(text));
|
||||
return Ok(());
|
||||
}
|
||||
// A point variable takes `x,y` typed at the command line too.
|
||||
Some(cadcraft_doc::HVal::Point(_)) => {
|
||||
d.header.set(&n, cadcraft_doc::HVal::Point(as_pt(v).ok_or_else(bad)?.to3(0.0)));
|
||||
|
||||
@@ -471,6 +471,26 @@ fn sysvars_roundtrip() {
|
||||
assert_eq!(sysvars::get(&s, "FONTALT"), Some(json!("")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn setvar_clayer_keeps_numeric_layer_name() {
|
||||
let mut s = Session::new();
|
||||
s.execute("layer.new", &json!({"name": "10"})).unwrap();
|
||||
s.execute("setvar", &json!({"name": "CLAYER", "value": "10"})).unwrap();
|
||||
assert_eq!(sysvars::get(&s, "CLAYER"), Some(json!("10")));
|
||||
let c = s.execute("circle", &json!({"center": [0, 0], "radius": 1})).unwrap();
|
||||
let h = cadcraft_doc::Handle::parse_hex(c["handle"].as_str().unwrap()).unwrap();
|
||||
assert_eq!(s.doc().unwrap().entity(h).unwrap().common.layer, "10");
|
||||
s.execute("setvar", &json!({"name": "CLAYER", "value": "0"})).unwrap();
|
||||
assert_eq!(sysvars::get(&s, "CLAYER"), Some(json!("0")));
|
||||
// Typed at the command line too.
|
||||
s.cmdline("SETVAR CLAYER 10").unwrap();
|
||||
assert!(s.running.is_none());
|
||||
assert_eq!(sysvars::get(&s, "CLAYER"), Some(json!("10")));
|
||||
// Numeric variables still parse typed text.
|
||||
s.execute("setvar", &json!({"name": "LUNITS", "value": "3"})).unwrap();
|
||||
assert_eq!(sysvars::get(&s, "LUNITS"), Some(json!(3)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn setvar_at_command_line() {
|
||||
let mut s = Session::new();
|
||||
|
||||
Reference in New Issue
Block a user