summaryrefslogtreecommitdiffstats
path: root/src/path
diff options
context:
space:
mode:
authorRyan Leckey <ryan@launchbadge.com>2017-06-16 11:15:40 -0700
committerRyan Leckey <ryan@launchbadge.com>2017-06-16 11:15:40 -0700
commit05e9cd421e75173462072e57ab7e27881730d250 (patch)
tree8db6e8d6951c57a3d50093a8fd789c2941df849f /src/path
parent1716c2fe7571b03215a5ae09c91f508d03335c2a (diff)
Support subscript access on path get/set
Diffstat (limited to 'src/path')
-rw-r--r--src/path/mod.rs78
-rw-r--r--src/path/parser.rs2
2 files changed, 72 insertions, 8 deletions
diff --git a/src/path/mod.rs b/src/path/mod.rs
index 4c4dc2b..046888e 100644
--- a/src/path/mod.rs
+++ b/src/path/mod.rs
@@ -10,7 +10,7 @@ mod parser;
pub enum Expression {
Identifier(String),
Child(Box<Expression>, String),
- Subscript(Box<Expression>, i32),
+ Subscript(Box<Expression>, isize),
}
impl FromStr for Expression {
@@ -21,6 +21,14 @@ impl FromStr for Expression {
}
}
+fn sindex_to_uindex(index: isize, len: usize) -> usize {
+ if index >= 0 {
+ index as usize
+ } else {
+ len - (index.abs() as usize)
+ }
+}
+
impl Expression {
pub fn get(self, root: &Value) -> Option<&Value> {
match self {
@@ -50,8 +58,26 @@ impl Expression {
}
}
- _ => {
- unimplemented!();
+ Expression::Subscript(expr, index) => {
+ match expr.get(root) {
+ Some(value) => {
+ match value.kind {
+ ValueKind::Array(ref array) => {
+ let index = sindex_to_uindex(index, array.len());
+
+ if index >= array.len() {
+ None
+ } else {
+ Some(&array[index])
+ }
+ }
+
+ _ => None,
+ }
+ }
+
+ _ => None,
+ }
}
}
}
@@ -92,8 +118,26 @@ impl Expression {
}
}
- _ => {
- unimplemented!();
+ Expression::Subscript(ref expr, index) => {
+ match expr.get_mut(root) {
+ Some(value) => {
+ match value.kind {
+ ValueKind::Array(ref mut array) => {
+ let index = sindex_to_uindex(index, array.len());
+
+ if index >= array.len() {
+ array.resize((index + 1) as usize, Value::new(None, ValueKind::Nil));
+ }
+
+ Some(&mut array[index])
+ }
+
+ _ => None,
+ }
+ }
+
+ _ => None,
+ }
}
}
}
@@ -151,8 +195,28 @@ impl Expression {
}
}
- _ => {
- unimplemented!();
+ Expression::Subscript(ref expr, index) => {
+ if let Some(parent) = expr.get_mut(root) {
+ match parent.kind {
+ ValueKind::Array(ref mut array) => {
+ let uindex = sindex_to_uindex(index, array.len());
+
+ if uindex >= array.len() {
+ array.resize((uindex + 1) as usize, Value::new(None, ValueKind::Nil));
+ }
+
+ array[uindex] = value.clone();
+ }
+
+ _ => {
+ // Didn't find an array ...
+ // Add an array and do this again
+ *parent = Vec::<Value>::new().into();
+
+ Expression::Subscript(expr.clone(), index).set(parent, value);
+ }
+ }
+ }
}
}
}
diff --git a/src/path/parser.rs b/src/path/parser.rs
index ee3fb65..d4b13dd 100644
--- a/src/path/parser.rs
+++ b/src/path/parser.rs
@@ -16,7 +16,7 @@ named!(ident_<String>,
)
);
-named!(integer <i32>,
+named!(integer <isize>,
map_res!(
map_res!(
ws!(digit),