forked from extern/nushell
move out call info deserializing from str
(#3294)
This commit is contained in:
committed by
GitHub
parent
b19a7aa8a6
commit
a131eddf54
@ -80,6 +80,16 @@ impl EvaluatedArgs {
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the corresponding Value for the named argument given, error if not possible
|
||||
pub fn expect_get(&self, name: &str) -> Result<&Value, ShellError> {
|
||||
match &self.named {
|
||||
None => Err(ShellError::unimplemented("Better error: expect_get")),
|
||||
Some(named) => named
|
||||
.get(name)
|
||||
.ok_or_else(|| ShellError::unimplemented("Better error: expect_get")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterates over the positional arguments
|
||||
pub fn positional_iter(&self) -> PositionalIter<'_> {
|
||||
match &self.positional {
|
||||
|
@ -429,6 +429,14 @@ impl Value {
|
||||
matches!(&self.value, UntaggedValue::Primitive(_))
|
||||
}
|
||||
|
||||
/// View the Value as char, if possible
|
||||
pub fn as_char(&self) -> Result<char, ShellError> {
|
||||
match &self.value {
|
||||
UntaggedValue::Primitive(primitive) => primitive.as_char(self.tag.span),
|
||||
_ => Err(ShellError::type_error("char", self.spanned_type_name())),
|
||||
}
|
||||
}
|
||||
|
||||
/// View the Value as unsigned size, if possible
|
||||
pub fn as_usize(&self) -> Result<usize, ShellError> {
|
||||
match &self.value {
|
||||
@ -453,7 +461,15 @@ impl Value {
|
||||
}
|
||||
}
|
||||
|
||||
/// View the Value as signed 64-bit, if possible
|
||||
/// View the Value as unsigned 32-bit, if possible
|
||||
pub fn as_u32(&self) -> Result<u32, ShellError> {
|
||||
match &self.value {
|
||||
UntaggedValue::Primitive(primitive) => primitive.as_u32(self.tag.span),
|
||||
_ => Err(ShellError::type_error("integer", self.spanned_type_name())),
|
||||
}
|
||||
}
|
||||
|
||||
/// View the Value as signed 32-bit, if possible
|
||||
pub fn as_i32(&self) -> Result<i32, ShellError> {
|
||||
match &self.value {
|
||||
UntaggedValue::Primitive(primitive) => primitive.as_i32(self.tag.span),
|
||||
@ -461,6 +477,14 @@ impl Value {
|
||||
}
|
||||
}
|
||||
|
||||
/// View the Value as signed 16-bit, if possible
|
||||
pub fn as_i16(&self) -> Result<i16, ShellError> {
|
||||
match &self.value {
|
||||
UntaggedValue::Primitive(primitive) => primitive.as_i16(self.tag.span),
|
||||
_ => Err(ShellError::type_error("integer", self.spanned_type_name())),
|
||||
}
|
||||
}
|
||||
|
||||
/// View the Value as boolean, if possible
|
||||
pub fn as_bool(&self) -> Result<bool, ShellError> {
|
||||
match &self.value {
|
||||
|
@ -60,6 +60,27 @@ pub enum Primitive {
|
||||
}
|
||||
|
||||
impl Primitive {
|
||||
/// Converts a primitive value to a char, if possible. Uses a span to build an error if the conversion isn't possible.
|
||||
pub fn as_char(&self, span: Span) -> Result<char, ShellError> {
|
||||
match self {
|
||||
Primitive::String(s) => {
|
||||
if s.len() > 1 {
|
||||
return Err(ShellError::type_error(
|
||||
"char",
|
||||
self.type_name().spanned(span),
|
||||
));
|
||||
}
|
||||
s.chars()
|
||||
.next()
|
||||
.ok_or_else(|| ShellError::type_error("char", self.type_name().spanned(span)))
|
||||
}
|
||||
other => Err(ShellError::type_error(
|
||||
"char",
|
||||
other.type_name().spanned(span),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts a primitive value to a u64, if possible. Uses a span to build an error if the conversion isn't possible.
|
||||
pub fn as_usize(&self, span: Span) -> Result<usize, ShellError> {
|
||||
match self {
|
||||
@ -108,6 +129,7 @@ impl Primitive {
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts a primitive value to a i64, if possible. Uses a span to build an error if the conversion isn't possible.
|
||||
pub fn as_i64(&self, span: Span) -> Result<i64, ShellError> {
|
||||
match self {
|
||||
Primitive::Int(int) => int.to_i64().ok_or_else(|| {
|
||||
@ -131,6 +153,30 @@ impl Primitive {
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts a primitive value to a u32, if possible. Uses a span to build an error if the conversion isn't possible.
|
||||
pub fn as_u32(&self, span: Span) -> Result<u32, ShellError> {
|
||||
match self {
|
||||
Primitive::Int(int) => int.to_u32().ok_or_else(|| {
|
||||
ShellError::range_error(
|
||||
ExpectedRange::U32,
|
||||
&format!("{}", int).spanned(span),
|
||||
"converting an integer into a unsigned 32-bit integer",
|
||||
)
|
||||
}),
|
||||
Primitive::Decimal(decimal) => decimal.to_u32().ok_or_else(|| {
|
||||
ShellError::range_error(
|
||||
ExpectedRange::U32,
|
||||
&format!("{}", decimal).spanned(span),
|
||||
"converting a decimal into a unsigned 32-bit integer",
|
||||
)
|
||||
}),
|
||||
other => Err(ShellError::type_error(
|
||||
"number",
|
||||
other.type_name().spanned(span),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_i32(&self, span: Span) -> Result<i32, ShellError> {
|
||||
match self {
|
||||
Primitive::Int(int) => int.to_i32().ok_or_else(|| {
|
||||
@ -154,6 +200,29 @@ impl Primitive {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_i16(&self, span: Span) -> Result<i16, ShellError> {
|
||||
match self {
|
||||
Primitive::Int(int) => int.to_i16().ok_or_else(|| {
|
||||
ShellError::range_error(
|
||||
ExpectedRange::I16,
|
||||
&format!("{}", int).spanned(span),
|
||||
"converting an integer into a signed 16-bit integer",
|
||||
)
|
||||
}),
|
||||
Primitive::Decimal(decimal) => decimal.to_i16().ok_or_else(|| {
|
||||
ShellError::range_error(
|
||||
ExpectedRange::I16,
|
||||
&format!("{}", decimal).spanned(span),
|
||||
"converting a decimal into a signed 16-bit integer",
|
||||
)
|
||||
}),
|
||||
other => Err(ShellError::type_error(
|
||||
"number",
|
||||
other.type_name().spanned(span),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: This is a bad name, but no other way to differentiate with our own Duration.
|
||||
pub fn into_chrono_duration(self, span: Span) -> Result<chrono::Duration, ShellError> {
|
||||
match self {
|
||||
|
Reference in New Issue
Block a user