From 04e9b99b09eb9f7d3cd19f143a983056faba0054 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Fri, 3 Apr 2020 16:38:01 -0400 Subject: [PATCH] fail on duplicate key in map declaration --- src/value/map.rs | 8 +++++--- src/value/parse.rs | 4 +++- tests/map.rs | 4 ++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/value/map.rs b/src/value/map.rs index c2e9d78..78e3308 100644 --- a/src/value/map.rs +++ b/src/value/map.rs @@ -56,14 +56,16 @@ impl SassMap { .collect() } - pub fn insert(&mut self, key: Value, value: Value) { - for &mut (ref k, ref mut v) in &mut self.0 { + /// Returns true if the key already exists + pub fn insert(&mut self, key: Value, value: Value) -> bool { + for (ref k, ref mut v) in &mut self.0 { if k == &key { *v = value; - return; + return true; } } self.0.push((key, value)); + false } } diff --git a/src/value/parse.rs b/src/value/parse.rs index c604dc8..4d95018 100644 --- a/src/value/parse.rs +++ b/src/value/parse.rs @@ -325,7 +325,9 @@ impl Value { super_selector, )?; devour_whitespace(paren_toks); - map.insert(key, val); + if map.insert(key, val) { + return Err("Duplicate key.".into()); + } if paren_toks.peek().is_none() { break; } diff --git a/tests/map.rs b/tests/map.rs index 4e6efa3..baba436 100644 --- a/tests/map.rs +++ b/tests/map.rs @@ -129,3 +129,7 @@ test!( "a {\n color: inspect(map-remove((\"foo\": 1, \"bar\": 2, \"baz\": 3), \"bar\", \"baz\"));\n}\n", "a {\n color: (\"foo\": 1);\n}\n" ); +error!( + duplicate_key_in_declaration, + "a {\n $a: (foo: a, foo: b);;\n}\n", "Error: Duplicate key." +);