nushell/crates/nu-protocol/src/value
Stefan Holderbach c87bac04c0
Add common map-like API to nu_protocol::Record (#10841)
# Description

> Our `Record` looks like a map, quacks like a map, so let's treat it
with the API for a map

Implement common methods found on e.g. `std::collections::HashMap` or
the insertion-ordered [indexmap](https://docs.rs/indexmap).

This allows contributors to not have to worry about how to get to the
relevant items and not mess up the assumptions of a Nushell record.

## Record assumptions
- `cols` and `vals` are of equal length
- for all practical purposes, keys/columns should be unique

## End goal

The end goal of the upcoming series of PR's is to allow us to make
`cols` and `vals` private.
Then it would be possible to exchange the backing datastructure to best
fit the expected workload.
This could be statically (by finding the best balance) or dynamically by
using an `enum` of potential representations.

## Parts
- Add validating explicit part constructor
`Record::from_raw_cols_vals()`
- Add `Record.columns()` iterator
- Add `Record.values()` iterator
- Add consuming `Record.into_values()` iterator
- Add `Record.contains()` helper
- Add `Record.insert()` that respects existing keys
- Add key-based `.get()`/`.get_mut()` to `Record`
- Add `Record.get_index()` for index-based access
- Implement `Extend` for `Record` naively
- Use checked constructor in `record!` macro
- Add `Record.index_of()` to get index by key

# User-Facing Changes
None directly

# Developer facing changes
You don't have to roll your own record handling and can use a familiar
API

# Tests + Formatting
No explicit unit tests yet. Wouldn't be too tricky to validate core
properties directly.
Will be exercised by the following PRs using the new
methods/traits/iterators.
2023-10-28 15:18:41 +02:00
..
custom_value.rs Fix to json for SQLite databases (#8343) 2023-03-06 14:36:26 -08:00
from_value.rs Use int type name consistently (#10579) 2023-10-03 18:24:32 +02:00
from.rs Spanned Value step 1: span all value cases (#10042) 2023-08-25 08:48:05 +12:00
lazy_record.rs Create Record type (#10103) 2023-08-25 07:50:29 +12:00
mod.rs Convert more examples and tests to record! macro (#10840) 2023-10-28 14:52:31 +02:00
range.rs Auto format let-else block (#10214) 2023-09-04 19:42:31 +12:00
record.rs Add common map-like API to nu_protocol::Record (#10841) 2023-10-28 15:18:41 +02:00
stream.rs Move Value to helpers, separate span call (#10121) 2023-09-03 07:27:29 -07:00
unit.rs Move Value to helpers, separate span call (#10121) 2023-09-03 07:27:29 -07:00