Invert &Options to Option<&T> (#10315)

Elide the reference for `Copy` type (`usize`)
Use the canonical deref where possible.
* `&Box` -> `&`
* `&String` -> `&str`
* `&PathBuf` -> `&Path`

Skips the ctrl-C handler for now.
This commit is contained in:
Stefan Holderbach
2023-09-13 01:00:58 +02:00
committed by GitHub
parent 3e14dc3eb8
commit a14e9e0a2e
24 changed files with 87 additions and 83 deletions

View File

@ -54,23 +54,33 @@ impl Command for Metadata {
} => {
let origin = stack.get_var_with_origin(*var_id, *span)?;
Ok(build_metadata_record(&origin, &input.metadata(), head)
.into_pipeline_data())
Ok(
build_metadata_record(&origin, input.metadata().as_deref(), head)
.into_pipeline_data(),
)
}
_ => {
let val: Value = call.req(engine_state, stack, 0)?;
Ok(build_metadata_record(&val, &input.metadata(), head)
.into_pipeline_data())
Ok(
build_metadata_record(&val, input.metadata().as_deref(), head)
.into_pipeline_data(),
)
}
}
} else {
let val: Value = call.req(engine_state, stack, 0)?;
Ok(build_metadata_record(&val, &input.metadata(), head).into_pipeline_data())
Ok(
build_metadata_record(&val, input.metadata().as_deref(), head)
.into_pipeline_data(),
)
}
}
Some(_) => {
let val: Value = call.req(engine_state, stack, 0)?;
Ok(build_metadata_record(&val, &input.metadata(), head).into_pipeline_data())
Ok(
build_metadata_record(&val, input.metadata().as_deref(), head)
.into_pipeline_data(),
)
}
None => {
let mut record = Record::new();
@ -109,11 +119,7 @@ impl Command for Metadata {
}
}
fn build_metadata_record(
arg: &Value,
metadata: &Option<Box<PipelineMetadata>>,
head: Span,
) -> Value {
fn build_metadata_record(arg: &Value, metadata: Option<&PipelineMetadata>, head: Span) -> Value {
let mut record = Record::new();
let span = arg.span();
@ -128,7 +134,7 @@ fn build_metadata_record(
),
);
if let Some(x) = metadata.as_deref() {
if let Some(x) = metadata {
match x {
PipelineMetadata {
data_source: DataSource::Ls,

View File

@ -87,7 +87,7 @@ impl Command for Save {
match input {
PipelineData::ExternalStream { stdout: None, .. } => {
// Open files to possibly truncate them
let _ = get_files(&path, &stderr_path, append, force)?;
let _ = get_files(&path, stderr_path.as_ref(), append, force)?;
Ok(PipelineData::empty())
}
PipelineData::ExternalStream {
@ -95,7 +95,7 @@ impl Command for Save {
stderr,
..
} => {
let (file, stderr_file) = get_files(&path, &stderr_path, append, force)?;
let (file, stderr_file) = get_files(&path, stderr_path.as_ref(), append, force)?;
// delegate a thread to redirect stderr to result.
let handler = stderr.map(|stderr_stream| match stderr_file {
@ -127,7 +127,7 @@ impl Command for Save {
PipelineData::ListStream(ls, _)
if raw || prepare_path(&path, append, force)?.0.extension().is_none() =>
{
let (mut file, _) = get_files(&path, &stderr_path, append, force)?;
let (mut file, _) = get_files(&path, stderr_path.as_ref(), append, force)?;
for val in ls {
file.write_all(&value_to_bytes(val)?)
.map_err(|err| ShellError::IOError(err.to_string()))?;
@ -143,7 +143,7 @@ impl Command for Save {
input_to_bytes(input, Path::new(&path.item), raw, engine_state, stack, span)?;
// Only open file after successful conversion
let (mut file, _) = get_files(&path, &stderr_path, append, force)?;
let (mut file, _) = get_files(&path, stderr_path.as_ref(), append, force)?;
file.write_all(&bytes)
.map_err(|err| ShellError::IOError(err.to_string()))?;
@ -317,7 +317,7 @@ fn open_file(path: &Path, span: Span, append: bool) -> Result<File, ShellError>
/// Get output file and optional stderr file
fn get_files(
path: &Spanned<PathBuf>,
stderr_path: &Option<Spanned<PathBuf>>,
stderr_path: Option<&Spanned<PathBuf>>,
append: bool,
force: bool,
) -> Result<(File, Option<File>), ShellError> {

View File

@ -130,7 +130,7 @@ pub fn split_by(
item: v.as_string()?,
span: name,
});
Ok(split(&splitter, input, name)?)
Ok(split(splitter.as_ref(), input, name)?)
}
// This uses the same format as the 'requires a column name' error in sort_utils.rs
None => Err(ShellError::GenericError(
@ -144,7 +144,7 @@ pub fn split_by(
}
pub fn split(
column_name: &Option<Spanned<String>>,
column_name: Option<&Spanned<String>>,
values: PipelineData,
span: Span,
) -> Result<PipelineData, ShellError> {
@ -156,24 +156,21 @@ pub fn split(
match grouper {
Grouper::ByColumn(Some(column_name)) => {
let block =
Box::new(
move |_, row: &Value| match row.get_data_by_key(&column_name.item) {
Some(group_key) => Ok(group_key.as_string()?),
None => Err(ShellError::CantFindColumn {
col_name: column_name.item.to_string(),
span: column_name.span,
src_span: row.span(),
}),
},
);
let block = move |_, row: &Value| match row.get_data_by_key(&column_name.item) {
Some(group_key) => Ok(group_key.as_string()?),
None => Err(ShellError::CantFindColumn {
col_name: column_name.item.to_string(),
span: column_name.span,
src_span: row.span(),
}),
};
data_split(values, &Some(block), span)
data_split(values, Some(&block), span)
}
Grouper::ByColumn(None) => {
let block = Box::new(move |_, row: &Value| row.as_string());
let block = move |_, row: &Value| row.as_string();
data_split(values, &Some(block), span)
data_split(values, Some(&block), span)
}
}
}
@ -181,7 +178,7 @@ pub fn split(
#[allow(clippy::type_complexity)]
fn data_group(
values: &Value,
grouper: &Option<Box<dyn Fn(usize, &Value) -> Result<String, ShellError> + Send>>,
grouper: Option<&dyn Fn(usize, &Value) -> Result<String, ShellError>>,
span: Span,
) -> Result<Value, ShellError> {
let mut groups: IndexMap<String, Vec<Value>> = IndexMap::new();
@ -209,7 +206,7 @@ fn data_group(
#[allow(clippy::type_complexity)]
pub fn data_split(
value: PipelineData,
splitter: &Option<Box<dyn Fn(usize, &Value) -> Result<String, ShellError> + Send>>,
splitter: Option<&dyn Fn(usize, &Value) -> Result<String, ShellError>>,
span: Span,
) -> Result<PipelineData, ShellError> {
let mut splits = indexmap::IndexMap::new();

View File

@ -234,7 +234,7 @@ fn sort_attributes(val: Value) -> Value {
fn generate_key(item: &ValueCounter) -> Result<String, ShellError> {
let value = sort_attributes(item.val_to_compare.clone()); //otherwise, keys could be different for Records
value_to_string(&value, Span::unknown(), 0, &None)
value_to_string(&value, Span::unknown(), 0, None)
}
fn generate_results_with_count(head: Span, uniq_values: Vec<ValueCounter>) -> Vec<Value> {

View File

@ -61,15 +61,15 @@ impl Command for ToNuon {
let value = input.into_value(span);
let nuon_result = if raw {
value_to_string(&value, span, 0, &None)
value_to_string(&value, span, 0, None)
} else if use_tabs {
let tab_count: usize = call.get_flag(engine_state, stack, "tabs")?.unwrap_or(1);
value_to_string(&value, span, 0, &Some("\t".repeat(tab_count)))
value_to_string(&value, span, 0, Some(&"\t".repeat(tab_count)))
} else if use_indent {
let indent: usize = call.get_flag(engine_state, stack, "indent")?.unwrap_or(2);
value_to_string(&value, span, 0, &Some(" ".repeat(indent)))
value_to_string(&value, span, 0, Some(&" ".repeat(indent)))
} else {
value_to_string(&value, span, 0, &None)
value_to_string(&value, span, 0, None)
};
match nuon_result {
@ -119,7 +119,7 @@ pub fn value_to_string(
v: &Value,
span: Span,
depth: usize,
indent: &Option<String>,
indent: Option<&str>,
) -> Result<String, ShellError> {
let (nl, sep) = get_true_separators(indent);
let idt = get_true_indentation(depth, indent);
@ -290,14 +290,14 @@ pub fn value_to_string(
}
}
fn get_true_indentation(depth: usize, indent: &Option<String>) -> String {
fn get_true_indentation(depth: usize, indent: Option<&str>) -> String {
match indent {
Some(i) => i.repeat(depth),
None => "".to_string(),
}
}
fn get_true_separators(indent: &Option<String>) -> (String, String) {
fn get_true_separators(indent: Option<&str>) -> (String, String) {
match indent {
Some(_) => ("\n".to_string(), "".to_string()),
None => ("".to_string(), " ".to_string()),
@ -308,7 +308,7 @@ fn value_to_string_without_quotes(
v: &Value,
span: Span,
depth: usize,
indent: &Option<String>,
indent: Option<&str>,
) -> Result<String, ShellError> {
match v {
Value::String { val, .. } => Ok({