Begin migration away from arg serialization (#3281)

* initial implementation

* Move a few commands over to new arg system

* Fix char also
This commit is contained in:
Jonathan Turner
2021-04-08 20:15:36 +12:00
committed by GitHub
parent 5fcf11fcb0
commit 09a1f5acb9
20 changed files with 442 additions and 224 deletions

View File

@ -429,6 +429,14 @@ impl Value {
matches!(&self.value, UntaggedValue::Primitive(_))
}
/// View the Value as unsigned size, if possible
pub fn as_usize(&self) -> Result<usize, ShellError> {
match &self.value {
UntaggedValue::Primitive(primitive) => primitive.as_usize(self.tag.span),
_ => Err(ShellError::type_error("integer", self.spanned_type_name())),
}
}
/// View the Value as unsigned 64-bit, if possible
pub fn as_u64(&self) -> Result<u64, ShellError> {
match &self.value {

View File

@ -60,6 +60,30 @@ pub enum Primitive {
}
impl Primitive {
/// 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 {
Primitive::Int(int) => int.to_usize().ok_or_else(|| {
ShellError::range_error(
ExpectedRange::U64,
&format!("{}", int).spanned(span),
"converting an integer into an unsigned 64-bit integer",
)
}),
Primitive::Decimal(decimal) => decimal.to_usize().ok_or_else(|| {
ShellError::range_error(
ExpectedRange::U64,
&format!("{}", decimal).spanned(span),
"converting a decimal into an unsigned 64-bit integer",
)
}),
other => Err(ShellError::type_error(
"number",
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_u64(&self, span: Span) -> Result<u64, ShellError> {
match self {