Skip to content

Commit

Permalink
Remove usages of internal_span (nushell#14700)
Browse files Browse the repository at this point in the history
# Description
Remove usages of `internal_span` in matches and initializers. I think
this should be the last of the usages, meaning `internal_span` can
finally be refactored out of `Value`(!?)
  • Loading branch information
132ikl authored Dec 30, 2024
1 parent 2bcf238 commit 378395c
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ pub fn new_dotnu_engine() -> (AbsolutePathBuf, String, EngineState, Stack) {

stack.add_env_var(
"NU_LIB_DIRS".to_string(),
Value::List {
vals: vec![
Value::list(
vec![
Value::string(file(dir.join("lib-dir1")), dir_span),
Value::string(file(dir.join("lib-dir2")), dir_span),
Value::string(file(dir.join("lib-dir3")), dir_span),
],
internal_span: dir_span,
},
dir_span,
),
);

// Merge environment into the permanent state
Expand Down
13 changes: 4 additions & 9 deletions crates/nu-command/src/conversions/into/cell_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,11 @@ fn value_to_cell_path(value: &Value, span: Span) -> Result<Value, ShellError> {
}

fn value_to_path_member(val: &Value, span: Span) -> Result<PathMember, ShellError> {
let val_span = val.span();
let member = match val {
Value::Int {
val,
internal_span: span,
} => int_to_path_member(*val, *span)?,
Value::String {
val,
internal_span: span,
} => PathMember::string(val.into(), false, *span),
Value::Record { val, internal_span } => record_to_path_member(val, *internal_span, span)?,
Value::Int { val, .. } => int_to_path_member(*val, val_span)?,
Value::String { val, .. } => PathMember::string(val.into(), false, val_span),
Value::Record { val, .. } => record_to_path_member(val, val_span, span)?,
other => {
return Err(ShellError::CantConvert {
to_type: "int or string".to_string(),
Expand Down
12 changes: 3 additions & 9 deletions crates/nu-command/src/conversions/split_cell_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,8 @@ fn split_cell_path(val: CellPath, span: Span) -> Result<Value, ShellError> {
| PathMember::Int { optional, span, .. } => (optional, span),
};
let value = match pm {
PathMember::String { val, .. } => Value::String { val, internal_span },
PathMember::Int { val, .. } => Value::Int {
val: val as i64,
internal_span,
},
PathMember::String { val, .. } => Value::string(val, internal_span),
PathMember::Int { val, .. } => Value::int(val as i64, internal_span),
};
Self { value, optional }
}
Expand All @@ -142,10 +139,7 @@ fn split_cell_path(val: CellPath, span: Span) -> Result<Value, ShellError> {
})
.collect();

Ok(Value::List {
vals: members,
internal_span: span,
})
Ok(Value::list(members, span))
}

#[cfg(test)]
Expand Down
14 changes: 7 additions & 7 deletions crates/nu-command/src/database/commands/into_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ fn action(
PipelineData::ListStream(stream, _) => {
insert_in_transaction(stream.into_iter(), span, table, signals)
}
PipelineData::Value(
Value::List {
vals,
internal_span,
},
_,
) => insert_in_transaction(vals.into_iter(), internal_span, table, signals),
PipelineData::Value(value @ Value::List { .. }, _) => {
let span = value.span();
let vals = value
.into_list()
.expect("Value matched as list above, but is not a list");
insert_in_transaction(vals.into_iter(), span, table, signals)
}
PipelineData::Value(val, _) => {
insert_in_transaction(std::iter::once(val), span, table, signals)
}
Expand Down
10 changes: 5 additions & 5 deletions crates/nu-command/src/filters/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ produce a table, a list will produce a list, and a record will produce a record.
let columns: Vec<Value> = call.rest(engine_state, stack, 0)?;
let mut new_columns: Vec<CellPath> = vec![];
for col_val in columns {
let col_span = &col_val.span();
let col_span = col_val.span();
match col_val {
Value::CellPath { val, .. } => {
new_columns.push(val);
Expand All @@ -65,25 +65,25 @@ produce a table, a list will produce a list, and a record will produce a record.
let cv = CellPath {
members: vec![PathMember::String {
val,
span: *col_span,
span: col_span,
optional: false,
}],
};
new_columns.push(cv);
}
Value::Int { val, internal_span } => {
Value::Int { val, .. } => {
if val < 0 {
return Err(ShellError::CantConvert {
to_type: "cell path".into(),
from_type: "negative number".into(),
span: internal_span,
span: col_span,
help: None,
});
}
let cv = CellPath {
members: vec![PathMember::Int {
val: val as usize,
span: *col_span,
span: col_span,
optional: false,
}],
};
Expand Down
19 changes: 11 additions & 8 deletions crates/nu-command/src/network/url/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,17 @@ pub fn table_to_query_string(
) -> Result<String, ShellError> {
let row_vec = table
.iter()
.map(|val| match val {
Value::Record { val, internal_span } => key_value_from_record(val, *internal_span),
_ => Err(ShellError::UnsupportedInput {
msg: "expected a table".into(),
input: "not a table, contains non-record values".into(),
msg_span: head,
input_span: span,
}),
.map(|val| {
let val_span = val.span();
match val {
Value::Record { val, .. } => key_value_from_record(val, val_span),
_ => Err(ShellError::UnsupportedInput {
msg: "expected a table".into(),
input: "not a table, contains non-record values".into(),
msg_span: head,
input_span: span,
}),
}
})
.collect::<Result<Vec<_>, ShellError>>()?;

Expand Down
15 changes: 8 additions & 7 deletions crates/nu-command/src/platform/ulimit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,13 @@ fn parse_limit(
hard_limit: rlim_t,
call_span: Span,
) -> Result<rlim_t, ShellError> {
let val_span = limit_value.span();
match limit_value {
Value::Int { val, internal_span } => {
Value::Int { val, .. } => {
let value = rlim_t::try_from(*val).map_err(|e| ShellError::CantConvert {
to_type: "rlim_t".into(),
from_type: "i64".into(),
span: *internal_span,
span: val_span,
help: Some(e.to_string()),
})?;

Expand All @@ -447,25 +448,25 @@ fn parse_limit(
Ok(limit)
}
}
Value::Filesize { val, internal_span } => {
Value::Filesize { val, .. } => {
if res.multiplier != 1024 {
return Err(ShellError::TypeMismatch {
err_message: format!(
"filesize is not compatible with resource {:?}",
res.resource
),
span: *internal_span,
span: val_span,
});
}

rlim_t::try_from(*val).map_err(|e| ShellError::CantConvert {
to_type: "rlim_t".into(),
from_type: "i64".into(),
span: *internal_span,
span: val_span,
help: Some(e.to_string()),
})
}
Value::String { val, internal_span } => {
Value::String { val, .. } => {
if val == "unlimited" {
Ok(RLIM_INFINITY)
} else if val == "soft" {
Expand All @@ -479,7 +480,7 @@ fn parse_limit(
} else {
return Err(ShellError::IncorrectValue {
msg: "Only unlimited, soft and hard are supported for strings".into(),
val_span: *internal_span,
val_span,
call_span,
});
}
Expand Down
5 changes: 3 additions & 2 deletions crates/nu-command/src/viewers/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ fn get_index_flag(
Some(value) => value,
None => return Ok(Some(0)),
};
let span = value.span();

match value {
Value::Bool { val, .. } => {
Expand All @@ -345,13 +346,13 @@ fn get_index_flag(
Ok(None)
}
}
Value::Int { val, internal_span } => {
Value::Int { val, .. } => {
if val < 0 {
Err(ShellError::UnsupportedInput {
msg: String::from("got a negative integer"),
input: val.to_string(),
msg_span: call.span(),
input_span: internal_span,
input_span: span,
})
} else {
Ok(Some(val as usize))
Expand Down
11 changes: 2 additions & 9 deletions crates/nu-protocol/src/engine/engine_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ mod test_cwd {
use crate::{
engine::{EngineState, Stack},
Span, Value,
Value,
};
use nu_path::{assert_path_eq, AbsolutePath, Path};
use tempfile::{NamedTempFile, TempDir};
Expand Down Expand Up @@ -1226,14 +1226,7 @@ mod test_cwd {
#[test]
fn pwd_is_non_string_value() {
let mut engine_state = EngineState::new();
engine_state.add_env_var(
"PWD".into(),
Value::Glob {
val: "*".into(),
no_expand: false,
internal_span: Span::unknown(),
},
);
engine_state.add_env_var("PWD".into(), Value::test_glob("*"));
engine_state.cwd(None).unwrap_err();
}

Expand Down
Loading

0 comments on commit 378395c

Please sign in to comment.