Skip to content

Truly independent web browser

LibWeb: Promote CalcRoundingStrategy to an enum

Callum Law7ac5f85386d3
diff --git a/Libraries/LibWeb/Rust/src/css/calc.rs b/Libraries/LibWeb/Rust/src/css/calc.rs
index 17dad788a2c..e4a8d5ed1ed 100644
--- a/Libraries/LibWeb/Rust/src/css/calc.rs
+++ b/Libraries/LibWeb/Rust/src/css/calc.rs
@@ -565,8 +565,15 @@ pub unsafe extern "C" fn rust_numeric_type_matches(
}
/// https://drafts.csswg.org/css-values-4/#round-func
-/// The rounding strategy of round(), opaque code from the C++ side.
-pub type CalcRoundingStrategy = u8;
+/// The rounding strategy of round()
+#[derive(Clone, Copy, PartialEq)]
+pub enum CalcRoundingStrategy {
+ Nearest,
+ Up,
+ Down,
+ ToZero,
+ // FIXME: Support line-width
+}
/// One node of a calculation tree. Child nodes are shared immutably.
///
@@ -1518,10 +1525,6 @@ impl CalcNode {
})
};
- // Rounding strategy codes follow the generated C++ RoundingStrategy
- // order: down, nearest, to-zero, up.
- let (down, nearest, to_zero, up) = (0u8, 1u8, 2u8, 3u8);
-
// https://drafts.csswg.org/css-values-4/#round-infinities
// In round(A, B), if B is 0, the result is NaN. If A and B are both infinite, the result is NaN.
if b == 0.0 || (a.is_infinite() && b.is_infinite()) {
@@ -1535,11 +1538,11 @@ impl CalcNode {
if a.is_finite() && b.is_infinite() {
let negative_zero = a.is_sign_negative();
let signed_zero = if negative_zero { -0.0 } else { 0.0 };
- let result = match *strategy {
+ let result = match strategy {
// nearest, to-zero: If A is positive or 0+, return 0+. Otherwise, return 0-.
- s if s == nearest || s == to_zero => signed_zero,
+ CalcRoundingStrategy::Nearest | CalcRoundingStrategy::ToZero => signed_zero,
// up: If A is positive (not zero), return +Infinity; else the signed zero.
- s if s == up => {
+ CalcRoundingStrategy::Up => {
if a > 0.0 {
f64::INFINITY
} else {
@@ -1547,14 +1550,13 @@ impl CalcNode {
}
}
// down: If A is negative (not zero), return -Infinity; else the signed zero.
- s if s == down => {
+ CalcRoundingStrategy::Down => {
if a < 0.0 {
f64::NEG_INFINITY
} else {
signed_zero
}
}
- _ => unreachable!("invalid rounding strategy {strategy}"),
};
return result_with(result);
}
@@ -1573,28 +1575,27 @@ impl CalcNode {
// if upper B would be zero, it is specifically equal to 0⁻.
let lower_b = (a / b).floor() * b;
let upper_b = (a / b).ceil() * b;
- let rounded = match *strategy {
+ let rounded = match strategy {
// nearest: whichever of lower B and upper B has the smallest absolute
// difference from A; ties choose upper B.
- s if s == nearest => {
+ CalcRoundingStrategy::Nearest => {
if (upper_b - a).abs() <= (lower_b - a).abs() {
upper_b
} else {
lower_b
}
}
- s if s == up => upper_b,
- s if s == down => lower_b,
+ CalcRoundingStrategy::Up => upper_b,
+ CalcRoundingStrategy::Down => lower_b,
// to-zero: whichever of lower B and upper B has the smallest absolute
// difference from 0.
- s if s == to_zero => {
+ CalcRoundingStrategy::ToZero => {
if upper_b.abs() < lower_b.abs() {
upper_b
} else {
lower_b
}
}
- _ => unreachable!("invalid rounding strategy {strategy}"),
};
result_with(rounded)
}
@@ -3733,11 +3734,10 @@ impl CalcSerializer<'_> {
// hope for the best.
if let CalcNode::Round { strategy, .. } = &**node {
self.literal(match *strategy {
- 0 => "down",
- 1 => "nearest",
- 2 => "to-zero",
- 3 => "up",
- _ => unreachable!("invalid rounding strategy"),
+ CalcRoundingStrategy::Down => "down",
+ CalcRoundingStrategy::Nearest => "nearest",
+ CalcRoundingStrategy::ToZero => "to-zero",
+ CalcRoundingStrategy::Up => "up",
});
first = false;
}
diff --git a/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs
index 9b222edf9ec..55f42fb5dd1 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs
@@ -14,7 +14,8 @@ use super::value_parser::{
TIME_UNIT_NAMES, context_allows_tree_counting_functions, parse_calculated_numeric_value_with_ranges,
};
use crate::css::calc::{
- CalcNode, CalcNumericType, CalcNumericValue, resolve_as_for_value_type, simplify_parsed_calculation,
+ CalcNode, CalcNumericType, CalcNumericValue, CalcRoundingStrategy, resolve_as_for_value_type,
+ simplify_parsed_calculation,
};
use crate::css::css_enums::{keyword_from_ascii_case_insensitive, keyword_to_channel_keyword};
use crate::css::css_string::CssString;
@@ -515,7 +516,7 @@ pub(crate) fn parse_a_calc_function_node(
let strategy = parse_rounding_strategy(arguments[0]).ok_or(CalcParseError::Invalid)?;
(strategy, &arguments[1..])
} else {
- (1, arguments.as_slice())
+ (CalcRoundingStrategy::Nearest, arguments.as_slice())
};
let nodes = parse_fixed_arguments(arguments, 2, context)?;
let types = nodes
@@ -540,20 +541,20 @@ pub(crate) fn parse_a_calc_function_node(
.ok_or(CalcParseError::Invalid)
}
-fn parse_rounding_strategy(values: &[ComponentValue]) -> Option<u8> {
+fn parse_rounding_strategy(values: &[ComponentValue]) -> Option<CalcRoundingStrategy> {
let mut values = values.iter().filter(|value| !value.is_whitespace());
let identifier = values.next()?.ident()?;
if values.next().is_some() {
return None;
}
if equals_ascii_case_insensitive(identifier, b"down") {
- Some(0)
+ Some(CalcRoundingStrategy::Down)
} else if equals_ascii_case_insensitive(identifier, b"nearest") {
- Some(1)
+ Some(CalcRoundingStrategy::Nearest)
} else if equals_ascii_case_insensitive(identifier, b"to-zero") {
- Some(2)
+ Some(CalcRoundingStrategy::ToZero)
} else if equals_ascii_case_insensitive(identifier, b"up") {
- Some(3)
+ Some(CalcRoundingStrategy::Up)
} else {
None
}