mirror of
https://github.com/nushell/nushell.git
synced 2024-11-29 11:54:02 +01:00
Support binary save
This commit is contained in:
parent
c6c4d4ddb1
commit
ab48d3a3f2
@ -496,6 +496,10 @@ pub trait WholeStreamCommand: Send + Sync {
|
|||||||
args: CommandArgs,
|
args: CommandArgs,
|
||||||
registry: ®istry::CommandRegistry,
|
registry: ®istry::CommandRegistry,
|
||||||
) -> Result<OutputStream, ShellError>;
|
) -> Result<OutputStream, ShellError>;
|
||||||
|
|
||||||
|
fn is_binary(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait PerItemCommand: Send + Sync {
|
pub trait PerItemCommand: Send + Sync {
|
||||||
@ -521,6 +525,10 @@ pub trait PerItemCommand: Send + Sync {
|
|||||||
raw_args: &RawCommandArgs,
|
raw_args: &RawCommandArgs,
|
||||||
input: Tagged<Value>,
|
input: Tagged<Value>,
|
||||||
) -> Result<OutputStream, ShellError>;
|
) -> Result<OutputStream, ShellError>;
|
||||||
|
|
||||||
|
fn is_binary(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
@ -608,6 +616,13 @@ impl Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_binary(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Command::WholeStream(command) => command.is_binary(),
|
||||||
|
Command::PerItem(command) => command.is_binary(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FnFilterCommand {
|
pub struct FnFilterCommand {
|
||||||
|
@ -96,7 +96,7 @@ fn save(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let content = if !save_raw {
|
let content : Result<Vec<u8>, ShellError> = if !save_raw {
|
||||||
if let Some(extension) = full_path.extension() {
|
if let Some(extension) = full_path.extension() {
|
||||||
let command_name = format!("to-{}", extension.to_str().unwrap());
|
let command_name = format!("to-{}", extension.to_str().unwrap());
|
||||||
if let Some(converter) = registry.get_command(&command_name) {
|
if let Some(converter) = registry.get_command(&command_name) {
|
||||||
@ -116,22 +116,43 @@ fn save(
|
|||||||
};
|
};
|
||||||
let mut result = converter.run(new_args.with_input(input), ®istry);
|
let mut result = converter.run(new_args.with_input(input), ®istry);
|
||||||
let result_vec: Vec<Result<ReturnSuccess, ShellError>> = result.drain_vec().await;
|
let result_vec: Vec<Result<ReturnSuccess, ShellError>> = result.drain_vec().await;
|
||||||
let mut result_string = String::new();
|
if converter.is_binary() {
|
||||||
for res in result_vec {
|
let mut result_binary : Vec<u8> = Vec::new();
|
||||||
match res {
|
for res in result_vec {
|
||||||
Ok(ReturnSuccess::Value(Tagged { item: Value::Primitive(Primitive::String(s)), .. })) => {
|
match res {
|
||||||
result_string.push_str(&s);
|
Ok(ReturnSuccess::Value(Tagged { item: Value::Binary(b), .. })) => {
|
||||||
|
for u in b.into_iter() {
|
||||||
|
result_binary.push(u);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
yield Err(ShellError::labeled_error(
|
||||||
|
"Save could not successfully save",
|
||||||
|
"unexpected data during binary save",
|
||||||
|
name_span,
|
||||||
|
));
|
||||||
|
},
|
||||||
}
|
}
|
||||||
_ => {
|
|
||||||
yield Err(ShellError::labeled_error(
|
|
||||||
"Save could not successfully save",
|
|
||||||
"unexpected data during save",
|
|
||||||
name_span,
|
|
||||||
));
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
Ok(result_binary)
|
||||||
|
} else {
|
||||||
|
let mut result_string = String::new();
|
||||||
|
for res in result_vec {
|
||||||
|
match res {
|
||||||
|
Ok(ReturnSuccess::Value(Tagged { item: Value::Primitive(Primitive::String(s)), .. })) => {
|
||||||
|
result_string.push_str(&s);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
yield Err(ShellError::labeled_error(
|
||||||
|
"Save could not successfully save",
|
||||||
|
"unexpected data during text save",
|
||||||
|
name_span,
|
||||||
|
));
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(result_string.into_bytes())
|
||||||
}
|
}
|
||||||
Ok(result_string)
|
|
||||||
} else {
|
} else {
|
||||||
let mut result_string = String::new();
|
let mut result_string = String::new();
|
||||||
for res in input {
|
for res in input {
|
||||||
@ -148,7 +169,7 @@ fn save(
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(result_string)
|
Ok(result_string.into_bytes())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let mut result_string = String::new();
|
let mut result_string = String::new();
|
||||||
@ -166,10 +187,10 @@ fn save(
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(result_string)
|
Ok(result_string.into_bytes())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
string_from(&input)
|
Ok(string_from(&input).into_bytes())
|
||||||
};
|
};
|
||||||
|
|
||||||
match content {
|
match content {
|
||||||
@ -185,7 +206,7 @@ fn save(
|
|||||||
Ok(OutputStream::new(stream))
|
Ok(OutputStream::new(stream))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn string_from(input: &Vec<Tagged<Value>>) -> Result<String, ShellError> {
|
fn string_from(input: &Vec<Tagged<Value>>) -> String {
|
||||||
let mut save_data = String::new();
|
let mut save_data = String::new();
|
||||||
|
|
||||||
if input.len() > 0 {
|
if input.len() > 0 {
|
||||||
@ -202,5 +223,5 @@ fn string_from(input: &Vec<Tagged<Value>>) -> Result<String, ShellError> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(save_data)
|
save_data
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,10 @@ impl WholeStreamCommand for ToBSON {
|
|||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
to_bson(args, registry)
|
to_bson(args, registry)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_binary(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn value_to_bson_value(v: &Tagged<Value>) -> Result<Bson, ShellError> {
|
pub fn value_to_bson_value(v: &Tagged<Value>) -> Result<Bson, ShellError> {
|
||||||
|
@ -27,6 +27,10 @@ impl WholeStreamCommand for ToSQLite {
|
|||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
to_sqlite(args, registry)
|
to_sqlite(args, registry)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_binary(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ToDB;
|
pub struct ToDB;
|
||||||
@ -51,6 +55,10 @@ impl WholeStreamCommand for ToDB {
|
|||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
to_sqlite(args, registry)
|
to_sqlite(args, registry)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_binary(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn comma_concat(acc: String, current: String) -> String {
|
fn comma_concat(acc: String, current: String) -> String {
|
||||||
|
Loading…
Reference in New Issue
Block a user