Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions float-pigment-css/src/parser/property_value/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl CalcExpr {
CalcExpr::Angle(angle) => !matches!(angle.as_ref(), Angle::Calc(_)),
CalcExpr::Number(num) => !matches!(num.as_ref(), Number::Calc(_)),
CalcExpr::Length(length) => !matches!(
length.as_ref(),
length,
Length::Expr(_) | Length::Auto | Length::Undefined
),
_ => false,
Expand Down Expand Up @@ -69,7 +69,7 @@ impl CalcExpr {
} else {
length.to_f32() / rhs
};
let ret = match length.as_ref() {
let ret = match length {
Length::Px(_) => Length::Px(v),
Length::Em(_) => Length::Em(v),
Length::Rpx(_) => Length::Rpx(v),
Expand All @@ -81,7 +81,7 @@ impl CalcExpr {
Length::Vmin(_) => Length::Vmin(v),
_ => unreachable!(),
};
CalcExpr::Length(Box::new(ret))
CalcExpr::Length(ret)
}
CalcExpr::Number(num) => {
let ret = if mul {
Expand Down Expand Up @@ -176,10 +176,7 @@ impl ComputeCalcExpr<Angle> {
_ => None,
}
}
CalcExpr::Length(l) => match l.as_ref() {
Length::Ratio(ratio) => Some(Angle::from_ratio(*ratio)),
_ => None,
},
CalcExpr::Length(Length::Ratio(ratio)) => Some(Angle::from_ratio(*ratio)),
_ => None,
}
}
Expand Down Expand Up @@ -250,7 +247,7 @@ impl LengthUnit {
impl ComputeCalcExpr<Length> {
pub(crate) fn try_compute(expr: &CalcExpr) -> Option<Length> {
match expr {
CalcExpr::Length(l) => Some(*l.clone()),
CalcExpr::Length(l) => Some(l.clone()),
CalcExpr::Angle(_) | CalcExpr::Number(_) => None,
CalcExpr::Plus(l, r) | CalcExpr::Sub(l, r) => {
let l = Self::try_compute(l)?;
Expand Down Expand Up @@ -334,17 +331,11 @@ pub(crate) enum ExpectValueType {
fn combine_calc_expr(operator: Operator, lhs: CalcExpr, rhs: CalcExpr) -> CalcExpr {
// Unwrap nested `Length::Expr(Calc(...))` to flatten the expression tree.
let final_lhs = match lhs {
CalcExpr::Length(length_expr) => match *length_expr {
Length::Expr(LengthExpr::Calc(calc_expr)) => calc_expr,
other => Box::new(CalcExpr::Length(Box::new(other))),
},
CalcExpr::Length(length_expr) => length_expr.into_calc_expr(),
other => Box::new(other),
};
let final_rhs = match rhs {
CalcExpr::Length(length_expr) => match *length_expr {
Length::Expr(LengthExpr::Calc(calc_expr)) => calc_expr,
other => Box::new(CalcExpr::Length(Box::new(other))),
},
CalcExpr::Length(length_expr) => length_expr.into_calc_expr(),
other => Box::new(other),
};
match operator {
Expand Down Expand Up @@ -501,7 +492,7 @@ fn parse_calc_value<'a, 't: 'a, 'i: 't>(
if expect_type == ExpectValueType::NumberAndLength
|| expect_type == ExpectValueType::AngleAndLength
{
return Ok(CalcExpr::Length(Box::new(length)));
return Ok(CalcExpr::Length(length));
}
}
// match angle
Expand Down
8 changes: 4 additions & 4 deletions float-pigment-css/src/parser/property_value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,18 @@ fn parse_length_inner<'a, 't: 'a, 'i: 't>(
env_default_value(parser, properties, st)
})
})?;
return Ok(Length::Expr(LengthExpr::Env(
return Ok(Length::Expr(Box::new(LengthExpr::Env(
name.into(),
Box::new(default_value.unwrap_or(Length::Undefined)),
)));
))));
}
"calc" => {
return parse_calc_inner(parser, properties, st, ExpectValueType::NumberAndLength)
.map(|ret| {
if let Some(r) = ComputeCalcExpr::<Length>::try_compute(&ret) {
return r;
}
Length::Expr(LengthExpr::Calc(Box::new(ret)))
Length::Expr(Box::new(LengthExpr::Calc(Box::new(ret))))
});
}
_ => {}
Expand Down Expand Up @@ -401,7 +401,7 @@ pub(crate) fn percentage<'a, 't: 'a, 'i: 't>(
if let Some(r) = ComputeCalcExpr::<Length>::try_compute(&ret) {
return r;
}
Length::Expr(LengthExpr::Calc(Box::new(ret)))
Length::Expr(Box::new(LengthExpr::Calc(Box::new(ret))))
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions float-pigment-css/src/sheet/borrow_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ use super::{borrow::Array, str_store::*};

#[repr(C)]
#[derive(Debug, Serialize, Deserialize)]
#[allow(dead_code)]
pub(crate) enum StyleSheetImportIndex {
None,
V1(StyleSheetImportIndexV1),
}

#[repr(C)]
#[derive(Debug)]
#[allow(dead_code)]
pub(crate) struct StyleSheetImportIndexV1 {
buf: StrBuffer,
deps: Array<(StrRef, Array<StrRef>)>,
Expand Down
29 changes: 24 additions & 5 deletions float-pigment-css/src/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub enum ImportantBitSet {
#[cfg_attr(debug_assertions, derive(CompatibilityEnumCheck))]
pub enum CalcExpr {
/// A length, e.g. `2px`.
Length(Box<Length>),
Length(Length),
/// A number, e.g. `0.1`.
Number(Box<Number>),
/// An angle, e.g. `45deg`.
Expand All @@ -58,7 +58,7 @@ pub enum CalcExpr {

impl Default for CalcExpr {
fn default() -> Self {
Self::Length(Box::new(Length::Undefined))
Self::Length(Length::Undefined)
}
}

Expand Down Expand Up @@ -150,7 +150,7 @@ pub enum Length {
Rpx(f32),
Em(f32),
Ratio(f32),
Expr(LengthExpr),
Expr(Box<LengthExpr>),
Vmin(f32),
Vmax(f32),
}
Expand Down Expand Up @@ -188,6 +188,25 @@ impl Length {
}
}

/// Create a new `Length` from a `CalcExpr`.
pub fn new_calc_expr(calc_expr: Box<CalcExpr>) -> Self {
Self::Expr(Box::new(LengthExpr::Calc(calc_expr)))
}

/// Convert to `Box<CalcExpr>`.
///
/// If the length is already a `CalcExpr`, it will be returned directly.
/// Otherwise, it will be wrapped into a new `CalcExpr`.
pub fn into_calc_expr(self) -> Box<CalcExpr> {
match self {
Length::Expr(x) => match *x {
LengthExpr::Calc(x) => x,
x => Box::new(CalcExpr::Length(Length::Expr(Box::new(x)))),
},
x => Box::new(CalcExpr::Length(x)),
}
}

/// Resolve the length value to `f32`.
///
/// The `relative_length` is used to calculate `...%` length.
Expand All @@ -214,7 +233,7 @@ impl Length {
}
}
Length::Ratio(x) => relative_length * *x,
Length::Expr(x) => match x {
Length::Expr(x) => match &**x {
LengthExpr::Invalid => None?,
LengthExpr::Env(name, default_value) => match name.as_str() {
"safe-area-inset-left" => media_query_status.env.safe_area_inset_left.to_f32(),
Expand Down Expand Up @@ -268,7 +287,7 @@ impl Length {
) -> Option<L> {
let r = match self {
Length::Undefined | Length::Auto => None?,
Length::Expr(x) => match x {
Length::Expr(x) => match &**x {
LengthExpr::Invalid => None?,
LengthExpr::Env(name, default_value) => match name.as_str() {
"safe-area-inset-left" => media_query_status.env.safe_area_inset_left,
Expand Down
2 changes: 1 addition & 1 deletion float-pigment-css/src/typing_stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ impl fmt::Display for Length {
&tmp
}
Length::Expr(expr) => {
match expr {
match &**expr {
LengthExpr::Calc(calc_expr) => {
tmp = calc_expr.to_string();
&tmp
Expand Down
Loading
Loading