Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduced LOC by replacing several instances of Value::Int {}, Value::Float{}, Value::Bool {}, and Value::String {} with Value::int(), Value::float(), Value::boolean() and Value::string() #7412

Merged
merged 4 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Next Next commit
Change various Value::Bool to Value::boolean
  • Loading branch information
webbedspace committed Dec 9, 2022
commit 6de6ba15676c5c32f235167f7df3c812b0956265
20 changes: 4 additions & 16 deletions crates/nu-command/src/bytes/ends_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,17 @@ impl Command for BytesEndsWith {
Example {
description: "Checks if binary ends with `0x[AA]`",
example: "0x[1F FF AA AA] | bytes ends-with 0x[AA]",
result: Some(Value::Bool {
val: true,
span: Span::test_data(),
}),
result: Some(Value::boolean(true, Span::test_data())),
},
Example {
description: "Checks if binary ends with `0x[FF AA AA]`",
example: "0x[1F FF AA AA] | bytes ends-with 0x[FF AA AA]",
result: Some(Value::Bool {
val: true,
span: Span::test_data(),
}),
result: Some(Value::boolean(true, Span::test_data())),
},
Example {
description: "Checks if binary ends with `0x[11]`",
example: "0x[1F FF AA AA] | bytes ends-with 0x[11]",
result: Some(Value::Bool {
val: false,
span: Span::test_data(),
}),
result: Some(Value::boolean(false, Span::test_data())),
},
]
}
Expand All @@ -98,10 +89,7 @@ fn ends_with(val: &Value, args: &Arguments, span: Span) -> Value {
Value::Binary {
val,
span: val_span,
} => Value::Bool {
val: val.ends_with(&args.pattern),
span: *val_span,
},
} => Value::boolean(val.ends_with(&args.pattern), *val_span),
other => Value::Error {
error: ShellError::UnsupportedInput(
format!(
Expand Down
20 changes: 4 additions & 16 deletions crates/nu-command/src/bytes/starts_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,17 @@ impl Command for BytesStartsWith {
Example {
description: "Checks if binary starts with `0x[1F FF AA]`",
example: "0x[1F FF AA AA] | bytes starts-with 0x[1F FF AA]",
result: Some(Value::Bool {
val: true,
span: Span::test_data(),
}),
result: Some(Value::boolean(true, Span::test_data())),
},
Example {
description: "Checks if binary starts with `0x[1F]`",
example: "0x[1F FF AA AA] | bytes starts-with 0x[1F]",
result: Some(Value::Bool {
val: true,
span: Span::test_data(),
}),
result: Some(Value::boolean(true, Span::test_data())),
},
Example {
description: "Checks if binary starts with `0x[1F]`",
example: "0x[1F FF AA AA] | bytes starts-with 0x[11]",
result: Some(Value::Bool {
val: false,
span: Span::test_data(),
}),
result: Some(Value::boolean(false, Span::test_data())),
},
]
}
Expand All @@ -104,10 +95,7 @@ fn starts_with(val: &Value, args: &Arguments, span: Span) -> Value {
Value::Binary {
val,
span: val_span,
} => Value::Bool {
val: val.starts_with(&args.pattern),
span: *val_span,
},
} => Value::boolean(val.starts_with(&args.pattern), *val_span),
other => Value::Error {
error: ShellError::UnsupportedInput(
format!(
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/dataframe/series/all_false.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ fn command(
)
})?;

let value = Value::Bool {
val: !bool.any(),
span: call.head,
};
let value = Value::boolean(!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
5 changes: 1 addition & 4 deletions crates/nu-command/src/dataframe/series/all_true.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ fn command(
)
})?;

let value = Value::Bool {
val: bool.all(),
span: call.head,
};
let value = Value::boolean(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
6 changes: 1 addition & 5 deletions crates/nu-command/src/experimental/is_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ impl Command for IsAdmin {
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(Value::Bool {
val: is_root(),
span: call.head,
}
.into_pipeline_data())
Ok(Value::boolean(is_root(), call.head).into_pipeline_data())
}

fn examples(&self) -> Vec<Example> {
Expand Down
53 changes: 11 additions & 42 deletions crates/nu-command/src/filters/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,18 @@ impl Command for Empty {
Example {
description: "Check if a string is empty",
example: "'' | is-empty",
result: Some(Value::Bool {
val: true,
span: Span::test_data(),
}),
result: Some(Value::boolean(true, Span::test_data())),
},
Example {
description: "Check if a list is empty",
example: "[] | is-empty",
result: Some(Value::Bool {
val: true,
span: Span::test_data(),
}),
result: Some(Value::boolean(true, Span::test_data())),
},
Example {
// TODO: revisit empty cell path semantics for a record.
description: "Check if more than one column are empty",
example: "[[meal size]; [arepa small] [taco '']] | is-empty meal size",
result: Some(Value::Bool {
val: false,
span: Span::test_data(),
}),
result: Some(Value::boolean(false, Span::test_data())),
},
]
}
Expand All @@ -84,23 +75,13 @@ fn empty(
let val = val.clone();
match val.follow_cell_path(&column.members, false) {
Ok(Value::Nothing { .. }) => {}
Ok(_) => {
return Ok(Value::Bool {
val: false,
span: head,
}
.into_pipeline_data())
}
Ok(_) => return Ok(Value::boolean(false, head).into_pipeline_data()),
Err(err) => return Err(err),
}
}
}

Ok(Value::Bool {
val: true,
span: head,
}
.into_pipeline_data())
Ok(Value::boolean(true, head).into_pipeline_data())
} else {
match input {
PipelineData::Empty => Ok(PipelineData::Empty),
Expand All @@ -109,30 +90,18 @@ fn empty(
let bytes = s.into_bytes();

match bytes {
Ok(s) => Ok(Value::Bool {
val: s.item.is_empty(),
span: head,
}
.into_pipeline_data()),
Ok(s) => Ok(Value::boolean(s.item.is_empty(), head).into_pipeline_data()),
Err(err) => Err(err),
}
}
None => Ok(Value::Bool {
val: true,
span: head,
}
.into_pipeline_data()),
None => Ok(Value::boolean(true, head).into_pipeline_data()),
},
PipelineData::ListStream(s, ..) => Ok(Value::Bool {
val: s.count() == 0,
span: head,
PipelineData::ListStream(s, ..) => {
Ok(Value::boolean(s.count() == 0, head).into_pipeline_data())
}
.into_pipeline_data()),
PipelineData::Value(value, ..) => Ok(Value::Bool {
val: value.is_empty(),
span: head,
PipelineData::Value(value, ..) => {
Ok(Value::boolean(value.is_empty(), head).into_pipeline_data())
}
.into_pipeline_data()),
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/formats/from/ods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,7 @@ fn from_ods(
val: *i,
span: head,
},
DataType::Bool(b) => Value::Bool {
val: *b,
span: head,
},
DataType::Bool(b) => Value::boolean(*b, head),
_ => Value::nothing(head),
};

Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/formats/from/xlsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,7 @@ fn from_xlsx(
val: *i,
span: head,
},
DataType::Bool(b) => Value::Bool {
val: *b,
span: head,
},
DataType::Bool(b) => Value::boolean(*b, head),
_ => Value::nothing(head),
};

Expand Down
10 changes: 2 additions & 8 deletions crates/nu-command/src/path/exists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ If you need to distinguish dirs and files, please use `path type`."#
Example {
description: "Check if a file exists",
example: "'C:\\Users\\joe\\todo.txt' | path exists",
result: Some(Value::Bool {
val: false,
span: Span::test_data(),
}),
result: Some(Value::boolean(false, Span::test_data())),
},
Example {
description: "Check if a file exists in a column",
Expand All @@ -88,10 +85,7 @@ If you need to distinguish dirs and files, please use `path type`."#
Example {
description: "Check if a file exists",
example: "'/home/joe/todo.txt' | path exists",
result: Some(Value::Bool {
val: false,
span: Span::test_data(),
}),
result: Some(Value::boolean(false, Span::test_data())),
},
Example {
description: "Check if a file exists in a column",
Expand Down