Remove stringification for binary values in save command (#4428)

* Remove stringification for binary values in `save`

* Fix typo and clippy warning
This commit is contained in:
Genna Wingert 2022-02-11 20:26:36 +01:00 committed by GitHub
parent 968ef1e953
commit 85d1a681c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -36,8 +36,6 @@ impl Command for Save {
let span = call.head;
let config = stack.get_config()?;
let path = call.req::<Spanned<String>>(engine_state, stack, 0)?;
let arg_span = path.span;
let path = Path::new(&path.item);
@ -66,7 +64,7 @@ impl Command for Save {
};
if let Some(ext) = ext {
match engine_state.find_decl(format!("to {}", ext).as_bytes()) {
let output = match engine_state.find_decl(format!("to {}", ext).as_bytes()) {
Some(converter_id) => {
let output = engine_state.get_decl(converter_id).run(
engine_state,
@ -75,44 +73,46 @@ impl Command for Save {
input,
)?;
let output = output.into_value(span);
match output {
Value::String { val, .. } => {
if let Err(err) = file.write_all(val.as_bytes()) {
return Err(ShellError::IOError(err.to_string()));
}
Ok(PipelineData::new(span))
}
Value::Binary { val, .. } => {
if let Err(err) = file.write_all(&val) {
return Err(ShellError::IOError(err.to_string()));
}
Ok(PipelineData::new(span))
}
v => Err(ShellError::UnsupportedInput(v.get_type().to_string(), span)),
}
output.into_value(span)
}
None => {
let output = input.collect_string("", &config)?;
None => input.into_value(span),
};
if let Err(err) = file.write_all(output.as_bytes()) {
match output {
Value::String { val, .. } => {
if let Err(err) = file.write_all(val.as_bytes()) {
return Err(ShellError::IOError(err.to_string()));
}
Ok(PipelineData::new(span))
}
Value::Binary { val, .. } => {
if let Err(err) = file.write_all(&val) {
return Err(ShellError::IOError(err.to_string()));
}
Ok(PipelineData::new(span))
}
v => Err(ShellError::UnsupportedInput(v.get_type().to_string(), span)),
}
} else {
let output = input.collect_string("", &config)?;
match input.into_value(span) {
Value::String { val, .. } => {
if let Err(err) = file.write_all(val.as_bytes()) {
return Err(ShellError::IOError(err.to_string()));
}
if let Err(err) = file.write_all(output.as_bytes()) {
return Err(ShellError::IOError(err.to_string()));
Ok(PipelineData::new(span))
}
Value::Binary { val, .. } => {
if let Err(err) = file.write_all(&val) {
return Err(ShellError::IOError(err.to_string()));
}
Ok(PipelineData::new(span))
}
v => Err(ShellError::UnsupportedInput(v.get_type().to_string(), span)),
}
Ok(PipelineData::new(span))
}
}
}