refactor(cell-path): update Value::follow_cell_path

- remove `insensitive` parameter, case sensitivity is determined by the
  cell-path itself
- update call sites
This commit is contained in:
Bahex 2025-05-04 10:25:55 +03:00
parent 662d7b566f
commit e10538bd8d
17 changed files with 42 additions and 54 deletions

View File

@ -104,7 +104,7 @@ pub(crate) fn eval_cell_path(
eval_constant(working_set, head)
}?;
head_value
.follow_cell_path(path_members, false)
.follow_cell_path(path_members)
.map(Cow::into_owned)
}

View File

@ -256,7 +256,7 @@ fn format_record(
.collect();
let expanded_string = data_as_value
.follow_cell_path(&path_members, false)?
.follow_cell_path(&path_members)?
.to_expanded_string(", ", config);
output.push_str(expanded_string.as_str())
}

View File

@ -181,7 +181,7 @@ fn action(
}
if rest.is_empty() {
follow_cell_path_into_stream(input, signals, cell_path.members, span, !sensitive)
follow_cell_path_into_stream(input, signals, cell_path.members, span)
} else {
let mut output = vec![];
@ -190,11 +190,7 @@ fn action(
let input = input.into_value(span)?;
for path in paths {
output.push(
input
.follow_cell_path(&path.members, !sensitive)?
.into_owned(),
);
output.push(input.follow_cell_path(&path.members)?.into_owned());
}
Ok(output.into_iter().into_pipeline_data(span, signals))
@ -213,7 +209,6 @@ pub fn follow_cell_path_into_stream(
signals: Signals,
cell_path: Vec<PathMember>,
head: Span,
insensitive: bool,
) -> Result<PipelineData, ShellError> {
// when given an integer/indexing, we fallback to
// the default nushell indexing behaviour
@ -228,7 +223,7 @@ pub fn follow_cell_path_into_stream(
let span = value.span();
value
.follow_cell_path(&cell_path, insensitive)
.follow_cell_path(&cell_path)
.map(Cow::into_owned)
.unwrap_or_else(|error| Value::error(error, span))
})
@ -238,7 +233,7 @@ pub fn follow_cell_path_into_stream(
}
_ => data
.follow_cell_path(&cell_path, head, insensitive)
.follow_cell_path(&cell_path, head)
.map(|x| x.into_pipeline_data()),
}
}

View File

