Skip to content

Commit

Permalink
Add functions for each Value case (#9736)
Browse files Browse the repository at this point in the history
# Description
This PR ensures functions exist to extract and create each and every
`Value` case. It also renames `Value::boolean` to `Value::bool` to match
`Value::test_bool`, `Value::as_bool`, and `Value::Bool`. Similarly,
`Value::as_integer` was renamed to `Value::as_int` to be consistent with
`Value::int`, `Value::test_int`, and `Value::Int`. These two renames can
be undone if necessary.

# User-Facing Changes
No user facing changes, but two public functions were renamed which may
affect downstream dependents.
  • Loading branch information
IanManske committed Jul 21, 2023
1 parent 0b1e368 commit 7e1b922
Show file tree
Hide file tree
Showing 30 changed files with 384 additions and 187 deletions.
4 changes: 2 additions & 2 deletions crates/nu-cli/src/menus/menu_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ fn convert_to_suggestions(
Some(span @ Value::Record { .. }) => {
let start = span
.get_data_by_key("start")
.and_then(|val| val.as_integer().ok());
.and_then(|val| val.as_int().ok());
let end = span
.get_data_by_key("end")
.and_then(|val| val.as_integer().ok());
.and_then(|val| val.as_int().ok());
match (start, end) {
(Some(start), Some(end)) => {
let start = start.min(end);
Expand Down
20 changes: 10 additions & 10 deletions crates/nu-cli/src/reedline_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,23 +183,23 @@ pub(crate) fn add_columnar_menu(
if let Value::Record { cols, vals, span } = &menu.menu_type {
columnar_menu = match extract_value("columns", cols, vals, span) {
Ok(columns) => {
let columns = columns.as_integer()?;
let columns = columns.as_int()?;
columnar_menu.with_columns(columns as u16)
}
Err(_) => columnar_menu,
};

columnar_menu = match extract_value("col_width", cols, vals, span) {
Ok(col_width) => {
let col_width = col_width.as_integer()?;
let col_width = col_width.as_int()?;
columnar_menu.with_column_width(Some(col_width as usize))
}
Err(_) => columnar_menu.with_column_width(None),
};

columnar_menu = match extract_value("col_padding", cols, vals, span) {
Ok(col_padding) => {
let col_padding = col_padding.as_integer()?;
let col_padding = col_padding.as_int()?;
columnar_menu.with_column_padding(col_padding as usize)
}
Err(_) => columnar_menu,
Expand Down Expand Up @@ -285,7 +285,7 @@ pub(crate) fn add_list_menu(
if let Value::Record { cols, vals, span } = &menu.menu_type {
list_menu = match extract_value("page_size", cols, vals, span) {
Ok(page_size) => {
let page_size = page_size.as_integer()?;
let page_size = page_size.as_int()?;
list_menu.with_page_size(page_size as usize)
}
Err(_) => list_menu,
Expand Down Expand Up @@ -371,39 +371,39 @@ pub(crate) fn add_description_menu(
if let Value::Record { cols, vals, span } = &menu.menu_type {
description_menu = match extract_value("columns", cols, vals, span) {
Ok(columns) => {
let columns = columns.as_integer()?;
let columns = columns.as_int()?;
description_menu.with_columns(columns as u16)
}
Err(_) => description_menu,
};

description_menu = match extract_value("col_width", cols, vals, span) {
Ok(col_width) => {
let col_width = col_width.as_integer()?;
let col_width = col_width.as_int()?;
description_menu.with_column_width(Some(col_width as usize))
}
Err(_) => description_menu.with_column_width(None),
};

description_menu = match extract_value("col_padding", cols, vals, span) {
Ok(col_padding) => {
let col_padding = col_padding.as_integer()?;
let col_padding = col_padding.as_int()?;
description_menu.with_column_padding(col_padding as usize)
}
Err(_) => description_menu,
};

description_menu = match extract_value("selection_rows", cols, vals, span) {
Ok(selection_rows) => {
let selection_rows = selection_rows.as_integer()?;
let selection_rows = selection_rows.as_int()?;
description_menu.with_selection_rows(selection_rows as u16)
}
Err(_) => description_menu,
};

description_menu = match extract_value("description_rows", cols, vals, span) {
Ok(description_rows) => {
let description_rows = description_rows.as_integer()?;
let description_rows = description_rows.as_int()?;
description_menu.with_description_rows(description_rows as usize)
}
Err(_) => description_menu,
Expand Down Expand Up @@ -884,7 +884,7 @@ fn edit_from_record(
"movebigwordrightstart" => EditCommand::MoveBigWordRightStart,
"movetoposition" => {
let value = extract_value("value", cols, vals, span)?;
EditCommand::MoveToPosition(value.as_integer()? as usize)
EditCommand::MoveToPosition(value.as_int()? as usize)
}
"insertchar" => {
let value = extract_value("value", cols, vals, span)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-cli/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,14 @@ pub fn evaluate_repl(

let current_shell = stack.get_env_var(engine_state, "NUSHELL_CURRENT_SHELL");
let current_shell = if let Some(v) = current_shell {
v.as_integer().unwrap_or_default() as usize
v.as_int().unwrap_or_default() as usize
} else {
0
};

let last_shell = stack.get_env_var(engine_state, "NUSHELL_LAST_SHELL");
let last_shell = if let Some(v) = last_shell {
v.as_integer().unwrap_or_default() as usize
v.as_int().unwrap_or_default() as usize
} else {
0
};
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cmd-dataframe/src/dataframe/series/all_false.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn command(
)
})?;

let value = Value::boolean(!bool.any(), call.head);
let value = Value::bool(!bool.any(), call.head);

NuDataFrame::try_from_columns(vec![Column::new("all_false".to_string(), vec![value])])
.map(|df| PipelineData::Value(NuDataFrame::into_value(df, call.head), None))
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cmd-dataframe/src/dataframe/series/all_true.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn command(
)
})?;

let value = Value::boolean(bool.all(), call.head);
let value = Value::bool(bool.all(), call.head);

NuDataFrame::try_from_columns(vec![Column::new("all_true".to_string(), vec![value])])
.map(|df| PipelineData::Value(NuDataFrame::into_value(df, call.head), None))
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cmd-extra/src/extra/bytes/ends_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn ends_with(val: &Value, args: &Arguments, span: Span) -> Value {
Value::Binary {
val,
span: val_span,
} => Value::boolean(val.ends_with(&args.pattern), *val_span),
} => Value::bool(val.ends_with(&args.pattern), *val_span),
// Propagate errors by explicitly matching them before the final case.
Value::Error { .. } => val.clone(),
other => Value::Error {
Expand Down
8 changes: 4 additions & 4 deletions crates/nu-cmd-extra/src/extra/bytes/starts_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,16 @@ impl Command for BytesStartsWith {
i += max;

if i >= arg.pattern.len() {
return Ok(Value::boolean(true, span).into_pipeline_data());
return Ok(Value::bool(true, span).into_pipeline_data());
}
} else {
return Ok(Value::boolean(false, span).into_pipeline_data());
return Ok(Value::bool(false, span).into_pipeline_data());
}
}

// We reached the end of the stream and never returned,
// the pattern wasn't exhausted so it probably doesn't match
Ok(Value::boolean(false, span).into_pipeline_data())
Ok(Value::bool(false, span).into_pipeline_data())
}
_ => operate(
starts_with,
Expand Down Expand Up @@ -145,7 +145,7 @@ fn starts_with(val: &Value, args: &Arguments, span: Span) -> Value {
Value::Binary {
val,
span: val_span,
} => Value::boolean(val.starts_with(&args.pattern), *val_span),
} => Value::bool(val.starts_with(&args.pattern), *val_span),
// Propagate errors by explicitly matching them before the final case.
Value::Error { .. } => val.clone(),
other => Value::Error {
Expand Down
20 changes: 10 additions & 10 deletions crates/nu-command/src/conversions/into/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,27 @@ impl Command for SubCommand {
vals: vec![
Value::Record {
cols: vec!["value".to_string()],
vals: vec![Value::boolean(false, span)],
vals: vec![Value::bool(false, span)],
span,
},
Value::Record {
cols: vec!["value".to_string()],
vals: vec![Value::boolean(true, span)],
vals: vec![Value::bool(true, span)],
span,
},
Value::Record {
cols: vec!["value".to_string()],
vals: vec![Value::boolean(false, span)],
vals: vec![Value::bool(false, span)],
span,
},
Value::Record {
cols: vec!["value".to_string()],
vals: vec![Value::boolean(true, span)],
vals: vec![Value::bool(true, span)],
span,
},
Value::Record {
cols: vec!["value".to_string()],
vals: vec![Value::boolean(true, span)],
vals: vec![Value::bool(true, span)],
span,
},
],
Expand All @@ -89,27 +89,27 @@ impl Command for SubCommand {
Example {
description: "Convert bool to boolean",
example: "true | into bool",
result: Some(Value::boolean(true, span)),
result: Some(Value::bool(true, span)),
},
Example {
description: "convert integer to boolean",
example: "1 | into bool",
result: Some(Value::boolean(true, span)),
result: Some(Value::bool(true, span)),
},
Example {
description: "convert decimal to boolean",
example: "0.3 | into bool",
result: Some(Value::boolean(true, span)),
result: Some(Value::bool(true, span)),
},
Example {
description: "convert decimal string to boolean",
example: "'0.0' | into bool",
result: Some(Value::boolean(false, span)),
result: Some(Value::bool(false, span)),
},
Example {
description: "convert string to boolean",
example: "'true' | into bool",
result: Some(Value::boolean(true, span)),
result: Some(Value::bool(true, span)),
},
]
}
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/conversions/into/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Command for SubCommand {
example: "[[value]; [false]] | into record",
result: Some(Value::Record {
cols: vec!["value".to_string()],
vals: vec![Value::boolean(false, span)],
vals: vec![Value::bool(false, span)],
span,
}),
},
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/experimental/is_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Command for IsAdmin {
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
Ok(Value::boolean(is_root(), call.head).into_pipeline_data())
Ok(Value::bool(is_root(), call.head).into_pipeline_data())
}

fn examples(&self) -> Vec<Example> {
Expand Down
6 changes: 3 additions & 3 deletions crates/nu-command/src/filters/drop/nth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ impl Command for DropNth {
rows
}
Either::Right(row_range) => {
let from = row_range.from.as_integer()?; // as usize;
let to = row_range.to.as_integer()?; // as usize;
let from = row_range.from.as_int()?; // as usize;
let to = row_range.to.as_int()?; // as usize;

// check for negative range inputs, e.g., (2..-5)
if from.is_negative() || to.is_negative() {
Expand Down Expand Up @@ -187,7 +187,7 @@ fn extract_int_or_range(
) -> Result<Either<i64, Range>, ShellError> {
let value = call.req::<Value>(engine_state, stack, 0)?;

let int_opt = value.as_integer().map(Either::Left).ok();
let int_opt = value.as_int().map(Either::Left).ok();
let range_opt: Result<nu_protocol::Range, ShellError> = FromValue::from_value(&value);

let range_opt = range_opt.map(Either::Right).ok();
Expand Down
12 changes: 6 additions & 6 deletions crates/nu-command/src/filters/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ fn empty(
let val = val.clone();
match val.follow_cell_path(&column.members, false) {
Ok(Value::Nothing { .. }) => {}
Ok(_) => return Ok(Value::boolean(false, head).into_pipeline_data()),
Ok(_) => return Ok(Value::bool(false, head).into_pipeline_data()),
Err(err) => return Err(err),
}
}
}

Ok(Value::boolean(true, head).into_pipeline_data())
Ok(Value::bool(true, head).into_pipeline_data())
} else {
match input {
PipelineData::Empty => Ok(PipelineData::Empty),
Expand All @@ -91,17 +91,17 @@ fn empty(
let bytes = s.into_bytes();

match bytes {
Ok(s) => Ok(Value::boolean(s.item.is_empty(), head).into_pipeline_data()),
Ok(s) => Ok(Value::bool(s.item.is_empty(), head).into_pipeline_data()),
Err(err) => Err(err),
}
}
None => Ok(Value::boolean(true, head).into_pipeline_data()),
None => Ok(Value::bool(true, head).into_pipeline_data()),
},
PipelineData::ListStream(s, ..) => {
Ok(Value::boolean(s.count() == 0, head).into_pipeline_data())
Ok(Value::bool(s.count() == 0, head).into_pipeline_data())
}
PipelineData::Value(value, ..) => {
Ok(Value::boolean(value.is_empty(), head).into_pipeline_data())
Ok(Value::bool(value.is_empty(), head).into_pipeline_data())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/formats/from/ods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn from_ods(
DataType::String(s) => Value::string(s, head),
DataType::Float(f) => Value::float(*f, head),
DataType::Int(i) => Value::int(*i, head),
DataType::Bool(b) => Value::boolean(*b, head),
DataType::Bool(b) => Value::bool(*b, head),
_ => Value::nothing(head),
};

Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/formats/from/xlsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ fn from_xlsx(
DataType::String(s) => Value::string(s, head),
DataType::Float(f) => Value::float(*f, head),
DataType::Int(i) => Value::int(*i, head),
DataType::Bool(b) => Value::boolean(*b, head),
DataType::Bool(b) => Value::bool(*b, head),
_ => Value::nothing(head),
};

Expand Down
6 changes: 3 additions & 3 deletions crates/nu-command/src/random/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ fn integer(

let (min, max) = if let Some(r) = range {
if r.is_end_inclusive() {
(r.from.as_integer()?, r.to.as_integer()?)
} else if r.to.as_integer()? > 0 {
(r.from.as_integer()?, r.to.as_integer()? - 1)
(r.from.as_int()?, r.to.as_int()?)
} else if r.to.as_int()? > 0 {
(r.from.as_int()?, r.to.as_int()? - 1)
} else {
(0, 0)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/strings/str_/contains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn action(
head: Span,
) -> Value {
match input {
Value::String { val, .. } => Value::boolean(
Value::String { val, .. } => Value::bool(
match case_insensitive {
true => {
if *not_contain {
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/strings/str_/ends_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
} else {
s.ends_with(&args.substring)
};
Value::boolean(ends_with, head)
Value::bool(ends_with, head)
}
Value::Error { .. } => input.clone(),
_ => Value::Error {
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/strings/str_/starts_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn action(
} else {
s.starts_with(substring)
};
Value::boolean(starts_with, head)
Value::bool(starts_with, head)
}
Value::Error { .. } => input.clone(),
_ => Value::Error {
Expand Down

0 comments on commit 7e1b922

Please sign in to comment.