Add binary literals (#4680)

This commit is contained in:
JT
2022-02-28 18:31:53 -05:00
committed by GitHub
parent e3100e6afd
commit a6a96b29cb
15 changed files with 199 additions and 10 deletions

View File

@ -201,6 +201,7 @@ fn convert_to_value(
"blocks not supported in nuon".into(),
expr.span,
)),
Expr::Binary(val) => Ok(Value::Binary { val, span }),
Expr::Bool(val) => Ok(Value::Bool { val, span }),
Expr::Call(..) => Err(ShellError::OutsideSpannedLabeledError(
original_text.to_string(),

View File

@ -1,3 +1,4 @@
use core::fmt::Write;
use nu_engine::get_columns;
use nu_protocol::ast::{Call, RangeInclusion};
use nu_protocol::engine::{Command, EngineState, Stack};
@ -46,10 +47,18 @@ impl Command for ToNuon {
fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> {
match v {
Value::Binary { .. } => Err(ShellError::UnsupportedInput(
"binary not supported".into(),
span,
)),
Value::Binary { val, .. } => {
let mut s = String::with_capacity(2 * val.len());
for byte in val {
if write!(s, "{:02X}", byte).is_err() {
return Err(ShellError::UnsupportedInput(
"binary could not translate to string".into(),
span,
));
}
}
Ok(format!("0x[{}]", s))
}
Value::Block { .. } => Err(ShellError::UnsupportedInput(
"block not supported".into(),
span,