mirror of
https://github.com/nushell/nushell.git
synced 2025-08-19 08:31:29 +02:00
Update how extensions are set to default to path when no extension can be determined from mime
This commit is contained in:
@@ -176,7 +176,7 @@ impl InternalCommand {
|
||||
|
||||
match contents {
|
||||
Value::Primitive(Primitive::String(string)) => {
|
||||
let value = crate::commands::open::parse_as_value(
|
||||
let value = crate::commands::open::parse_string_as_value(
|
||||
file_extension,
|
||||
string,
|
||||
contents_tag,
|
||||
|
@@ -51,16 +51,18 @@ fn run(call_info: &CallInfo, shell_manager: &ShellManager) -> Result<OutputStrea
|
||||
|
||||
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) =
|
||||
fetch(&full_path, &path_str, path_span).await.unwrap();
|
||||
let file_extension = if call_info.args.has("raw") {
|
||||
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 we have loaded something, track its source
|
||||
@@ -70,9 +72,24 @@ fn run(call_info: &CallInfo, shell_manager: &ShellManager) -> Result<OutputStrea
|
||||
));
|
||||
}
|
||||
|
||||
match contents {
|
||||
Value::Primitive(Primitive::String(string)) => {
|
||||
let value = parse_as_value(file_extension, string, contents_tag, name_span).unwrap();
|
||||
match contents {
|
||||
Value::Primitive(Primitive::String(string)) => {
|
||||
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 {
|
||||
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>,
|
||||
contents: String,
|
||||
contents_tag: Tag,
|
||||
@@ -477,3 +494,27 @@ pub fn parse_as_value(
|
||||
_ => 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)),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user