summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author+merlan #flirora <uruwi@protonmail.com>2024-11-27 11:36:04 -0500
committerGitHub <noreply@github.com>2024-11-27 16:36:04 +0000
commit89d96c623dc882fcdbff02e201bcfb27e6bbfd2a (patch)
tree9557298a2be59ab2ebf5fe07462b8ddcb056b348
parent27cc489a1d98c6badd6151b5ed70af4a532692d9 (diff)
Let decimal constructor accept decimal values (#5481)
-rw-r--r--crates/typst-library/src/foundations/decimal.rs5
-rw-r--r--tests/suite/foundations/decimal.typ4
2 files changed, 8 insertions, 1 deletions
diff --git a/crates/typst-library/src/foundations/decimal.rs b/crates/typst-library/src/foundations/decimal.rs
index cf11e1dd..d363a6a4 100644
--- a/crates/typst-library/src/foundations/decimal.rs
+++ b/crates/typst-library/src/foundations/decimal.rs
@@ -317,6 +317,7 @@ impl Decimal {
})
.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.
pub enum ToDecimal {
+ /// A decimal to be converted to itself.
+ Decimal(Decimal),
/// A string with the decimal's representation.
Str(EcoString),
/// An integer to be converted to the equivalent decimal.
@@ -439,7 +442,9 @@ pub enum ToDecimal {
cast! {
ToDecimal,
+ v: Decimal => Self::Decimal(v),
v: i64 => Self::Int(v),
+ v: bool => Self::Int(v as i64),
v: f64 => Self::Float(v),
v: Str => Self::Str(EcoString::from(v)),
}
diff --git a/tests/suite/foundations/decimal.typ b/tests/suite/foundations/decimal.typ
index bae0d2e6..6b3787e9 100644
--- a/tests/suite/foundations/decimal.typ
+++ b/tests/suite/foundations/decimal.typ
@@ -4,10 +4,12 @@
#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(decimal(3)), decimal("3.0"))
+#test(decimal(true), decimal("1.0"))
#test(type(decimal(10)), decimal)
--- 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-constructor-bad-value ---