Refuse to create a variable collection with a name that is already taken (#416)

This commit is contained in:
MarekSpodBiedry
2026-10-15 17:17:44 +02:00
parent 102404e8c3
commit fdf397d7dc
2 changed files with 14 additions and 0 deletions
+3
View File
@@ -112,6 +112,9 @@ pub fn specs() -> Vec<CommandSpec> {
spec!("variable.createCollection", "Create variable collection", "{name, modes? [names]}", true, |s, p| {
let name = req_string("variable.createCollection", p, "name")?;
let doc = s.doc_mut()?;
if doc.variables.collections.iter().any(|c| c.name == name) {
return Err(bad("variable.createCollection", format!("a collection named `{name}` already exists")));
}
let id = doc.new_collection_id();
let modes: Vec<String> = match get(p, "modes") {
Some(Value::Array(a)) => a.iter().filter_map(|m| m.as_str().map(str::to_string)).collect(),
@@ -114,3 +114,14 @@ fn field_scopes_by_property_and_layer() {
assert_eq!(scope(f, "paddingHorizontal"), Some("GAP"));
assert_eq!(scope(f, "visible"), None);
}
#[test]
fn create_collection_refuses_a_taken_name() {
let mut s = Session::new();
ex(&mut s, "variable.createCollection", json!({"name": "Theme"}));
let before = s.doc().unwrap().variables.collections.len();
assert!(s.execute("variable.createCollection", &json!({"name": "Theme"})).is_err());
assert_eq!(s.doc().unwrap().variables.collections.len(), before);
ex(&mut s, "variable.createCollection", json!({"name": "Theme 2"}));
assert_eq!(s.doc().unwrap().variables.collections.len(), before + 1);
}