Allow open to read its filename from input (#841)

* Allow `open` to read its filename from input

* Add examples
This commit is contained in:
JT
2022-01-24 16:04:28 -05:00
committed by GitHub
parent ec94ca46bb
commit 988a873466
2 changed files with 86 additions and 5 deletions

View File

@ -190,6 +190,33 @@ impl Value {
}
}
pub fn as_spanned_string(&self) -> Result<Spanned<String>, ShellError> {
match self {
Value::String { val, span } => Ok(Spanned {
item: val.to_string(),
span: *span,
}),
Value::Binary { val, span } => Ok(match std::str::from_utf8(val) {
Ok(s) => Spanned {
item: s.to_string(),
span: *span,
},
Err(_) => {
return Err(ShellError::CantConvert(
"binary".into(),
"string".into(),
self.span()?,
))
}
}),
x => Err(ShellError::CantConvert(
"string".into(),
x.get_type().to_string(),
self.span()?,
)),
}
}
pub fn as_path(&self) -> Result<PathBuf, ShellError> {
match self {
Value::String { val, .. } => Ok(PathBuf::from(val)),