rust: Fix Length conversions in paper-sizes crate and add tests.
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 30 Nov 2025 17:26:48 +0000 (09:26 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 30 Nov 2025 18:28:18 +0000 (10:28 -0800)
rust/Cargo.lock
rust/paper-sizes/Cargo.toml
rust/paper-sizes/src/lib.rs

index 9ed9ede56c9c19bc93673b5827c269a22ace6eb6..74ed653cff86c3a676994a90ec31261c432a6114 100644 (file)
@@ -1571,7 +1571,7 @@ dependencies = [
 
 [[package]]
 name = "paper-sizes"
-version = "0.3.0"
+version = "0.4.0"
 dependencies = [
  "bindgen",
  "libc",
index 28b88e2ed1914cb25b8e67a0476c734f26ad610f..9befd45ad4cae22f1271da8aa58fe851bb4d8639 100644 (file)
@@ -1,6 +1,6 @@
 [package]
 name = "paper-sizes"
-version = "0.3.0"
+version = "0.4.0"
 edition = "2024"
 license = "MIT OR Apache-2.0 OR LGPL-2.1-or-later"
 authors = [ "Ben Pfaff" ]
index e312374bf312d16e0c4750ec7ca99ce156bc113d..49f6c2f73abb02f809ed97a84d2802a4de1e9d8b 100644 (file)
@@ -149,7 +149,7 @@ impl Length {
     /// Returns this length converted to `unit`.
     pub fn as_unit(&self, unit: Unit) -> Self {
         Self {
-            value: self.value * unit.as_unit(Unit::Inch),
+            value: self.value * self.unit.as_unit(unit),
             unit,
         }
     }
@@ -831,7 +831,7 @@ mod tests {
     use std::{borrow::Cow, path::Path, str::FromStr};
 
     use crate::{
-        A4, CatalogBuildError, CatalogBuilder, PaperSize, PaperSpec, ParsePaperSizeError,
+        A4, CatalogBuildError, CatalogBuilder, Length, PaperSize, PaperSpec, ParsePaperSizeError,
         ParsePaperSpecError, Unit, locale,
     };
 
@@ -883,6 +883,67 @@ mod tests {
         );
     }
 
+    #[test]
+    fn length() {
+        assert_eq!(
+            format!(
+                "{:.3}",
+                Length::new(1.0, Unit::Inch).into_unit(Unit::Millimeter)
+            ),
+            "25.400"
+        );
+        assert_eq!(
+            format!("{:.3}", Length::new(1.0, Unit::Inch).into_unit(Unit::Inch)),
+            "1.000"
+        );
+        assert_eq!(
+            format!("{:.3}", Length::new(1.0, Unit::Inch).into_unit(Unit::Point)),
+            "72.000"
+        );
+        assert_eq!(
+            format!(
+                "{:.3}",
+                Length::new(36.0, Unit::Point).into_unit(Unit::Millimeter)
+            ),
+            "12.700"
+        );
+        assert_eq!(
+            format!(
+                "{:.3}",
+                Length::new(36.0, Unit::Point).into_unit(Unit::Inch)
+            ),
+            "0.500"
+        );
+        assert_eq!(
+            format!(
+                "{:.3}",
+                Length::new(36.0, Unit::Point).into_unit(Unit::Point)
+            ),
+            "36.000"
+        );
+        assert_eq!(
+            format!(
+                "{:.3}",
+                Length::new(12.7, Unit::Millimeter).into_unit(Unit::Millimeter)
+            ),
+            "12.700"
+        );
+        assert_eq!(
+            format!(
+                "{:.3}",
+                Length::new(12.7, Unit::Millimeter).into_unit(Unit::Inch)
+            ),
+            "0.500"
+        );
+        assert_eq!(
+            format!(
+                "{:.3}",
+                Length::new(12.7, Unit::Millimeter).into_unit(Unit::Point)
+            ),
+            "36.000"
+        );
+    }
+
     #[test]
     fn papersize() {
         assert_eq!(