mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
Attempt to guess the content type of a file when opening with --raw (#13521)
# Description Attempt to guess the content type of a file when opening with --raw and set it in the pipeline metadata. <img width="644" alt="Screenshot 2024-08-02 at 11 30 10" src="https://github.com/user-attachments/assets/071f0967-c4dd-405a-b8c8-f7aa073efa98"> # User-Facing Changes - Content of files can be directly piped into commands like `http post` with the content type set appropriately when using `--raw`.
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
use chrono::{DateTime, Datelike, FixedOffset, Timelike};
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_protocol::ast::PathMember;
|
||||
use nu_protocol::{ast::PathMember, PipelineMetadata};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ToToml;
|
||||
@ -100,9 +100,18 @@ fn toml_into_pipeline_data(
|
||||
toml_value: &toml::Value,
|
||||
value_type: Type,
|
||||
span: Span,
|
||||
metadata: Option<PipelineMetadata>,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let new_md = Some(
|
||||
metadata
|
||||
.unwrap_or_default()
|
||||
.with_content_type(Some("text/x-toml".into())),
|
||||
);
|
||||
|
||||
match toml::to_string_pretty(&toml_value) {
|
||||
Ok(serde_toml_string) => Ok(Value::string(serde_toml_string, span).into_pipeline_data()),
|
||||
Ok(serde_toml_string) => {
|
||||
Ok(Value::string(serde_toml_string, span).into_pipeline_data_with_metadata(new_md))
|
||||
}
|
||||
_ => Ok(Value::error(
|
||||
ShellError::CantConvert {
|
||||
to_type: "TOML".into(),
|
||||
@ -112,7 +121,7 @@ fn toml_into_pipeline_data(
|
||||
},
|
||||
span,
|
||||
)
|
||||
.into_pipeline_data()),
|
||||
.into_pipeline_data_with_metadata(new_md)),
|
||||
}
|
||||
}
|
||||
|
||||
@ -139,6 +148,7 @@ fn to_toml(
|
||||
input: PipelineData,
|
||||
span: Span,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let metadata = input.metadata();
|
||||
let value = input.into_value(span)?;
|
||||
|
||||
let toml_value = value_to_toml_value(engine_state, &value, span)?;
|
||||
@ -148,10 +158,11 @@ fn to_toml(
|
||||
vec.iter().next().expect("this should never trigger"),
|
||||
value.get_type(),
|
||||
span,
|
||||
metadata,
|
||||
),
|
||||
_ => toml_into_pipeline_data(&toml_value, value.get_type(), span),
|
||||
_ => toml_into_pipeline_data(&toml_value, value.get_type(), span, metadata),
|
||||
},
|
||||
_ => toml_into_pipeline_data(&toml_value, value.get_type(), span),
|
||||
_ => toml_into_pipeline_data(&toml_value, value.get_type(), span, metadata),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -98,6 +98,7 @@ fn to_yaml(input: PipelineData, head: Span) -> Result<PipelineData, ShellError>
|
||||
let metadata = input
|
||||
.metadata()
|
||||
.unwrap_or_default()
|
||||
// Per RFC-9512, application/yaml should be used
|
||||
.with_content_type(Some("application/yaml".into()));
|
||||
let value = input.into_value(head)?;
|
||||
|
||||
|
Reference in New Issue
Block a user