Struct nu_protocol::Record
source · pub struct Record {
pub cols: Vec<String>,
pub vals: Vec<Value>,
}
Fields§
§cols: Vec<String>
§vals: Vec<Value>
Implementations§
source§impl Record
impl Record
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
pub fn from_raw_cols_vals(cols: Vec<String>, vals: Vec<Value>) -> Self
pub fn iter(&self) -> Zip<Iter<'_, String>, Iter<'_, Value>> ⓘ
pub fn iter_mut(&mut self) -> Zip<Iter<'_, String>, IterMut<'_, Value>> ⓘ
pub fn is_empty(&self) -> bool
pub fn len(&self) -> usize
sourcepub fn push(&mut self, col: impl Into<String>, val: Value)
pub fn push(&mut self, col: impl Into<String>, val: Value)
Naive push to the end of the datastructure.
May duplicate data!
Consider to use Record::insert
instead
sourcepub fn insert<K>(&mut self, col: K, val: Value) -> Option<Value>
pub fn insert<K>(&mut self, col: K, val: Value) -> Option<Value>
Insert into the record, replacing preexisting value if found.
Returns Some(previous_value)
if found. Else None
pub fn contains(&self, col: impl AsRef<str>) -> bool
pub fn index_of(&self, col: impl AsRef<str>) -> Option<usize>
pub fn get(&self, col: impl AsRef<str>) -> Option<&Value>
pub fn get_mut(&mut self, col: impl AsRef<str>) -> Option<&mut Value>
pub fn get_index(&self, idx: usize) -> Option<(&String, &Value)>
sourcepub fn remove(&mut self, col: impl AsRef<str>) -> Option<Value>
pub fn remove(&mut self, col: impl AsRef<str>) -> Option<Value>
Remove single value by key
Returns None
if key not found
Note: makes strong assumption that keys are unique
sourcepub fn retain<F>(&mut self, keep: F)
pub fn retain<F>(&mut self, keep: F)
Remove elements in-place that do not satisfy keep
Note: Panics if vals.len() > cols.len()
use nu_protocol::{record, Value};
let mut rec = record!(
"a" => Value::test_nothing(),
"b" => Value::test_int(42),
"c" => Value::test_nothing(),
"d" => Value::test_int(42),
);
rec.retain(|_k, val| !val.is_nothing());
let mut iter_rec = rec.columns();
assert_eq!(iter_rec.next().map(String::as_str), Some("b"));
assert_eq!(iter_rec.next().map(String::as_str), Some("d"));
assert_eq!(iter_rec.next(), None);
sourcepub fn retain_mut<F>(&mut self, keep: F)
pub fn retain_mut<F>(&mut self, keep: F)
Remove elements in-place that do not satisfy keep
while allowing mutation of the value.
This can for example be used to recursively prune nested records.
Note: Panics if vals.len() > cols.len()
use nu_protocol::{record, Record, Value};
fn remove_foo_recursively(val: &mut Value) {
if let Value::Record {val, ..} = val {
val.retain_mut(keep_non_foo);
}
}
fn keep_non_foo(k: &str, v: &mut Value) -> bool {
if k == "foo" {
return false;
}
remove_foo_recursively(v);
true
}
let mut test = Value::test_record(record!(
"foo" => Value::test_nothing(),
"bar" => Value::test_record(record!(
"foo" => Value::test_nothing(),
"baz" => Value::test_nothing(),
))
));
remove_foo_recursively(&mut test);
let expected = Value::test_record(record!(
"bar" => Value::test_record(record!(
"baz" => Value::test_nothing(),
))
));
assert_eq!(test, expected);
sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Truncate record to the first len
elements.
len > self.len()
will be ignored
use nu_protocol::{record, Value};
let mut rec = record!(
"a" => Value::test_nothing(),
"b" => Value::test_int(42),
"c" => Value::test_nothing(),
"d" => Value::test_int(42),
);
rec.truncate(42); // this is fine
assert_eq!(rec.columns().map(String::as_str).collect::<String>(), "abcd");
rec.truncate(2); // truncate
assert_eq!(rec.columns().map(String::as_str).collect::<String>(), "ab");
rec.truncate(0); // clear the record
assert_eq!(rec.len(), 0);
pub fn columns(&self) -> Columns<'_>
pub fn values(&self) -> Values<'_>
pub fn into_values(self) -> IntoValues
sourcepub fn drain<R>(&mut self, range: R) -> Drain<'_>
pub fn drain<R>(&mut self, range: R) -> Drain<'_>
Obtain an iterator to remove elements in range
Elements not consumed from the iterator will be dropped
use nu_protocol::{record, Value};
let mut rec = record!(
"a" => Value::test_nothing(),
"b" => Value::test_int(42),
"c" => Value::test_string("foo"),
);
{
let mut drainer = rec.drain(1..);
assert_eq!(drainer.next(), Some(("b".into(), Value::test_int(42))));
// Dropping the `Drain`
}
let mut rec_iter = rec.into_iter();
assert_eq!(rec_iter.next(), Some(("a".into(), Value::test_nothing())));
assert_eq!(rec_iter.next(), None);
Trait Implementations§
source§impl<'de> Deserialize<'de> for Record
impl<'de> Deserialize<'de> for Record
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl Extend<(String, Value)> for Record
impl Extend<(String, Value)> for Record
source§fn extend<T: IntoIterator<Item = (String, Value)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (String, Value)>>(&mut self, iter: T)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl FromValue for Record
impl FromValue for Record
fn from_value(v: Value) -> Result<Self, ShellError>
source§impl<'a> IntoIterator for &'a Record
impl<'a> IntoIterator for &'a Record
source§impl<'a> IntoIterator for &'a mut Record
impl<'a> IntoIterator for &'a mut Record
source§impl IntoIterator for Record
impl IntoIterator for Record
Auto Trait Implementations§
impl !RefUnwindSafe for Record
impl Send for Record
impl Sync for Record
impl Unpin for Record
impl !UnwindSafe for Record
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<D> OwoColorize for D
impl<D> OwoColorize for D
§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
§fn on_yellow<'a>(&'a self) -> BgColorDisplay<'a, Yellow, Self>
fn on_yellow<'a>(&'a self) -> BgColorDisplay<'a, Yellow, Self>
§fn magenta<'a>(&'a self) -> FgColorDisplay<'a, Magenta, Self>
fn magenta<'a>(&'a self) -> FgColorDisplay<'a, Magenta, Self>
§fn on_magenta<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
fn on_magenta<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
§fn on_purple<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
fn on_purple<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
§fn default_color<'a>(&'a self) -> FgColorDisplay<'a, Default, Self>
fn default_color<'a>(&'a self) -> FgColorDisplay<'a, Default, Self>
§fn on_default_color<'a>(&'a self) -> BgColorDisplay<'a, Default, Self>
fn on_default_color<'a>(&'a self) -> BgColorDisplay<'a, Default, Self>
§fn bright_black<'a>(&'a self) -> FgColorDisplay<'a, BrightBlack, Self>
fn bright_black<'a>(&'a self) -> FgColorDisplay<'a, BrightBlack, Self>
§fn on_bright_black<'a>(&'a self) -> BgColorDisplay<'a, BrightBlack, Self>
fn on_bright_black<'a>(&'a self) -> BgColorDisplay<'a, BrightBlack, Self>
§fn bright_red<'a>(&'a self) -> FgColorDisplay<'a, BrightRed, Self>
fn bright_red<'a>(&'a self) -> FgColorDisplay<'a, BrightRed, Self>
§fn on_bright_red<'a>(&'a self) -> BgColorDisplay<'a, BrightRed, Self>
fn on_bright_red<'a>(&'a self) -> BgColorDisplay<'a, BrightRed, Self>
§fn bright_green<'a>(&'a self) -> FgColorDisplay<'a, BrightGreen, Self>
fn bright_green<'a>(&'a self) -> FgColorDisplay<'a, BrightGreen, Self>
§fn on_bright_green<'a>(&'a self) -> BgColorDisplay<'a, BrightGreen, Self>
fn on_bright_green<'a>(&'a self) -> BgColorDisplay<'a, BrightGreen, Self>
§fn bright_yellow<'a>(&'a self) -> FgColorDisplay<'a, BrightYellow, Self>
fn bright_yellow<'a>(&'a self) -> FgColorDisplay<'a, BrightYellow, Self>
§fn on_bright_yellow<'a>(&'a self) -> BgColorDisplay<'a, BrightYellow, Self>
fn on_bright_yellow<'a>(&'a self) -> BgColorDisplay<'a, BrightYellow, Self>
§fn bright_blue<'a>(&'a self) -> FgColorDisplay<'a, BrightBlue, Self>
fn bright_blue<'a>(&'a self) -> FgColorDisplay<'a, BrightBlue, Self>
§fn on_bright_blue<'a>(&'a self) -> BgColorDisplay<'a, BrightBlue, Self>
fn on_bright_blue<'a>(&'a self) -> BgColorDisplay<'a, BrightBlue, Self>
§fn bright_magenta<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
fn bright_magenta<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
§fn on_bright_magenta<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
fn on_bright_magenta<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
§fn bright_purple<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
fn bright_purple<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
§fn on_bright_purple<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
fn on_bright_purple<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
§fn bright_cyan<'a>(&'a self) -> FgColorDisplay<'a, BrightCyan, Self>
fn bright_cyan<'a>(&'a self) -> FgColorDisplay<'a, BrightCyan, Self>
§fn on_bright_cyan<'a>(&'a self) -> BgColorDisplay<'a, BrightCyan, Self>
fn on_bright_cyan<'a>(&'a self) -> BgColorDisplay<'a, BrightCyan, Self>
§fn bright_white<'a>(&'a self) -> FgColorDisplay<'a, BrightWhite, Self>
fn bright_white<'a>(&'a self) -> FgColorDisplay<'a, BrightWhite, Self>
§fn on_bright_white<'a>(&'a self) -> BgColorDisplay<'a, BrightWhite, Self>
fn on_bright_white<'a>(&'a self) -> BgColorDisplay<'a, BrightWhite, Self>
§fn blink_fast<'a>(&'a self) -> BlinkFastDisplay<'a, Self>
fn blink_fast<'a>(&'a self) -> BlinkFastDisplay<'a, Self>
§fn strikethrough<'a>(&'a self) -> StrikeThroughDisplay<'a, Self>
fn strikethrough<'a>(&'a self) -> StrikeThroughDisplay<'a, Self>
§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg
or
a color-specific method, such as OwoColorize::green
, Read more§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg
or
a color-specific method, such as OwoColorize::on_yellow
, Read more