From 4b1dc397058a50863d3c5fca191d31fb2aca32f4 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Mon, 30 Mar 2020 16:33:43 -0400 Subject: [PATCH] handle map-merge key overlaps --- src/value/map.rs | 14 +++++++++++++- tests/map.rs | 5 +++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/value/map.rs b/src/value/map.rs index 0e3fe80..238214a 100644 --- a/src/value/map.rs +++ b/src/value/map.rs @@ -1,4 +1,5 @@ use std::slice::Iter; +use std::vec::IntoIter; use super::Value; use crate::error::SassResult; @@ -30,7 +31,9 @@ impl SassMap { } pub fn merge(&mut self, other: SassMap) { - self.0.extend(other.0); + for (key, value) in other.into_iter() { + self.insert(key, value); + } } pub fn iter(&self) -> Iter<(Value, Value)> { @@ -55,3 +58,12 @@ impl SassMap { self.0.push((key, value)); } } + +impl IntoIterator for SassMap { + type Item = (Value, Value); + type IntoIter = IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} diff --git a/tests/map.rs b/tests/map.rs index 740a849..f7b18bc 100644 --- a/tests/map.rs +++ b/tests/map.rs @@ -78,6 +78,11 @@ test!( "a {\n color: inspect(map-merge((), ()));\n}\n", "a {\n color: ();\n}\n" ); +test!( + map_merge_same_keys, + "a {\n color: inspect(map-merge((c: d, e: f), (c: 1, e: 2)));\n}\n", + "a {\n color: (c: 1, e: 2);\n}\n" +); error!( map_merge_map1_non_map, "a {\n color: map-merge(foo, (a: b));\n}\n", "Error: $map1: foo is not a map."