Ian Manske 6fd854ed9f
Replace ExternalStream with new ByteStream type (#12774)
# Description
This PR introduces a `ByteStream` type which is a `Read`-able stream of
bytes. Internally, it has an enum over three different byte stream
sources:
```rust
pub enum ByteStreamSource {
    Read(Box<dyn Read + Send + 'static>),
    File(File),
    Child(ChildProcess),
}
```

This is in comparison to the current `RawStream` type, which is an
`Iterator<Item = Vec<u8>>` and has to allocate for each read chunk.

Currently, `PipelineData::ExternalStream` serves a weird dual role where
it is either external command output or a wrapper around `RawStream`.
`ByteStream` makes this distinction more clear (via `ByteStreamSource`)
and replaces `PipelineData::ExternalStream` in this PR:
```rust
pub enum PipelineData {
    Empty,
    Value(Value, Option<PipelineMetadata>),
    ListStream(ListStream, Option<PipelineMetadata>),
    ByteStream(ByteStream, Option<PipelineMetadata>),
}
```

The PR is relatively large, but a decent amount of it is just repetitive
changes.

This PR fixes #7017, fixes #10763, and fixes #12369.

This PR also improves performance when piping external commands. Nushell
should, in most cases, have competitive pipeline throughput compared to,
e.g., bash.
| Command | Before (MB/s) | After (MB/s) | Bash (MB/s) |
| -------------------------------------------------- | -------------:|
------------:| -----------:|
| `throughput \| rg 'x'` | 3059 | 3744 | 3739 |
| `throughput \| nu --testbin relay o> /dev/null` | 3508 | 8087 | 8136 |

# User-Facing Changes
- This is a breaking change for the plugin communication protocol,
because the `ExternalStreamInfo` was replaced with `ByteStreamInfo`.
Plugins now only have to deal with a single input stream, as opposed to
the previous three streams: stdout, stderr, and exit code.
- The output of `describe` has been changed for external/byte streams.
- Temporary breaking change: `bytes starts-with` no longer works with
byte streams. This is to keep the PR smaller, and `bytes ends-with`
already does not work on byte streams.
- If a process core dumped, then instead of having a `Value::Error` in
the `exit_code` column of the output returned from `complete`, it now is
a `Value::Int` with the negation of the signal number.

# After Submitting
- Update docs and book as necessary
- Release notes (e.g., plugin protocol changes)
- Adapt/convert commands to work with byte streams (high priority is
`str length`, `bytes starts-with`, and maybe `bytes ends-with`).
- Refactor the `tee` code, Devyn has already done some work on this.

---------

Co-authored-by: Devyn Cairns <devyn.cairns@gmail.com>
2024-05-16 07:11:18 -07:00

166 lines
5.5 KiB
Rust

use nu_engine::command_prelude::*;
use nu_protocol::ast::PathMember;
#[derive(Clone)]
pub struct ToJson;
impl Command for ToJson {
fn name(&self) -> &str {
"to json"
}
fn signature(&self) -> Signature {
Signature::build("to json")
.input_output_types(vec![(Type::Any, Type::String)])
.switch("raw", "remove all of the whitespace", Some('r'))
.named(
"indent",
SyntaxShape::Number,
"specify indentation width",
Some('i'),
)
.named(
"tabs",
SyntaxShape::Number,
"specify indentation tab quantity",
Some('t'),
)
.category(Category::Formats)
}
fn usage(&self) -> &str {
"Converts table data into JSON text."
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let raw = call.has_flag(engine_state, stack, "raw")?;
let use_tabs = call.get_flag(engine_state, stack, "tabs")?;
let indent = call.get_flag(engine_state, stack, "indent")?;
let span = call.head;
// allow ranges to expand and turn into array
let input = input.try_expand_range()?;
let value = input.into_value(span)?;
let json_value = value_to_json_value(&value)?;
let json_result = if raw {
nu_json::to_string_raw(&json_value)
} else if let Some(tab_count) = use_tabs {
nu_json::to_string_with_tab_indentation(&json_value, tab_count)
} else if let Some(indent) = indent {
nu_json::to_string_with_indent(&json_value, indent)
} else {
nu_json::to_string(&json_value)
};
match json_result {
Ok(serde_json_string) => {
Ok(Value::string(serde_json_string, span).into_pipeline_data())
}
_ => Ok(Value::error(
ShellError::CantConvert {
to_type: "JSON".into(),
from_type: value.get_type().to_string(),
span,
help: None,
},
span,
)
.into_pipeline_data()),
}
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description:
"Outputs a JSON string, with default indentation, representing the contents of this table",
example: "[a b c] | to json",
result: Some(Value::test_string("[\n \"a\",\n \"b\",\n \"c\"\n]")),
},
Example {
description:
"Outputs a JSON string, with 4-space indentation, representing the contents of this table",
example: "[Joe Bob Sam] | to json --indent 4",
result: Some(Value::test_string("[\n \"Joe\",\n \"Bob\",\n \"Sam\"\n]")),
},
Example {
description:
"Outputs an unformatted JSON string representing the contents of this table",
example: "[1 2 3] | to json -r",
result: Some(Value::test_string("[1,2,3]")),
},
]
}
}
pub fn value_to_json_value(v: &Value) -> Result<nu_json::Value, ShellError> {
let span = v.span();
Ok(match v {
Value::Bool { val, .. } => nu_json::Value::Bool(*val),
Value::Filesize { val, .. } => nu_json::Value::I64(*val),
Value::Duration { val, .. } => nu_json::Value::I64(*val),
Value::Date { val, .. } => nu_json::Value::String(val.to_string()),
Value::Float { val, .. } => nu_json::Value::F64(*val),
Value::Int { val, .. } => nu_json::Value::I64(*val),
Value::Nothing { .. } => nu_json::Value::Null,
Value::String { val, .. } => nu_json::Value::String(val.to_string()),
Value::Glob { val, .. } => nu_json::Value::String(val.to_string()),
Value::CellPath { val, .. } => nu_json::Value::Array(
val.members
.iter()
.map(|x| match &x {
PathMember::String { val, .. } => Ok(nu_json::Value::String(val.clone())),
PathMember::Int { val, .. } => Ok(nu_json::Value::U64(*val as u64)),
})
.collect::<Result<Vec<nu_json::Value>, ShellError>>()?,
),
Value::List { vals, .. } => nu_json::Value::Array(json_list(vals)?),
Value::Error { error, .. } => return Err(*error.clone()),
Value::Closure { .. } | Value::Range { .. } => nu_json::Value::Null,
Value::Binary { val, .. } => {
nu_json::Value::Array(val.iter().map(|x| nu_json::Value::U64(*x as u64)).collect())
}
Value::Record { val, .. } => {
let mut m = nu_json::Map::new();
for (k, v) in &**val {
m.insert(k.clone(), value_to_json_value(v)?);
}
nu_json::Value::Object(m)
}
Value::Custom { val, .. } => {
let collected = val.to_base_value(span)?;
value_to_json_value(&collected)?
}
})
}
fn json_list(input: &[Value]) -> Result<Vec<nu_json::Value>, ShellError> {
let mut out = vec![];
for value in input {
out.push(value_to_json_value(value)?);
}
Ok(out)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(ToJson {})
}
}