Let decimal constructor accept decimal values (#5481)

This commit is contained in:
+merlan #flirora 2024-11-27 11:36:04 -05:00 committed by GitHub
parent 27cc489a1d
commit 89d96c623d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 1 deletions

View File

@ -317,6 +317,7 @@ impl Decimal {
}) })
.at(value.span) .at(value.span)
} }
ToDecimal::Decimal(decimal) => Ok(decimal),
} }
} }
} }
@ -429,6 +430,8 @@ impl Hash for Decimal {
/// A value that can be cast to a decimal. /// A value that can be cast to a decimal.
pub enum ToDecimal { pub enum ToDecimal {
/// A decimal to be converted to itself.
Decimal(Decimal),
/// A string with the decimal's representation. /// A string with the decimal's representation.
Str(EcoString), Str(EcoString),
/// An integer to be converted to the equivalent decimal. /// An integer to be converted to the equivalent decimal.
@ -439,7 +442,9 @@ pub enum ToDecimal {
cast! { cast! {
ToDecimal, ToDecimal,
v: Decimal => Self::Decimal(v),
v: i64 => Self::Int(v), v: i64 => Self::Int(v),
v: bool => Self::Int(v as i64),
v: f64 => Self::Float(v), v: f64 => Self::Float(v),
v: Str => Self::Str(EcoString::from(v)), v: Str => Self::Str(EcoString::from(v)),
} }

View File

@ -4,10 +4,12 @@
#test(decimal("\u{2212}7654.321"), decimal("-7654.321")) #test(decimal("\u{2212}7654.321"), decimal("-7654.321"))
#test(decimal({ 3.141592653 }), decimal("3.141592653000000012752934707")) #test(decimal({ 3.141592653 }), decimal("3.141592653000000012752934707"))
#test(decimal({ -3.141592653 }), decimal("-3.141592653000000012752934707")) #test(decimal({ -3.141592653 }), decimal("-3.141592653000000012752934707"))
#test(decimal(decimal(3)), decimal("3.0"))
#test(decimal(true), decimal("1.0"))
#test(type(decimal(10)), decimal) #test(type(decimal(10)), decimal)
--- decimal-constructor-bad-type --- --- decimal-constructor-bad-type ---
// Error: 10-17 expected integer, float, or string, found type // Error: 10-17 expected decimal, integer, boolean, float, or string, found type
#decimal(decimal) #decimal(decimal)
--- decimal-constructor-bad-value --- --- decimal-constructor-bad-value ---