From: Ben Pfaff Date: Sun, 30 Nov 2025 17:26:48 +0000 (-0800) Subject: rust: Fix Length conversions in paper-sizes crate and add tests. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7e73793a073faa515fc45662840ee9f85d964d6f;p=pspp rust: Fix Length conversions in paper-sizes crate and add tests. --- diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 9ed9ede56c..74ed653cff 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -1571,7 +1571,7 @@ dependencies = [ [[package]] name = "paper-sizes" -version = "0.3.0" +version = "0.4.0" dependencies = [ "bindgen", "libc", diff --git a/rust/paper-sizes/Cargo.toml b/rust/paper-sizes/Cargo.toml index 28b88e2ed1..9befd45ad4 100644 --- a/rust/paper-sizes/Cargo.toml +++ b/rust/paper-sizes/Cargo.toml @@ -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" ] diff --git a/rust/paper-sizes/src/lib.rs b/rust/paper-sizes/src/lib.rs index e312374bf3..49f6c2f73a 100644 --- a/rust/paper-sizes/src/lib.rs +++ b/rust/paper-sizes/src/lib.rs @@ -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!(