Rows and values can be checked for emptiness. Allows to set a value if desired. (#1665)

This commit is contained in:
Andrés N. Robalino
2020-04-26 12:30:52 -05:00
committed by GitHub
parent a62745eefb
commit 80025ea684
11 changed files with 437 additions and 98 deletions

View File

@ -8,8 +8,6 @@ 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>;
@ -41,14 +39,6 @@ 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)
}
@ -534,52 +524,3 @@ 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,
}
}