@@ -28,7 +28,6 @@ use crate::{Expression, Extension, Location, QueryResult, QueryResults};
2828use crate :: query:: traversal:: traverse;
2929
3030use crate :: Rc ;
31- use alloc:: collections:: btree_map:: Entry as BTreeMapEntry ;
3231use alloc:: collections:: { BTreeMap , BTreeSet } ;
3332use anyhow:: { anyhow, bail, Result } ;
3433use core:: ops:: Bound :: * ;
@@ -1312,10 +1311,10 @@ impl Interpreter {
13121311 * obj = Value :: new_object ( ) ;
13131312 }
13141313
1315- obj = obj
1316- . as_object_mut ( ) ?
1317- . entry ( Value :: String ( p . to_string ( ) . into ( ) ) )
1318- . or_insert ( Value :: new_object ( ) ) ;
1314+ obj = obj. as_object_mut ( ) ? . get_or_insert_with (
1315+ Value :: String ( p . to_string ( ) . into ( ) ) ,
1316+ Value :: new_object ,
1317+ ) ;
13191318 }
13201319 * obj = value;
13211320 // Mark modified rules as processed.
@@ -1682,29 +1681,21 @@ impl Interpreter {
16821681 let set = obj
16831682 . as_object_mut ( )
16841683 . map_err ( |_| anyhow ! ( span. error( "previous value is not an object" ) ) ) ?
1685- . entry ( p)
1686- . or_insert ( Value :: new_set ( ) )
1684+ . get_or_insert_with ( p, Value :: new_set)
16871685 . as_set_mut ( )
16881686 . map_err ( |_| anyhow ! ( span. error( "previous value is not a set" ) ) ) ?;
16891687 set. append ( value. as_set_mut ( ) ?) ;
16901688 } else {
16911689 let obj = obj
16921690 . as_object_mut ( )
16931691 . map_err ( |_| anyhow ! ( span. error( "previous value is not an object" ) ) ) ?;
1694- match obj. entry ( p) {
1695- BTreeMapEntry :: Vacant ( v) => {
1696- if value != Value :: Undefined {
1697- v. insert ( value) ;
1698- } else {
1699- // TODO: clean this assumption between Undefined vs Object.
1700- v. insert ( Value :: new_object ( ) ) ;
1701- }
1702- }
1703- BTreeMapEntry :: Occupied ( o) => {
1704- if o. get ( ) != & value && value != Value :: Undefined {
1705- bail ! ( span
1706- . error( "complete rules should not produce multiple outputs" ) )
1707- }
1692+ if value == Value :: Undefined {
1693+ // TODO: clean this assumption between Undefined vs Object.
1694+ obj. get_or_insert_with ( p, Value :: new_object) ;
1695+ } else {
1696+ let existing = obj. get_or_insert_with ( p, || value. clone ( ) ) ;
1697+ if * existing != value {
1698+ bail ! ( span. error( "complete rules should not produce multiple outputs" ) )
17081699 }
17091700 }
17101701 }
@@ -1713,8 +1704,7 @@ impl Interpreter {
17131704 obj = obj
17141705 . as_object_mut ( )
17151706 . map_err ( |_| anyhow ! ( span. error( "previous value is not an object" ) ) ) ?
1716- . entry ( p)
1717- . or_insert ( Value :: new_object ( ) ) ;
1707+ . get_or_insert_with ( p, Value :: new_object) ;
17181708 }
17191709 }
17201710 Ok ( ( ) )
@@ -1822,8 +1812,7 @@ impl Interpreter {
18221812 let set = ctx_mut
18231813 . rule_value
18241814 . as_object_mut ( ) ?
1825- . entry ( Value :: from_array ( comps) )
1826- . or_insert ( Value :: new_set ( ) ) ;
1815+ . get_or_insert_with ( Value :: from_array ( comps) , Value :: new_set) ;
18271816 if output != Value :: Undefined {
18281817 set. as_set_mut ( ) ?. insert ( output) ;
18291818 return Ok ( true ) ;
@@ -1832,20 +1821,13 @@ impl Interpreter {
18321821 }
18331822
18341823 // Non-set rule.
1835- match ctx_mut
1836- . rule_value
1837- . as_object_mut ( ) ?
1838- . entry ( Value :: from_array ( comps) )
1839- {
1840- BTreeMapEntry :: Vacant ( v) => {
1841- v. insert ( output) ;
1842- }
1843- BTreeMapEntry :: Occupied ( o) if o. get ( ) != & output => bail ! ( rule_ref
1824+ let key = Value :: from_array ( comps) ;
1825+ let obj_mut = ctx_mut. rule_value . as_object_mut ( ) ?;
1826+ let existing = obj_mut. get_or_insert_with ( key, || output. clone ( ) ) ;
1827+ if * existing != output {
1828+ bail ! ( rule_ref
18441829 . span( )
1845- . error( "rules must not produce multiple outputs" ) ) ,
1846- _ => {
1847- // Rule produced same value.
1848- }
1830+ . error( "rules must not produce multiple outputs" ) ) ;
18491831 }
18501832
18511833 return Ok ( true ) ;
@@ -2471,7 +2453,7 @@ impl Interpreter {
24712453 }
24722454 Value :: Object ( map) => {
24732455 s. push ( '{' ) ;
2474- for ( idx, ( k, entry_value) ) in map. iter ( ) . enumerate ( ) {
2456+ for ( idx, ( k, entry_value) ) in map. iter_sorted ( ) . enumerate ( ) {
24752457 if idx > 0 {
24762458 s. push_str ( ", " ) ;
24772459 }
0 commit comments