config: Simplify and reconcile binds parsing
Browse files at this commit600ec86217eacf5ec0024a521ed1fc35166476a5Parent: 807681c2 changed files +38 -37
- niri-config/src/binds.rs +19 -24
- niri-config/src/recent_windows.rs +19 -13
niri-config/src/binds.rsmodified+19 -24
| @@ -1,5 +1,3 @@ | ||
| 1 | -use std::collections::hash_map::Entry; | |
| 2 | -use std::collections::HashMap; | |
| 3 | 1 | use std::str::FromStr; |
| 4 | 2 | use std::time::Duration; |
| 5 | 3 | |
| @@ -779,9 +777,8 @@ where | ||
| 779 | 777 | ) -> Result<Self, DecodeError<S>> { |
| 780 | 778 | expect_only_children(node, ctx); |
| 781 | 779 | |
| 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(); | |
| 785 | 782 | |
| 786 | 783 | for child in node.children() { |
| 787 | 784 | match Bind::decode_node(child, ctx) { |
| @@ -789,26 +786,24 @@ where | ||
| 789 | 786 | ctx.emit_error(e); |
| 790 | 787 | } |
| 791 | 788 | 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 | + )); | |
| 801 | 798 | |
| 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); | |
| 812 | 807 | } |
| 813 | 808 | } |
| 814 | 809 | } |
niri-config/src/recent_windows.rsmodified+19 -13
| @@ -1,5 +1,3 @@ | ||
| 1 | -use std::collections::HashSet; | |
| 2 | - | |
| 3 | 1 | use knuffel::errors::DecodeError; |
| 4 | 2 | use smithay::input::keyboard::Keysym; |
| 5 | 3 | |
| @@ -262,26 +260,34 @@ where | ||
| 262 | 260 | ) -> Result<Self, DecodeError<S>> { |
| 263 | 261 | expect_only_children(node, ctx); |
| 264 | 262 | |
| 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(); | |
| 268 | 265 | |
| 269 | 266 | for child in node.children() { |
| 270 | 267 | match MruBind::decode_node(child, ctx) { |
| 268 | + Err(e) => { | |
| 269 | + ctx.emit_error(e); | |
| 270 | + } | |
| 271 | 271 | 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 | + | |
| 273 | 282 | ctx.emit_error(DecodeError::unexpected( |
| 274 | 283 | &child.node_name, |
| 275 | 284 | "keybind", |
| 276 | - "duplicate keybind", | |
| 285 | + "duplicate keybind later defined here", | |
| 277 | 286 | )); |
| 278 | - continue; | |
| 287 | + } else { | |
| 288 | + nodes.push(child); | |
| 289 | + binds.push(bind); | |
| 279 | 290 | } |
| 280 | - | |
| 281 | - binds.push(bind); | |
| 282 | - } | |
| 283 | - Err(e) => { | |
| 284 | - ctx.emit_error(e); | |
| 285 | 291 | } |
| 286 | 292 | } |
| 287 | 293 | } |