Update how extensions are set to default to path when no extension can be determined from mime

This commit is contained in:
Patrick Meredith 2019-08-24 14:19:22 -04:00
parent dd74657385
commit 93a1a0604e
5 changed files with 58 additions and 13 deletions

View File

@ -37,6 +37,7 @@ bytes = "0.4.12"
log = "0.4.8"
pretty_env_logger = "0.3.1"
serde = { version = "1.0.98", features = ["derive"] }
bson = "0.13.0"
serde_json = "1.0.40"
serde-hjson = "0.9.1"
serde_yaml = "0.8"

View File

@ -173,6 +173,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
whole_stream_command(FromArray),
whole_stream_command(FromCSV),
whole_stream_command(FromINI),
whole_stream_command(FromBSON),
whole_stream_command(FromJSON),
whole_stream_command(FromTOML),
whole_stream_command(FromXML),

View File

@ -15,6 +15,7 @@ crate mod enter;
crate mod exit;
crate mod first;
crate mod from_array;
crate mod from_bson;
crate mod from_csv;
crate mod from_ini;
crate mod from_json;
@ -70,6 +71,7 @@ crate use enter::Enter;
crate use exit::Exit;
crate use first::First;
crate use from_array::FromArray;
crate use from_bson::FromBSON;
crate use from_csv::FromCSV;
crate use from_ini::FromINI;
crate use from_json::FromJSON;

View File

@ -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,

View File

@ -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)),
}
}