@ -322,7 +322,7 @@ fn group_cell_path(
let mut groups = IndexMap::<_, Vec<_>>::new();
for value in values.into_iter() {
let key = value.follow_cell_path(&column_name.members, false)?;
let key = value.follow_cell_path(&column_name.members)?;
if key.is_nothing() {
continue; // likely the result of a failed optional access, ignore this value

View File

@ -301,7 +301,7 @@ fn insert_value_by_closure(
) -> Result<(), ShellError> {
let value_at_path = if first_path_member_int {
value
.follow_cell_path(cell_path, false)
.follow_cell_path(cell_path)
.map(Cow::into_owned)
.unwrap_or(Value::nothing(span))
} else {
@ -321,7 +321,7 @@ fn insert_single_value_by_closure(
) -> Result<(), ShellError> {
let value_at_path = if first_path_member_int {
value
.follow_cell_path(cell_path, false)
.follow_cell_path(cell_path)
.map(Cow::into_owned)
.unwrap_or(Value::nothing(span))
} else {

View File

@ -236,7 +236,7 @@ fn select(
if !columns.is_empty() {
let mut record = Record::new();
for path in &columns {
match input_val.follow_cell_path(&path.members, false) {
match input_val.follow_cell_path(&path.members) {
Ok(fetcher) => {
record.push(path.to_column_name(), fetcher.into_owned());
}
@ -259,7 +259,7 @@ fn select(
let mut record = Record::new();
for cell_path in columns {
let result = v.follow_cell_path(&cell_path.members, false)?;
let result = v.follow_cell_path(&cell_path.members)?;
record.push(cell_path.to_column_name(), result.into_owned());
}
@ -276,7 +276,7 @@ fn select(
if !columns.is_empty() {
let mut record = Record::new();
for path in &columns {
match x.follow_cell_path(&path.members, false) {
match x.follow_cell_path(&path.members) {
Ok(value) => {
record.push(path.to_column_name(), value.into_owned());
}

View File

@ -243,7 +243,7 @@ fn update_value_by_closure(
cell_path: &[PathMember],
first_path_member_int: bool,
) -> Result<(), ShellError> {
let value_at_path = value.follow_cell_path(cell_path, false)?;
let value_at_path = value.follow_cell_path(cell_path)?;
let arg = if first_path_member_int {
value_at_path.as_ref()
@ -266,7 +266,7 @@ fn update_single_value_by_closure(
cell_path: &[PathMember],
first_path_member_int: bool,
) -> Result<(), ShellError> {
let value_at_path = value.follow_cell_path(cell_path, false)?;
let value_at_path = value.follow_cell_path(cell_path)?;
let arg = if first_path_member_int {
value_at_path.as_ref()

View File

@ -321,7 +321,7 @@ fn upsert_value_by_closure(
cell_path: &[PathMember],
first_path_member_int: bool,
) -> Result<(), ShellError> {
let value_at_path = value.follow_cell_path(cell_path, false);
let value_at_path = value.follow_cell_path(cell_path);
let arg = if first_path_member_int {
value_at_path
@ -352,7 +352,7 @@ fn upsert_single_value_by_closure(
cell_path: &[PathMember],
first_path_member_int: bool,
) -> Result<(), ShellError> {
let value_at_path = value.follow_cell_path(cell_path, false);
let value_at_path = value.follow_cell_path(cell_path);
let arg = if first_path_member_int {
value_at_path

View File

@ -89,7 +89,7 @@ impl Command for InputList {
.into_iter()
.map(move |val| {
let display_value = if let Some(ref cellpath) = display_path {
val.follow_cell_path(&cellpath.members, false)?
val.follow_cell_path(&cellpath.members)?
.to_expanded_string(", ", &config)
} else {
val.to_expanded_string(", ", &config)

View File

@ -239,8 +239,8 @@ pub fn compare_cell_path(
insensitive: bool,
natural: bool,
) -> Result<Ordering, ShellError> {
let left = left.follow_cell_path(&cell_path.members, false)?;
let right = right.follow_cell_path(&cell_path.members, false)?;
let left = left.follow_cell_path(&cell_path.members)?;
let right = right.follow_cell_path(&cell_path.members)?;
compare_values(&left, &right, insensitive, natural)
}

View File

@ -268,7 +268,7 @@ pub fn eval_expression_with_input<D: DebugContext>(
// FIXME: protect this collect with ctrl-c
input = eval_subexpression::<D>(engine_state, stack, block, input)?
.into_value(*span)?
.follow_cell_path(&full_cell_path.tail, false)?
.follow_cell_path(&full_cell_path.tail)?
.into_owned()
.into_pipeline_data()
} else {

View File

@ -678,7 +678,7 @@ fn eval_instruction<D: DebugContext>(
let data = ctx.take_reg(*src_dst);
let path = ctx.take_reg(*path);
if let PipelineData::Value(Value::CellPath { val: path, .. }, _) = path {
let value = data.follow_cell_path(&path.members, *span, true)?;
let value = data.follow_cell_path(&path.members, *span)?;
ctx.put_reg(*src_dst, value.into_pipeline_data());
Ok(Continue)
} else if let PipelineData::Value(Value::Error { error, .. }, _) = path {
@ -694,7 +694,7 @@ fn eval_instruction<D: DebugContext>(
let value = ctx.clone_reg_value(*src, *span)?;
let path = ctx.take_reg(*path);
if let PipelineData::Value(Value::CellPath { val: path, .. }, _) = path {
let value = value.follow_cell_path(&path.members, true)?;
let value = value.follow_cell_path(&path.members)?;
ctx.put_reg(*dst, value.into_owned().into_pipeline_data());
Ok(Continue)
} else if let PipelineData::Value(Value::Error { error, .. }, _) = path {

View File

@ -64,7 +64,7 @@ impl LanguageServer {
Some(
var.const_val
.as_ref()
.and_then(|val| val.follow_cell_path(cell_path, false).ok())
.and_then(|val| val.follow_cell_path(cell_path).ok())
.map(|val| val.span())
.unwrap_or(var.declaration_span),
)

View File

@ -161,7 +161,7 @@ impl LanguageServer {
markdown_hover(
var.const_val
.as_ref()
.and_then(|val| val.follow_cell_path(&cell_path, false).ok())
.and_then(|val| val.follow_cell_path(&cell_path).ok())
.map(|val| {
let ty = val.get_type();
if let Ok(s) = val.coerce_str() {

View File

@ -411,16 +411,13 @@ impl PipelineData {
self,
cell_path: &[PathMember],
head: Span,
insensitive: bool,
) -> Result<Value, ShellError> {
match self {
// FIXME: there are probably better ways of doing this
PipelineData::ListStream(stream, ..) => Value::list(stream.into_iter().collect(), head)
.follow_cell_path(cell_path, insensitive)
.map(Cow::into_owned),
PipelineData::Value(v, ..) => v
.follow_cell_path(cell_path, insensitive)
.follow_cell_path(cell_path)
.map(Cow::into_owned),
PipelineData::Value(v, ..) => v.follow_cell_path(cell_path).map(Cow::into_owned),
PipelineData::Empty => Err(ShellError::IncompatiblePathAccess {
type_name: "empty pipeline".to_string(),
span: head,

View File

@ -1083,7 +1083,6 @@ impl Value {
pub fn follow_cell_path<'out>(
&'out self,
cell_path: &[PathMember],
insensitive: bool,
) -> Result<Cow<'out, Value>, ShellError> {
enum MultiLife<'out, 'local, T>
where
@ -1116,7 +1115,7 @@ impl Value {
for member in cell_path {
current = match current {
MultiLife::Out(current) => match get_value_member(current, member, insensitive)? {
MultiLife::Out(current) => match get_value_member(current, member)? {
ControlFlow::Break(span) => return Ok(Cow::Owned(Value::nothing(span))),
ControlFlow::Continue(x) => match x {
Cow::Borrowed(x) => MultiLife::Out(x),
@ -1126,18 +1125,16 @@ impl Value {
}
},
},
MultiLife::Local(current) => {
match get_value_member(current, member, insensitive)? {
ControlFlow::Break(span) => return Ok(Cow::Owned(Value::nothing(span))),
ControlFlow::Continue(x) => match x {
Cow::Borrowed(x) => MultiLife::Local(x),
Cow::Owned(x) => {
store = x;
MultiLife::Local(&store)
}
},
}
}
MultiLife::Local(current) => match get_value_member(current, member)? {
ControlFlow::Break(span) => return Ok(Cow::Owned(Value::nothing(span))),
ControlFlow::Continue(x) => match x {
Cow::Borrowed(x) => MultiLife::Local(x),
Cow::Owned(x) => {
store = x;
MultiLife::Local(&store)
}
},
},
};
}
@ -1166,7 +1163,7 @@ impl Value {
cell_path: &[PathMember],
callback: Box<dyn FnOnce(&Value) -> Value>,
) -> Result<(), ShellError> {
let new_val = callback(self.follow_cell_path(cell_path, false)?.as_ref());
let new_val = callback(self.follow_cell_path(cell_path)?.as_ref());
match new_val {
Value::Error { error, .. } => Err(*error),
@ -1266,7 +1263,7 @@ impl Value {
cell_path: &[PathMember],
callback: Box<dyn FnOnce(&Value) -> Value + 'a>,
) -> Result<(), ShellError> {
let new_val = callback(self.follow_cell_path(cell_path, false)?.as_ref());
let new_val = callback(self.follow_cell_path(cell_path)?.as_ref());
match new_val {
Value::Error { error, .. } => Err(*error),
@ -2007,7 +2004,6 @@ impl Value {
fn get_value_member<'a>(
current: &'a Value,
member: &PathMember,
insensitive: bool,
) -> Result<ControlFlow<Span, Cow<'a, Value>>, ShellError> {
match member {
PathMember::Int {
@ -2100,7 +2096,7 @@ fn get_value_member<'a>(
match current {
Value::Record { val, .. } => {
if let Some(found) = val.iter().rev().find(|x| {
if insensitive {
if *insensitive {
x.0.eq_ignore_case(column_name)
} else {
x.0 == column_name
@ -2134,7 +2130,7 @@ fn get_value_member<'a>(
match val {
Value::Record { val, .. } => {
if let Some(found) = val.iter().rev().find(|x| {
if insensitive {
if *insensitive {
x.0.eq_ignore_case(column_name)
} else {
x.0 == column_name

View File

@ -91,7 +91,7 @@ impl Inc {
pub fn inc(&self, head: Span, value: &Value) -> Result<Value, LabeledError> {
if let Some(cell_path) = &self.cell_path {
let cell_value = value.follow_cell_path(&cell_path.members, false)?;
let cell_value = value.follow_cell_path(&cell_path.members)?;
let cell_value = self.inc_value(head, &cell_value)?;