pub struct Record { /* private fields */ }
Implementations§
Source§impl Record
impl Record
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
pub fn cased(&self, casing: Casing) -> DynCasedRecord<&Record>
pub fn cased_mut(&mut self, casing: Casing) -> DynCasedRecord<&mut Record>
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 using CasedRecord::insert
or DynCasedRecord::insert
instead.
pub fn get_index(&self, idx: usize) -> Option<(&String, &Value)>
pub fn get_index_mut(&mut self, idx: usize) -> Option<(&mut String, &mut Value)>
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"),
))
);
Methods from Deref<Target = CasedRecord<CaseSensitive>>§
pub fn index_of(&self, col: impl AsRef<str>) -> Option<usize>
pub fn contains(&self, col: impl AsRef<str>) -> bool
pub fn get(&self, col: impl AsRef<str>) -> Option<&Value>
pub fn get_mut(&mut self, col: impl AsRef<str>) -> Option<&mut Value>
Trait Implementations§
Source§impl Deref for Record
impl Deref for Record
Source§type Target = CasedRecord<CaseSensitive>
type Target = CasedRecord<CaseSensitive>
The resulting type after dereferencing.
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)
Extends a collection with the contents of an iterator. Read more
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one
)Extends a collection with exactly one element.
Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
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
Expected
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
Source§impl<'a> WrapCased for &'a Record
impl<'a> WrapCased for &'a Record
Source§type Wrapper<S: CaseSensitivity> = &'a CasedRecord<S>
type Wrapper<S: CaseSensitivity> = &'a CasedRecord<S>
Wrapper type generic over case sensitivity.
fn case_sensitive(self) -> Self::Wrapper<CaseSensitive>
fn case_insensitive(self) -> Self::Wrapper<CaseInsensitive>
Source§impl<'a> WrapCased for &'a mut Record
impl<'a> WrapCased for &'a mut Record
Source§type Wrapper<S: CaseSensitivity> = &'a mut CasedRecord<S>
type Wrapper<S: CaseSensitivity> = &'a mut CasedRecord<S>
Wrapper type generic over case sensitivity.
fn case_sensitive(self) -> Self::Wrapper<CaseSensitive>
fn case_insensitive(self) -> Self::Wrapper<CaseInsensitive>
impl StructuralPartialEq 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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
Converts
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>
Converts
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,
Set the foreground color generically Read more
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Set the background color generically. Read more
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Change the foreground color to black
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Change the background color to black
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Change the foreground color to red
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Change the background color to red
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Change the foreground color to green
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Change the background color to green
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Change the foreground color to yellow
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Change the background color to yellow
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Change the foreground color to blue
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Change the background color to blue
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Change the foreground color to magenta
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Change the background color to magenta
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Change the foreground color to purple
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Change the background color to purple
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Change the foreground color to cyan
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Change the background color to cyan
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Change the foreground color to white
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Change the background color to white
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Change the foreground color to the terminal default
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Change the background color to the terminal default
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Change the foreground color to bright black
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Change the background color to bright black
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Change the foreground color to bright red
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Change the background color to bright red
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Change the foreground color to bright green
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Change the background color to bright green
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Change the foreground color to bright yellow
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Change the background color to bright yellow
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Change the foreground color to bright blue
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Change the background color to bright blue
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Change the foreground color to bright magenta
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Change the background color to bright magenta
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Change the foreground color to bright purple
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Change the background color to bright purple
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Change the foreground color to bright cyan
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Change the background color to bright cyan
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Change the foreground color to bright white
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Change the background color to bright white
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Make the text bold
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Make the text dim
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Make the text italicized
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Make the text underlined
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Make the text blink
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Make the text blink (but fast!)
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Swap the foreground and background colors
Hide the text
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Cross out the text
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,
Set the foreground color at runtime. Only use if you do not know which color will be used at
compile-time. If the color is constant, use either
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,
Set the background color at runtime. Only use if you do not know what color to use at
compile-time. If the color is constant, use either
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>
Set the foreground color to a specific RGB value.
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>
Set the background color to a specific RGB value.
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>
Sets the foreground color to an RGB value.
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>
Sets the background color to an RGB value.
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>
Tries to convert the given value into a
Value
.