-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(interpreter): Create dedicated Path for indexing into a Value
- Loading branch information
Showing
14 changed files
with
169 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use std::fmt; | ||
use std::iter; | ||
use std::slice; | ||
|
||
use itertools; | ||
|
||
use super::Index; | ||
|
||
/// Path to a value in an `Object`. | ||
#[derive(Clone, Debug, Default, PartialEq)] | ||
pub struct Path { | ||
indexes: Vec<Index>, | ||
} | ||
|
||
impl Path { | ||
/// Create a `Path` from iterator of `Index`s | ||
pub fn new<T: IntoIterator<Item=Index>>(indexes: T) -> Self { | ||
let indexes = indexes.into_iter().collect(); | ||
Self { indexes } | ||
} | ||
|
||
/// Create a `Value` reference. | ||
pub fn with_index<I: Into<Index>>(value: I) -> Self { | ||
let indexes = vec![value.into()]; | ||
Self { indexes } | ||
} | ||
|
||
/// Access the `Value` reference. | ||
pub fn iter(&self) -> IndexIter { | ||
IndexIter(self.indexes.iter()) | ||
} | ||
} | ||
|
||
impl Extend<Index> for Path { | ||
fn extend<T: IntoIterator<Item = Index>>(&mut self, iter: T) { | ||
self.indexes.extend(iter); | ||
} | ||
} | ||
|
||
impl iter::FromIterator<Index> for Path { | ||
fn from_iter<I>(iter: I) -> Self | ||
where | ||
I: IntoIterator<Item = Index>, | ||
{ | ||
let indexes = iter.into_iter().collect(); | ||
Self { indexes } | ||
} | ||
} | ||
|
||
impl fmt::Display for Path { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let data = itertools::join(self.iter(), "."); | ||
write!(f, "{}", data) | ||
} | ||
} | ||
|
||
/// Iterate over indexes in a `Value`'s `Path`. | ||
#[derive(Debug)] | ||
pub struct IndexIter<'i>(slice::Iter<'i, Index>); | ||
|
||
impl<'i> Iterator for IndexIter<'i> { | ||
type Item = &'i Index; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<&'i Index> { | ||
self.0.next() | ||
} | ||
|
||
#[inline] | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.0.size_hint() | ||
} | ||
|
||
#[inline] | ||
fn count(self) -> usize { | ||
self.0.count() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.