Value helpers (#3000)

* Update README.md

add contributors graphic

* just a couple of helpers

* separated some helpers out to individual fns
This commit is contained in:
Darren Schroeder
2021-02-02 20:06:11 -06:00
committed by GitHub
parent fa928bd25d
commit ecaea57263
2 changed files with 72 additions and 31 deletions

View File

@ -320,6 +320,48 @@ impl Value {
}
}
/// View the Value as a FilePath (PathBuf), if possible
pub fn as_filepath(&self) -> Result<PathBuf, ShellError> {
match &self.value {
UntaggedValue::Primitive(Primitive::FilePath(path)) => Ok(path.clone()),
_ => Err(ShellError::type_error("string", self.spanned_type_name())),
}
}
/// View the Value as a Int (BigInt), if possible
pub fn as_int(&self) -> Result<BigInt, ShellError> {
match &self.value {
UntaggedValue::Primitive(Primitive::Int(n)) => Ok(n.clone()),
_ => Err(ShellError::type_error("bigint", self.spanned_type_name())),
}
}
/// View the Value as a Filesize (BigInt), if possible
pub fn as_filesize(&self) -> Result<BigInt, ShellError> {
match &self.value {
UntaggedValue::Primitive(Primitive::Filesize(fs)) => Ok(fs.clone()),
_ => Err(ShellError::type_error("bigint", self.spanned_type_name())),
}
}
/// View the Value as a Duration (BigInt), if possible
pub fn as_duration(&self) -> Result<BigInt, ShellError> {
match &self.value {
UntaggedValue::Primitive(Primitive::Duration(dur)) => Ok(dur.clone()),
_ => Err(ShellError::type_error("bigint", self.spanned_type_name())),
}
}
/// View the Value as a Decimal (BigDecimal), if possible
pub fn as_decimal(&self) -> Result<BigDecimal, ShellError> {
match &self.value {
UntaggedValue::Primitive(Primitive::Decimal(d)) => Ok(d.clone()),
_ => Err(ShellError::type_error(
"bigdecimal",
self.spanned_type_name(),
)),
}
}
pub fn convert_to_string(&self) -> String {
match &self.value {
UntaggedValue::Primitive(Primitive::String(s)) => s.clone(),