diff --git a/Cargo.toml b/Cargo.toml index 51dc6622b..d1b760b9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/cli.rs b/src/cli.rs index a04a6211b..03fb9ac24 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -173,6 +173,7 @@ pub async fn cli() -> Result<(), Box> { 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), diff --git a/src/commands.rs b/src/commands.rs index 3728474f2..a57219a22 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -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; diff --git a/src/commands/classified.rs b/src/commands/classified.rs index 4582d5bee..28005ceb7 100644 --- a/src/commands/classified.rs +++ b/src/commands/classified.rs @@ -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, diff --git a/src/commands/open.rs b/src/commands/open.rs index f3df99865..6fc3f0183 100644 --- a/src/commands/open.rs +++ b/src/commands/open.rs @@ -51,16 +51,18 @@ fn run(call_info: &CallInfo, shell_manager: &ShellManager) -> Result Result { - 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> { } } -pub fn parse_as_value( +pub fn parse_string_as_value( extension: Option, 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, + contents: Vec, + contents_tag: Tag, + name_span: Span, +) -> Result, 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)), + } +}