Skip to content

Commit

Permalink
refactor(path): Flatten with let-else
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 19, 2024
1 parent 8323fa2 commit c397a10
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions src/path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,56 +107,55 @@ impl Expression {
if !matches!(root.kind, ValueKind::Table(_)) {
*root = Map::<String, Value>::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::<String, Value>::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::<Value>::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]
}
}
}
Expand Down

0 comments on commit c397a10

Please sign in to comment.