summaryrefslogtreecommitdiffstats
path: root/src/path
diff options
context:
space:
mode:
authorRyan Leckey <leckey.ryan@gmail.com>2018-07-02 15:33:38 -0700
committerRyan Leckey <leckey.ryan@gmail.com>2018-07-02 15:33:38 -0700
commitcbb9ef88ea6f6452614dd8bbffce7203b1358a55 (patch)
treefd4f67587f9a36c517a3a3f1ff7ef727697d5fef /src/path
parentbbc78d85610dec79aa4af4dd360131c7880efb5f (diff)
Run rustfmt (nightly)
Diffstat (limited to 'src/path')
-rw-r--r--src/path/mod.rs155
-rw-r--r--src/path/parser.rs53
2 files changed, 96 insertions, 112 deletions
diff --git a/src/path/mod.rs b/src/path/mod.rs
index f042992..7fe6e44 100644
--- a/src/path/mod.rs
+++ b/src/path/mod.rs
@@ -1,7 +1,7 @@
-use std::str::FromStr;
-use std::collections::HashMap;
-use nom::ErrorKind;
use error::*;
+use nom::ErrorKind;
+use std::collections::HashMap;
+use std::str::FromStr;
use value::{Value, ValueKind};
mod parser;
@@ -58,111 +58,97 @@ impl Expression {
}
}
- 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])
- }
- }
+ 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());
- _ => None,
- },
+ if index >= array.len() {
+ None
+ } else {
+ Some(&array[index])
+ }
+ }
_ => None,
- }
+ },
+
+ _ => None,
},
}
}
pub fn get_mut<'a>(&self, root: &'a mut Value) -> Option<&'a mut Value> {
match *self {
- Expression::Identifier(ref id) => {
- match root.kind {
- ValueKind::Table(ref mut map) => map.get_mut(id),
+ Expression::Identifier(ref id) => match root.kind {
+ ValueKind::Table(ref mut map) => map.get_mut(id),
- _ => None,
- }
+ _ => None,
},
- Expression::Child(ref expr, ref key) => {
- match expr.get_mut(root) {
- Some(value) => {
- match value.kind {
- ValueKind::Table(ref mut map) => map.get_mut(key),
-
- _ => None
- }
- },
+ Expression::Child(ref expr, ref key) => match expr.get_mut(root) {
+ Some(value) => match value.kind {
+ ValueKind::Table(ref mut map) => map.get_mut(key),
_ => None,
- }
+ },
+
+ _ => None,
},
- 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());
+ 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() {
- None
- } else {
- Some(&mut array[index])
- }
+ if index >= array.len() {
+ None
+ } else {
+ Some(&mut array[index])
}
-
- _ => None,
- },
+ }
_ => None,
- }
+ },
+
+ _ => None,
},
}
}
pub fn get_mut_forcibly<'a>(&self, root: &'a mut Value) -> Option<&'a mut Value> {
match *self {
- Expression::Identifier(ref id) => {
- match root.kind {
+ Expression::Identifier(ref id) => match root.kind {
+ ValueKind::Table(ref mut map) => Some(
+ map.entry(id.clone())
+ .or_insert_with(|| Value::new(None, ValueKind::Nil)),
+ ),
+
+ _ => None,
+ },
+
+ Expression::Child(ref expr, ref key) => match expr.get_mut_forcibly(root) {
+ Some(value) => match value.kind {
ValueKind::Table(ref mut map) => Some(
- map.entry(id.clone())
+ map.entry(key.clone())
.or_insert_with(|| Value::new(None, ValueKind::Nil)),
),
- _ => None,
- }
- },
-
- Expression::Child(ref expr, ref key) => {
- match expr.get_mut_forcibly(root) {
- Some(value) => match value.kind {
- ValueKind::Table(ref mut map) => Some(
- map.entry(key.clone())
- .or_insert_with(|| Value::new(None, ValueKind::Nil)),
- ),
+ _ => {
+ *value = HashMap::<String, Value>::new().into();
- _ => {
- *value = HashMap::<String, Value>::new().into();
-
- if let ValueKind::Table(ref mut map) = value.kind {
- Some(
- map.entry(key.clone())
- .or_insert_with(|| Value::new(None, ValueKind::Nil)),
- )
- } else {
- unreachable!();
- }
+ if let ValueKind::Table(ref mut map) = value.kind {
+ Some(
+ map.entry(key.clone())
+ .or_insert_with(|| Value::new(None, ValueKind::Nil)),
+ )
+ } else {
+ unreachable!();
}
- },
+ }
+ },
- _ => None,
- }
+ _ => None,
},
Expression::Subscript(ref expr, index) => {
@@ -171,7 +157,7 @@ impl Expression {
Some(value) => {
match value.kind {
ValueKind::Array(_) => (),
- _ => *value = Vec::<Value>::new().into()
+ _ => *value = Vec::<Value>::new().into(),
}
match value.kind {
@@ -179,18 +165,21 @@ impl Expression {
let index = sindex_to_uindex(index, array.len());
if index >= array.len() {
- array.resize((index + 1) as usize, Value::new(None, ValueKind::Nil));
+ array.resize(
+ (index + 1) as usize,
+ Value::new(None, ValueKind::Nil),
+ );
}
Some(&mut array[index])
}
- _ => None
+ _ => None,
}
- },
- _ => None
+ }
+ _ => None,
}
- },
+ }
}
}
@@ -252,7 +241,7 @@ impl Expression {
if let Some(parent) = expr.get_mut_forcibly(root) {
match parent.kind {
ValueKind::Array(_) => (),
- _ => *parent = Vec::<Value>::new().into()
+ _ => *parent = Vec::<Value>::new().into(),
}
match parent.kind {
@@ -268,7 +257,7 @@ impl Expression {
array[uindex] = value.clone();
}
- _ => ()
+ _ => (),
}
}
}
diff --git a/src/path/parser.rs b/src/path/parser.rs
index 1ecd6e7..a1660ca 100644
--- a/src/path/parser.rs
+++ b/src/path/parser.rs
@@ -1,7 +1,7 @@
-use nom::{ErrorKind, digit, IResult};
-use nom::types::CompleteStr;
-use std::str::{FromStr, from_utf8};
use super::Expression;
+use nom::types::CompleteStr;
+use nom::{digit, ErrorKind, IResult};
+use std::str::{from_utf8, FromStr};
named!(raw_ident<CompleteStr, String>,
map!(is_a!(
@@ -28,24 +28,20 @@ named!(ident<CompleteStr, Expression>, map!(raw_ident, Expression::Identifier));
#[allow(cyclomatic_complexity)]
fn postfix(expr: Expression) -> Box<Fn(CompleteStr) -> IResult<CompleteStr, Expression>> {
Box::new(move |i: CompleteStr| {
- alt!(i,
- do_parse!(
- tag!(".") >>
- id: raw_ident >>
- (Expression::Child(Box::new(expr.clone()), id))
- ) |
- delimited!(
- char!('['),
- do_parse!(
- negative: opt!(tag!("-")) >>
- num: integer >>
- (Expression::Subscript(
- Box::new(expr.clone()),
- num * (if negative.is_none() { 1 } else { -1 })
- ))
- ),
- char!(']')
- )
+ alt!(
+ i,
+ do_parse!(tag!(".") >> id: raw_ident >> (Expression::Child(Box::new(expr.clone()), id)))
+ | delimited!(
+ char!('['),
+ do_parse!(
+ negative: opt!(tag!("-")) >> num: integer
+ >> (Expression::Subscript(
+ Box::new(expr.clone()),
+ num * (if negative.is_none() { 1 } else { -1 }),
+ ))
+ ),
+ char!(']')
+ )
)
})
}
@@ -62,7 +58,7 @@ pub fn from_str(input: &str) -> Result<Expression, ErrorKind> {
// Forward Incomplete and Error
result => {
- return result.map(|(_,o)| o).map_err(|e| e.into_error_kind());
+ return result.map(|(_, o)| o).map_err(|e| e.into_error_kind());
}
}
}
@@ -71,14 +67,14 @@ pub fn from_str(input: &str) -> Result<Expression, ErrorKind> {
}
// Forward Incomplete and Error
- result => result.map(|(_,o)| o).map_err(|e| e.into_error_kind()),
+ result => result.map(|(_, o)| o).map_err(|e| e.into_error_kind()),
}
}
#[cfg(test)]
mod test {
- use super::*;
use super::Expression::*;
+ use super::*;
#[test]
fn test_id() {
@@ -100,11 +96,10 @@ mod test {
assert_eq!(parsed, expected);
let parsed: Expression = from_str("abcd.efgh.ijkl").unwrap();
- let expected = Child(Box::new(
- Child(Box::new(
- Identifier("abcd".into())
- ), "efgh".into())
- ), "ijkl".into());
+ let expected = Child(
+ Box::new(Child(Box::new(Identifier("abcd".into())), "efgh".into())),
+ "ijkl".into(),
+ );
assert_eq!(parsed, expected);
}