mirror of
https://github.com/nushell/nushell.git
synced 2025-01-03 13:00:08 +01:00
Update how extensions are set to default to path when no extension can be determined from mime
This commit is contained in:
parent
dd74657385
commit
93a1a0604e
@ -37,6 +37,7 @@ bytes = "0.4.12"
|
|||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
pretty_env_logger = "0.3.1"
|
pretty_env_logger = "0.3.1"
|
||||||
serde = { version = "1.0.98", features = ["derive"] }
|
serde = { version = "1.0.98", features = ["derive"] }
|
||||||
|
bson = "0.13.0"
|
||||||
serde_json = "1.0.40"
|
serde_json = "1.0.40"
|
||||||
serde-hjson = "0.9.1"
|
serde-hjson = "0.9.1"
|
||||||
serde_yaml = "0.8"
|
serde_yaml = "0.8"
|
||||||
|
@ -173,6 +173,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
|
|||||||
whole_stream_command(FromArray),
|
whole_stream_command(FromArray),
|
||||||
whole_stream_command(FromCSV),
|
whole_stream_command(FromCSV),
|
||||||
whole_stream_command(FromINI),
|
whole_stream_command(FromINI),
|
||||||
|
whole_stream_command(FromBSON),
|
||||||
whole_stream_command(FromJSON),
|
whole_stream_command(FromJSON),
|
||||||
whole_stream_command(FromTOML),
|
whole_stream_command(FromTOML),
|
||||||
whole_stream_command(FromXML),
|
whole_stream_command(FromXML),
|
||||||
|
@ -15,6 +15,7 @@ crate mod enter;
|
|||||||
crate mod exit;
|
crate mod exit;
|
||||||
crate mod first;
|
crate mod first;
|
||||||
crate mod from_array;
|
crate mod from_array;
|
||||||
|
crate mod from_bson;
|
||||||
crate mod from_csv;
|
crate mod from_csv;
|
||||||
crate mod from_ini;
|
crate mod from_ini;
|
||||||
crate mod from_json;
|
crate mod from_json;
|
||||||
@ -70,6 +71,7 @@ crate use enter::Enter;
|
|||||||
crate use exit::Exit;
|
crate use exit::Exit;
|
||||||
crate use first::First;
|
crate use first::First;
|
||||||
crate use from_array::FromArray;
|
crate use from_array::FromArray;
|
||||||
|
crate use from_bson::FromBSON;
|
||||||
crate use from_csv::FromCSV;
|
crate use from_csv::FromCSV;
|
||||||
crate use from_ini::FromINI;
|
crate use from_ini::FromINI;
|
||||||
crate use from_json::FromJSON;
|
crate use from_json::FromJSON;
|
||||||
|
@ -176,7 +176,7 @@ impl InternalCommand {
|
|||||||
|
|
||||||
match contents {
|
match contents {
|
||||||
Value::Primitive(Primitive::String(string)) => {
|
Value::Primitive(Primitive::String(string)) => {
|
||||||
let value = crate::commands::open::parse_as_value(
|
let value = crate::commands::open::parse_string_as_value(
|
||||||
file_extension,
|
file_extension,
|
||||||
string,
|
string,
|
||||||
contents_tag,
|
contents_tag,
|
||||||
|
@ -51,16 +51,18 @@ fn run(call_info: &CallInfo, shell_manager: &ShellManager) -> Result<OutputStrea
|
|||||||
|
|
||||||
let stream = async_stream_block! {
|
let stream = async_stream_block! {
|
||||||
|
|
||||||
//FIXME: unwraps
|
//FIXME: unwraps
|
||||||
|
let (file_extension, contents, contents_tag, span_source) =
|
||||||
|
fetch(&full_path, &path_str, path_span).await.unwrap();
|
||||||
|
|
||||||
let (file_extension, contents, contents_tag, span_source) =
|
let file_extension = if call_info.args.has("raw") {
|
||||||
fetch(&full_path, &path_str, path_span).await.unwrap();
|
None
|
||||||
|
} else {
|
||||||
|
// If the extension could not be determined via mimetype, try to use the path
|
||||||
|
// extension. Some file types do not declare their mimetypes (such as bson files).
|
||||||
|
file_extension.or(path_str.split('.').last().map(String::from))
|
||||||
|
};
|
||||||
|
|
||||||
let file_extension = if has_raw {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
file_extension
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(uuid) = contents_tag.origin {
|
if let Some(uuid) = contents_tag.origin {
|
||||||
// If we have loaded something, track its source
|
// If we have loaded something, track its source
|
||||||
@ -70,9 +72,24 @@ fn run(call_info: &CallInfo, shell_manager: &ShellManager) -> Result<OutputStrea
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
match contents {
|
match contents {
|
||||||
Value::Primitive(Primitive::String(string)) => {
|
Value::Primitive(Primitive::String(string)) => {
|
||||||
let value = parse_as_value(file_extension, string, contents_tag, name_span).unwrap();
|
let value = parse_string_as_value(file_extension, string, contents_tag, call_info.name_span)?;
|
||||||
|
|
||||||
|
match value {
|
||||||
|
Tagged {
|
||||||
|
item: Value::List(list),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
for elem in list {
|
||||||
|
stream.push_back(ReturnSuccess::value(elem));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
x => stream.push_back(ReturnSuccess::value(x)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Value::Binary(binary) => {
|
||||||
|
let value = parse_binary_as_value(file_extension, binary, contents_tag, call_info.name_span)?;
|
||||||
|
|
||||||
match value {
|
match value {
|
||||||
Tagged {
|
Tagged {
|
||||||
@ -402,7 +419,7 @@ fn read_be_u16(input: &[u8]) -> Option<Vec<u16>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_as_value(
|
pub fn parse_string_as_value(
|
||||||
extension: Option<String>,
|
extension: Option<String>,
|
||||||
contents: String,
|
contents: String,
|
||||||
contents_tag: Tag,
|
contents_tag: Tag,
|
||||||
@ -477,3 +494,27 @@ pub fn parse_as_value(
|
|||||||
_ => Ok(Value::string(contents).tagged(contents_tag)),
|
_ => Ok(Value::string(contents).tagged(contents_tag)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parse_binary_as_value(
|
||||||
|
extension: Option<String>,
|
||||||
|
contents: Vec<u8>,
|
||||||
|
contents_tag: Tag,
|
||||||
|
name_span: Span,
|
||||||
|
) -> Result<Tagged<Value>, ShellError> {
|
||||||
|
println!("{:?}", extension);
|
||||||
|
match extension {
|
||||||
|
Some(x) if x == "bson" => {
|
||||||
|
Err(ShellError::labeled_error("Could not open as BSON", "Could not open as BSON", name_span))
|
||||||
|
//crate::commands::from_json::from_bson_bytes_to_value(contents, contents_tag).map_err(
|
||||||
|
// move |_| {
|
||||||
|
// ShellError::labeled_error(
|
||||||
|
// "Could not open as BSON",
|
||||||
|
// "could not open as BSON",
|
||||||
|
// name_span,
|
||||||
|
// )
|
||||||
|
// },
|
||||||
|
// )
|
||||||
|
}
|
||||||
|
_ => Ok(Value::Binary(contents).tagged(contents_tag)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user