forge
Explore
Log in

Don't you dare go Hollow

config: Simplify and reconcile binds parsing

Browse files at this commit

600ec86217eacf5ec0024a521ed1fc35166476a5Ivan MolodetskikhParent: 807681c
- Use a Vec instead of a HashMap
- Update the recent_windows version to match the normal one,
  printing the duplicate bind
niri-config/src/binds.rsmodified+19 -24
@@ -1,5 +1,3 @@
1-use std::collections::hash_map::Entry;
2-use std::collections::HashMap;
31 use std::str::FromStr;
42 use std::time::Duration;
53
@@ -779,9 +777,8 @@ where
779777 ) -> Result<Self, DecodeError<S>> {
780778 expect_only_children(node, ctx);
781779
782- let mut seen_keys: HashMap<Key, &knuffel::ast::SpannedNode<S>> = HashMap::new();
783-
784- let mut binds = Vec::new();
780+ let mut nodes: Vec<&knuffel::ast::SpannedNode<S>> = Vec::new();
781+ let mut binds: Vec<Bind> = Vec::new();
785782
786783 for child in node.children() {
787784 match Bind::decode_node(child, ctx) {
@@ -789,26 +786,24 @@ where
789786 ctx.emit_error(e);
790787 }
791788 Ok(bind) => {
792- match seen_keys.entry(bind.key) {
793- Entry::Occupied(entry) => {
794- // Even though it's technically incorrect, we use
795- // `DecodeError::Missing` here because it labels the bind with
796- // "node starts here", which is the least bad option
797- ctx.emit_error(DecodeError::missing(
798- entry.get(),
799- "keybind first defined here",
800- ));
789+ let conflict = binds.iter().position(|prev| prev.key == bind.key);
790+ if let Some(idx) = conflict {
791+ // Even though it's technically incorrect, we use
792+ // `DecodeError::Missing` here because it labels the bind with
793+ // "node starts here", which is the least bad option
794+ ctx.emit_error(DecodeError::missing(
795+ nodes[idx],
796+ "keybind first defined here",
797+ ));
801798
802- ctx.emit_error(DecodeError::unexpected(
803- &child.node_name,
804- "keybind",
805- "duplicate keybind later defined here",
806- ));
807- }
808- Entry::Vacant(entry) => {
809- entry.insert(child);
810- binds.push(bind);
811- }
799+ ctx.emit_error(DecodeError::unexpected(
800+ &child.node_name,
801+ "keybind",
802+ "duplicate keybind later defined here",
803+ ));
804+ } else {
805+ nodes.push(child);
806+ binds.push(bind);
812807 }
813808 }
814809 }
niri-config/src/recent_windows.rsmodified+19 -13
@@ -1,5 +1,3 @@
1-use std::collections::HashSet;
2-
31 use knuffel::errors::DecodeError;
42 use smithay::input::keyboard::Keysym;
53
@@ -262,26 +260,34 @@ where
262260 ) -> Result<Self, DecodeError<S>> {
263261 expect_only_children(node, ctx);
264262
265- let mut seen_keys = HashSet::new();
266-
267- let mut binds = Vec::new();
263+ let mut nodes: Vec<&knuffel::ast::SpannedNode<S>> = Vec::new();
264+ let mut binds: Vec<MruBind> = Vec::new();
268265
269266 for child in node.children() {
270267 match MruBind::decode_node(child, ctx) {
268+ Err(e) => {
269+ ctx.emit_error(e);
270+ }
271271 Ok(bind) => {
272- if !seen_keys.insert(bind.key) {
272+ let conflict = binds.iter().position(|prev| prev.key == bind.key);
273+ if let Some(idx) = conflict {
274+ // Even though it's technically incorrect, we use
275+ // `DecodeError::Missing` here because it labels the bind with
276+ // "node starts here", which is the least bad option
277+ ctx.emit_error(DecodeError::missing(
278+ nodes[idx],
279+ "keybind first defined here",
280+ ));
281+
273282 ctx.emit_error(DecodeError::unexpected(
274283 &child.node_name,
275284 "keybind",
276- "duplicate keybind",
285+ "duplicate keybind later defined here",
277286 ));
278- continue;
287+ } else {
288+ nodes.push(child);
289+ binds.push(bind);
279290 }
280-
281- binds.push(bind);
282- }
283- Err(e) => {
284- ctx.emit_error(e);
285291 }
286292 }
287293 }