summaryrefslogtreecommitdiff
path: root/crates/typst-library/src/foundations/float.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2025-06-23 11:12:58 +0200
committerGitHub <noreply@github.com>2025-06-23 09:12:58 +0000
commite9dc4bb20404037cf192c19f00a010ff3bb1a10b (patch)
tree46dca870d8809b7749f5e00e4c40aa23d909c521 /crates/typst-library/src/foundations/float.rs
parent3602d06a155a0567fe2b2e75a4d5970578d0f14f (diff)
Typed HTML API (#6476)
Diffstat (limited to 'crates/typst-library/src/foundations/float.rs')
-rw-r--r--crates/typst-library/src/foundations/float.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/crates/typst-library/src/foundations/float.rs b/crates/typst-library/src/foundations/float.rs
index 21d0a8d8..353e498d 100644
--- a/crates/typst-library/src/foundations/float.rs
+++ b/crates/typst-library/src/foundations/float.rs
@@ -210,3 +210,25 @@ cast! {
fn parse_float(s: EcoString) -> Result<f64, ParseFloatError> {
s.replace(repr::MINUS_SIGN, "-").parse()
}
+
+/// A floating-point number that must be positive (strictly larger than zero).
+#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
+pub struct PositiveF64(f64);
+
+impl PositiveF64 {
+ /// Wrap a float if it is positive.
+ pub fn new(value: f64) -> Option<Self> {
+ (value > 0.0).then_some(Self(value))
+ }
+
+ /// Get the underlying value.
+ pub fn get(self) -> f64 {
+ self.0
+ }
+}
+
+cast! {
+ PositiveF64,
+ self => self.get().into_value(),
+ v: f64 => Self::new(v).ok_or("number must be positive")?,
+}