From 2c75aabbfcdbb7075ed3d1763db65588bc3abedb Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Mon, 17 Jan 2022 17:41:59 -0600 Subject: [PATCH] allow `size` and other to count bytes from binary with `as_string()` (#769) --- crates/nu-protocol/src/value/mod.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/nu-protocol/src/value/mod.rs b/crates/nu-protocol/src/value/mod.rs index d5934fc485..c449c39b9e 100644 --- a/crates/nu-protocol/src/value/mod.rs +++ b/crates/nu-protocol/src/value/mod.rs @@ -172,6 +172,16 @@ impl Value { pub fn as_string(&self) -> Result { match self { Value::String { val, .. } => Ok(val.to_string()), + Value::Binary { val, .. } => Ok(match std::str::from_utf8(val) { + Ok(s) => s.to_string(), + Err(_) => { + return Err(ShellError::CantConvert( + "binary".into(), + "string".into(), + self.span()?, + )) + } + }), x => Err(ShellError::CantConvert( "string".into(), x.get_type().to_string(),