allow size and other to count bytes from binary with as_string() (#769)

This commit is contained in:
Darren Schroeder 2022-01-17 17:41:59 -06:00 committed by GitHub
parent 01e691c5ba
commit 2c75aabbfc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -172,6 +172,16 @@ impl Value {
pub fn as_string(&self) -> Result<String, ShellError> { pub fn as_string(&self) -> Result<String, ShellError> {
match self { match self {
Value::String { val, .. } => Ok(val.to_string()), 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( x => Err(ShellError::CantConvert(
"string".into(), "string".into(),
x.get_type().to_string(), x.get_type().to_string(),