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 | use std::str::FromStr; | 1 | use std::str::FromStr; |
| 4 | use std::time::Duration; | 2 | use std::time::Duration; |
| 5 | 3 | ||
| @@ -779,9 +777,8 @@ where | |||
| 779 | ) -> Result<Self, DecodeError<S>> { | 777 | ) -> Result<Self, DecodeError<S>> { |
| 780 | expect_only_children(node, ctx); | 778 | expect_only_children(node, ctx); |
| 781 | 779 | ||
| 782 | - let mut seen_keys: HashMap<Key, &knuffel::ast::SpannedNode<S>> = HashMap::new(); | 780 | + let mut nodes: Vec<&knuffel::ast::SpannedNode<S>> = Vec::new(); |
| 783 | - | 781 | + let mut binds: Vec<Bind> = Vec::new(); |
| 784 | - let mut binds = Vec::new(); | ||
| 785 | 782 | ||
| 786 | for child in node.children() { | 783 | for child in node.children() { |
| 787 | match Bind::decode_node(child, ctx) { | 784 | match Bind::decode_node(child, ctx) { |
| @@ -789,26 +786,24 @@ where | |||
| 789 | ctx.emit_error(e); | 786 | ctx.emit_error(e); |
| 790 | } | 787 | } |
| 791 | Ok(bind) => { | 788 | Ok(bind) => { |
| 792 | - match seen_keys.entry(bind.key) { | 789 | + let conflict = binds.iter().position(|prev| prev.key == bind.key); |
| 793 | - Entry::Occupied(entry) => { | 790 | + if let Some(idx) = conflict { |
| 794 | - // Even though it's technically incorrect, we use | 791 | + // Even though it's technically incorrect, we use |
| 795 | - // `DecodeError::Missing` here because it labels the bind with | 792 | + // `DecodeError::Missing` here because it labels the bind with |
| 796 | - // "node starts here", which is the least bad option | 793 | + // "node starts here", which is the least bad option |
| 797 | - ctx.emit_error(DecodeError::missing( | 794 | + ctx.emit_error(DecodeError::missing( |
| 798 | - entry.get(), | 795 | + nodes[idx], |
| 799 | - "keybind first defined here", | 796 | + "keybind first defined here", |
| 800 | - )); | 797 | + )); |
| 801 | 798 | ||
| 802 | - ctx.emit_error(DecodeError::unexpected( | 799 | + ctx.emit_error(DecodeError::unexpected( |
| 803 | - &child.node_name, | 800 | + &child.node_name, |
| 804 | - "keybind", | 801 | + "keybind", |
| 805 | - "duplicate keybind later defined here", | 802 | + "duplicate keybind later defined here", |
| 806 | - )); | 803 | + )); |
| 807 | - } | 804 | + } else { |
| 808 | - Entry::Vacant(entry) => { | 805 | + nodes.push(child); |
| 809 | - entry.insert(child); | 806 | + binds.push(bind); |
| 810 | - binds.push(bind); | ||
| 811 | - } | ||
| 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 | use knuffel::errors::DecodeError; | 1 | use knuffel::errors::DecodeError; |
| 4 | use smithay::input::keyboard::Keysym; | 2 | use smithay::input::keyboard::Keysym; |
| 5 | 3 | ||
| @@ -262,26 +260,34 @@ where | |||
| 262 | ) -> Result<Self, DecodeError<S>> { | 260 | ) -> Result<Self, DecodeError<S>> { |
| 263 | expect_only_children(node, ctx); | 261 | expect_only_children(node, ctx); |
| 264 | 262 | ||
| 265 | - let mut seen_keys = HashSet::new(); | 263 | + let mut nodes: Vec<&knuffel::ast::SpannedNode<S>> = Vec::new(); |
| 266 | - | 264 | + let mut binds: Vec<MruBind> = Vec::new(); |
| 267 | - let mut binds = Vec::new(); | ||
| 268 | 265 | ||
| 269 | for child in node.children() { | 266 | for child in node.children() { |
| 270 | match MruBind::decode_node(child, ctx) { | 267 | match MruBind::decode_node(child, ctx) { |
| 268 | + Err(e) => { | ||
| 269 | + ctx.emit_error(e); | ||
| 270 | + } | ||
| 271 | Ok(bind) => { | 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 | ctx.emit_error(DecodeError::unexpected( | 282 | ctx.emit_error(DecodeError::unexpected( |
| 274 | &child.node_name, | 283 | &child.node_name, |
| 275 | "keybind", | 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 | } |