Baseline environment and configuration work. (#1287)

This commit is contained in:
Andrés N. Robalino
2020-01-27 22:13:22 -05:00
committed by GitHub
parent f8be1becf2
commit caa6830184
17 changed files with 824 additions and 99 deletions

View File

@ -122,7 +122,6 @@ impl UntaggedValue {
}
/// Helper for creating row values
#[allow(unused)]
pub fn row(entries: IndexMap<String, Value>) -> UntaggedValue {
UntaggedValue::Row(entries.into())
}

View File

@ -10,14 +10,17 @@ license = "MIT"
doctest = false
[dependencies]
nu = { path = "../..", version = "0.8.0" }
nu-parser = { path = "../nu-parser", version = "0.8.0" }
nu-source = { path = "../nu-source", version = "0.8.0" }
nu-protocol = { path = "../nu-protocol", version = "0.8.0" }
app_dirs = "1.2.1"
dunce = "1.0.0"
getset = "0.0.9"
glob = "0.3.0"
tempfile = "3.1.0"
indexmap = { version = "1.3.0", features = ["serde-1"] }
[build-dependencies]
nu-build = { version = "0.8.0", path = "../nu-build" }

View File

@ -17,6 +17,7 @@ nu-protocol = { path = "../nu-protocol", version = "0.8.0" }
num-traits = "0.2.10"
itertools = "0.8.2"
indexmap = { version = "1.3.0", features = ["serde-1"] }
[build-dependencies]
nu-build = { version = "0.8.0", path = "../nu-build" }

View File

@ -8,6 +8,8 @@ use nu_source::{HasSpan, PrettyDebug, Spanned, SpannedItem, Tag, Tagged, TaggedI
use num_traits::cast::ToPrimitive;
pub trait ValueExt {
fn row_entries(&self) -> RowValueIter<'_>;
fn table_entries(&self) -> TableValueIter<'_>;
fn into_parts(self) -> (UntaggedValue, Tag);
fn get_data(&self, desc: &str) -> MaybeOwned<'_, Value>;
fn get_data_by_key(&self, name: Spanned<&str>) -> Option<Value>;
@ -39,6 +41,14 @@ pub trait ValueExt {
}
impl ValueExt for Value {
fn row_entries(&self) -> RowValueIter<'_> {
row_entries(self)
}
fn table_entries(&self) -> TableValueIter<'_> {
table_entries(self)
}
fn into_parts(self) -> (UntaggedValue, Tag) {
(self.value, self.tag)
}
@ -524,3 +534,52 @@ pub(crate) fn get_mut_data_by_member<'value>(
_ => None,
}
}
pub enum RowValueIter<'a> {
Empty,
Entries(indexmap::map::Iter<'a, String, Value>),
}
pub enum TableValueIter<'a> {
Empty,
Entries(std::slice::Iter<'a, Value>),
}
impl<'a> Iterator for RowValueIter<'a> {
type Item = (&'a String, &'a Value);
fn next(&mut self) -> Option<Self::Item> {
match self {
RowValueIter::Empty => None,
RowValueIter::Entries(iter) => iter.next(),
}
}
}
impl<'a> Iterator for TableValueIter<'a> {
type Item = &'a Value;
fn next(&mut self) -> Option<Self::Item> {
match self {
TableValueIter::Empty => None,
TableValueIter::Entries(iter) => iter.next(),
}
}
}
pub fn table_entries(value: &Value) -> TableValueIter<'_> {
match &value.value {
UntaggedValue::Table(t) => TableValueIter::Entries(t.iter()),
_ => TableValueIter::Empty,
}
}
pub fn row_entries(value: &Value) -> RowValueIter<'_> {
match &value.value {
UntaggedValue::Row(o) => {
let iter = o.entries.iter();
RowValueIter::Entries(iter)
}
_ => RowValueIter::Empty,
}
}