nushell/crates/nu-cmd-extra
Ian Manske 1c49ca503a
Name the Value conversion functions more clearly (#11851)
# Description
This PR renames the conversion functions on `Value` to be more consistent.
It follows the Rust [API guidelines](https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv) for ad-hoc conversions.
The conversion functions on `Value` now come in a few forms:
- `coerce_{type}` takes a `&Value` and attempts to convert the value to
`type` (e.g., `i64` are converted to `f64`). This is the old behavior of
some of the `as_{type}` functions -- these functions have simply been
renamed to better reflect what they do.
- The new `as_{type}` functions take a `&Value` and returns an `Ok`
result only if the value is of `type` (no conversion is attempted). The
returned value will be borrowed if `type` is non-`Copy`, otherwise an
owned value is returned.
- `into_{type}` exists for non-`Copy` types, but otherwise does not
attempt conversion just like `as_type`. It takes an owned `Value` and
always returns an owned result.
- `coerce_into_{type}` has the same relationship with `coerce_{type}` as
`into_{type}` does with `as_{type}`.
- `to_{kind}_string`: conversion to different string formats (debug,
abbreviated, etc.). Only two of the old string conversion functions were
removed, the rest have been renamed only.
- `to_{type}`: other conversion functions. Currently, only `to_path`
exists. (And `to_string` through `Display`.)

This table summaries the above:
| Form | Cost | Input Ownership | Output Ownership | Converts `Value`
case/`type` |
| ---------------------------- | ----- | --------------- |
---------------- | -------- |
| `as_{type}` | Cheap | Borrowed | Borrowed/Owned | No |
| `into_{type}` | Cheap | Owned | Owned | No |
| `coerce_{type}` | Cheap | Borrowed | Borrowed/Owned | Yes |
| `coerce_into_{type}` | Cheap | Owned | Owned | Yes |
| `to_{kind}_string` | Expensive | Borrowed | Owned | Yes |
| `to_{type}` | Expensive | Borrowed | Owned | Yes |

# User-Facing Changes
Breaking API change for `Value` in `nu-protocol` which is exposed as
part of the plugin API.
2024-02-17 18:14:16 +00:00
..
assets REFACTOR: move the 0% commands to nu-cmd-extra (#9404) 2023-07-06 08:31:31 -07:00
src Name the Value conversion functions more clearly (#11851) 2024-02-17 18:14:16 +00:00
tests Clean up tests containing unnecessary cwd: tokens (#9692) 2023-07-17 18:43:51 +02:00
Cargo.toml bump to dev release of nushell 0.90.2 (#11793) 2024-02-07 16:26:03 -06:00
LICENSE nu-cmd-extra crate infrastructure in place with the Bits command as the model for adding other commands (#9327) 2023-06-01 10:46:16 -07:00
README.md add in a Readme for the crate nu-cmd-extra (#9745) 2023-07-19 21:35:58 -07:00

nu-cmd-extra

the extra commands are not part of the Nushell binary

The commands in this crate are the extra commands of Nushell. They do not get built for the release and it is the responsibility of the developer to build these commands if they want to use them.

These commands are not going to part of the 1.0 Api; meaning that there is no guarantee longer term that these commands will be around into the future. Of course since they are part of the source tree one could always incorporate them into their own custom release.

How to build the commands in this crate

Step 1 is to read the installation notes for Nushell which is located in our Nushell book.

Once Rust is installed you can then build Nushell with the following command.

cargo build --features=extra

Your Nushell binary which just got built is called nu and will be located here.

nushell/target/debug/nu