Repository: nushell/nushell
File: crates/nu-protocol/src/value/mod.rs (Rust)

Code before:
                                available_idx: vals.len(),
                                span: *span,
                            });
                        } else {
                            // If the insert is at 1 + the end of the list, it's OK.
                            let new_val_span = new_val.span();
                            vals.push(Value::with_data_at_cell_path(path, new_val, new_val_span)?);
                        }
                    }
                    _ => {
                        return Err(ShellError::NotAList {
                            dst_span: *span,
                            src_span: v_span,
                        });
                    }
                },
            }
        } else {
            *self = new_val;
        }
        Ok(())
    }

    /// Creates a new [Value] with the specified member at the specified path.
    /// This is used by [Value::insert_data_at_cell_path] and [Value::upsert_data_at_cell_path] whenever they have the need to insert a non-existent element
    pub fn with_data_at_cell_path(
        cell_path: &[PathMember],
        value: Value,
        span: Span,
    ) -> Result<Value, ShellError> {
        if let Some((member, path)) = cell_path.split_first() {
            match member {
                PathMember::String { val, .. } => Ok(Value::record(
                    std::iter::once((
                        val.clone(),
                        Value::with_data_at_cell_path(path, value, span)?,
                    ))
                    .collect(),
                    span,
                )),
                PathMember::Int { val, .. } => {
                    if *val == 0usize {
                        Ok(Value::list(
                            vec![Value::with_data_at_cell_path(path, value, span)?],
                            span,
                        ))
                    } else {
                        Err(ShellError::InsertAfterNextFreeIndex {
                            available_idx: 0,
                            span,
                        })

Review comment (suggestion):
```suggestion
    fn with_data_at_cell_path(
```

This can be private I think.

Code after:
                        });
                    }
                },
            }
        } else {
            *self = new_val;
        }
        Ok(())
    }

    /// Creates a new [Value] with the specified member at the specified path.
    /// This is used by [Value::insert_data_at_cell_path] and [Value::upsert_data_at_cell_path] whenever they have the need to insert a non-existent element
    fn with_data_at_cell_path(cell_path: &[PathMember], value: Value) -> Result<Value, ShellError> {
        if let Some((member, path)) = cell_path.split_first() {
            let span = value.span();
            match member {
                PathMember::String { val, .. } => Ok(Value::record(
                    std::iter::once((val.clone(), Value::with_data_at_cell_path(path, value)?))
                        .collect(),
                    span,
                )),
                PathMember::Int { val, .. } => {
                    if *val == 0usize {
                        Ok(Value::list(
                            vec![Value::with_data_at_cell_path(path, value)?],
                            span,
                        ))
                    } else {
                        Err(ShellError::InsertAfterNextFreeIndex {
                            available_idx: 0,
                            span,
                        })
                    }
                }
            }
        } else {
            Ok(value)
        }
    }

    /// Visits all values contained within the value (including this value) with a mutable reference
    /// given to the closure.
    ///
    /// If the closure returns `Err`, the traversal will stop.
    ///
    /// Captures of closure values are currently visited, as they are values owned by the closure.
    pub fn recurse_mut<E>(
        &mut self,
        f: &mut impl FnMut(&mut Value) -> Result<(), E>,
    ) -> Result<(), E> {
        // Visit this value