pub struct Record { /* private fields */ }
Implementations§
Source§impl Record
impl Record
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
Sourcepub fn from_raw_cols_vals(
cols: Vec<String>,
vals: Vec<Value>,
input_span: Span,
creation_site_span: Span,
) -> Result<Self, ShellError>
pub fn from_raw_cols_vals( cols: Vec<String>, vals: Vec<Value>, input_span: Span, creation_site_span: Span, ) -> Result<Self, ShellError>
pub fn iter(&self) -> Iter<'_> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_> ⓘ
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
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.
use nu_protocol::{record, Record, Value};
fn remove_foo_recursively(val: &mut Value) {
if let Value::Record {val, ..} = val {
val.to_mut().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 into_columns(self) -> IntoColumns ⓘ
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);
Sourcepub fn sort_cols(&mut self)
pub fn sort_cols(&mut self)
Sort the record by its columns.
use nu_protocol::{record, Value};
let mut rec = record!(
"c" => Value::test_string("foo"),
"b" => Value::test_int(42),
"a" => Value::test_nothing(),
);
rec.sort_cols();
assert_eq!(
Value::test_record(rec),
Value::test_record(record!(
"a" => Value::test_nothing(),
"b" => Value::test_int(42),
"c" => Value::test_string("foo"),
))
);
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>,
Special deserialization implementation that turns a map-pattern into a Record
Denies duplicate keys
use serde_json::{from_str, Result};
use nu_protocol::{Record, Value, record};
// A `Record` in json is a Record with a packed `Value`
// The `Value` record has a single key indicating its type and the inner record describing
// its representation of value and the associated `Span`
let ok = r#"{"a": {"Int": {"val": 42, "span": {"start": 0, "end": 0}}},
"b": {"Int": {"val": 37, "span": {"start": 0, "end": 0}}}}"#;
let ok_rec: Record = from_str(ok).unwrap();
assert_eq!(Value::test_record(ok_rec),
Value::test_record(record!{"a" => Value::test_int(42),
"b" => Value::test_int(37)}));
// A repeated key will lead to a deserialization error
let bad = r#"{"a": {"Int": {"val": 42, "span": {"start": 0, "end": 0}}},
"a": {"Int": {"val": 37, "span": {"start": 0, "end": 0}}}}"#;
let bad_rec: Result<Record> = from_str(bad);
assert!(bad_rec.is_err());
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
Source§fn from_value(v: Value) -> Result<Self, ShellError>
fn from_value(v: Value) -> Result<Self, ShellError>
Source§fn expected_type() -> Type
fn expected_type() -> Type
Value
type. Read moreSource§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 Freeze for Record
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoSpanned for T
impl<T> IntoSpanned for T
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§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 moreSource§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 moreSource§fn fg_rgb<const R: u8, const G: u8, const B: u8>(
&self,
) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>
fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>
Source§fn bg_rgb<const R: u8, const G: u8, const B: u8>(
&self,
) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>
fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>
Source§fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>
fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>
Source§fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>
fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> Serialize for T
impl<T> Serialize for T
fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>
fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>
Source§impl<T> TryIntoValue for Twhere
T: IntoValue,
impl<T> TryIntoValue for Twhere
T: IntoValue,
Source§fn try_into_value(self, span: Span) -> Result<Value, ShellError>
fn try_into_value(self, span: Span) -> Result<Value, ShellError>
Value
.