mirror of
https://github.com/nushell/nushell.git
synced 2025-07-01 07:00:37 +02:00
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:
@ -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(),
|
||||
|
Reference in New Issue
Block a user