From c397a10cde368d4184dd0850b36dfb633317942f Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 19 Dec 2024 13:22:58 -0600 Subject: [PATCH] refactor(path): Flatten with let-else --- src/path/mod.rs | 61 ++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/src/path/mod.rs b/src/path/mod.rs index 0f87b878..db79a813 100644 --- a/src/path/mod.rs +++ b/src/path/mod.rs @@ -107,56 +107,55 @@ impl Expression { if !matches!(root.kind, ValueKind::Table(_)) { *root = Map::::new().into(); } - - if let ValueKind::Table(ref mut map) = root.kind { - map.entry(key.clone()) - .or_insert_with(|| Value::new(None, ValueKind::Nil)) - } else { + let ValueKind::Table(ref mut map) = root.kind else { unreachable!() - } + }; + + map.entry(key.clone()) + .or_insert_with(|| Value::new(None, ValueKind::Nil)) } Self::Child(ref expr, ref key) => { let child = expr.get_mut_forcibly(root); + if !matches!(child.kind, ValueKind::Table(_)) { *child = Map::::new().into(); } - - if let ValueKind::Table(ref mut map) = child.kind { - map.entry(key.clone()) - .or_insert_with(|| Value::new(None, ValueKind::Nil)) - } else { + let ValueKind::Table(ref mut map) = child.kind else { unreachable!() - } + }; + + map.entry(key.clone()) + .or_insert_with(|| Value::new(None, ValueKind::Nil)) } Self::Subscript(ref expr, index) => { let child = expr.get_mut_forcibly(root); + if !matches!(child.kind, ValueKind::Array(_)) { *child = Vec::::new().into(); } + let ValueKind::Array(ref mut array) = child.kind else { + unreachable!() + }; - if let ValueKind::Array(ref mut array) = child.kind { - let uindex = match abs_index(index, array.len()) { - Ok(uindex) => { - if uindex >= array.len() { - array.resize(uindex + 1, Value::new(None, ValueKind::Nil)); - } - uindex + let uindex = match abs_index(index, array.len()) { + Ok(uindex) => { + if uindex >= array.len() { + array.resize(uindex + 1, Value::new(None, ValueKind::Nil)); } - Err(insertion) => { - array.splice( - 0..0, - (0..insertion).map(|_| Value::new(None, ValueKind::Nil)), - ); - 0 - } - }; + uindex + } + Err(insertion) => { + array.splice( + 0..0, + (0..insertion).map(|_| Value::new(None, ValueKind::Nil)), + ); + 0 + } + }; - &mut array[uindex] - } else { - unreachable!() - } + &mut array[uindex] } } }