From e9dc4bb20404037cf192c19f00a010ff3bb1a10b Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 23 Jun 2025 11:12:58 +0200 Subject: Typed HTML API (#6476) --- crates/typst-library/src/foundations/float.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'crates/typst-library/src/foundations/float.rs') 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 { 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 { + (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")?, +} -- cgit v1.2.3