mirror of
https://github.com/nushell/nushell.git
synced 2025-07-01 07:00:37 +02:00
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.
This commit is contained in:
@ -2646,7 +2646,7 @@ pub fn parse_overlay_new(working_set: &mut StateWorkingSet, call: Box<Call>) ->
|
||||
|
||||
let (overlay_name, _) = if let Some(expr) = call.positional_nth(0) {
|
||||
match eval_constant(working_set, expr) {
|
||||
Ok(val) => match val.as_string() {
|
||||
Ok(val) => match val.coerce_into_string() {
|
||||
Ok(s) => (s, expr.span),
|
||||
Err(err) => {
|
||||
working_set.error(err.wrap(working_set, call_span));
|
||||
@ -2695,7 +2695,7 @@ pub fn parse_overlay_use(working_set: &mut StateWorkingSet, call: Box<Call>) ->
|
||||
|
||||
let (overlay_name, overlay_name_span) = if let Some(expr) = call.positional_nth(0) {
|
||||
match eval_constant(working_set, expr) {
|
||||
Ok(val) => match val.as_string() {
|
||||
Ok(val) => match val.coerce_into_string() {
|
||||
Ok(s) => (s, expr.span),
|
||||
Err(err) => {
|
||||
working_set.error(err.wrap(working_set, call_span));
|
||||
@ -2718,7 +2718,7 @@ pub fn parse_overlay_use(working_set: &mut StateWorkingSet, call: Box<Call>) ->
|
||||
let new_name = if let Some(kw_expression) = call.positional_nth(1) {
|
||||
if let Some(new_name_expression) = kw_expression.as_keyword() {
|
||||
match eval_constant(working_set, new_name_expression) {
|
||||
Ok(val) => match val.as_string() {
|
||||
Ok(val) => match val.coerce_into_string() {
|
||||
Ok(s) => Some(Spanned {
|
||||
item: s,
|
||||
span: new_name_expression.span,
|
||||
@ -2916,7 +2916,7 @@ pub fn parse_overlay_hide(working_set: &mut StateWorkingSet, call: Box<Call>) ->
|
||||
|
||||
let (overlay_name, overlay_name_span) = if let Some(expr) = call.positional_nth(0) {
|
||||
match eval_constant(working_set, expr) {
|
||||
Ok(val) => match val.as_string() {
|
||||
Ok(val) => match val.coerce_into_string() {
|
||||
Ok(s) => (s, expr.span),
|
||||
Err(err) => {
|
||||
working_set.error(err.wrap(working_set, call_span));
|
||||
@ -3383,7 +3383,7 @@ pub fn parse_source(working_set: &mut StateWorkingSet, spans: &[Span]) -> Pipeli
|
||||
}
|
||||
};
|
||||
|
||||
let filename = match val.as_string() {
|
||||
let filename = match val.coerce_into_string() {
|
||||
Ok(s) => s,
|
||||
Err(err) => {
|
||||
working_set.error(err.wrap(working_set, span(&spans[1..])));
|
||||
@ -3590,7 +3590,7 @@ pub fn parse_register(working_set: &mut StateWorkingSet, spans: &[Span]) -> Pipe
|
||||
let val =
|
||||
eval_constant(working_set, expr).map_err(|err| err.wrap(working_set, call.head))?;
|
||||
let filename = val
|
||||
.as_string()
|
||||
.coerce_into_string()
|
||||
.map_err(|err| err.wrap(working_set, call.head))?;
|
||||
|
||||
let Some(path) = find_in_dirs(&filename, working_set, &cwd, PLUGIN_DIRS_VAR) else {
|
||||
@ -3800,7 +3800,7 @@ pub fn find_in_dirs(
|
||||
.ok()?
|
||||
.iter()
|
||||
.map(|lib_dir| -> Option<PathBuf> {
|
||||
let dir = lib_dir.as_path().ok()?;
|
||||
let dir = lib_dir.to_path().ok()?;
|
||||
let dir_abs = canonicalize_with(dir, actual_cwd).ok()?;
|
||||
canonicalize_with(filename, dir_abs).ok()
|
||||
})
|
||||
@ -3833,7 +3833,7 @@ pub fn find_in_dirs(
|
||||
if let Some(lib_dirs) = working_set.get_env_var(dirs_env) {
|
||||
if let Ok(dirs) = lib_dirs.as_list() {
|
||||
for lib_dir in dirs {
|
||||
if let Ok(dir) = lib_dir.as_path() {
|
||||
if let Ok(dir) = lib_dir.to_path() {
|
||||
// make sure the dir is absolute path
|
||||
if let Ok(dir_abs) = canonicalize_with(dir, actual_cwd) {
|
||||
if let Ok(path) = canonicalize_with(filename, dir_abs) {
|
||||
|
@ -2795,7 +2795,7 @@ pub fn parse_import_pattern(working_set: &mut StateWorkingSet, spans: &[Span]) -
|
||||
let head_expr = parse_value(working_set, *head_span, &SyntaxShape::Any);
|
||||
|
||||
let (maybe_module_id, head_name) = match eval_constant(working_set, &head_expr) {
|
||||
Ok(val) => match val.as_string() {
|
||||
Ok(val) => match val.coerce_into_string() {
|
||||
Ok(s) => (working_set.find_module(s.as_bytes()), s.into_bytes()),
|
||||
Err(err) => {
|
||||
working_set.error(err.wrap(working_set, span(spans)));
|
||||
|
Reference in New Issue
Block a user