pub enum Expression {
Literal(Length),
Percentage(Percentage),
Add(Box<Expression>, Box<Expression>),
Sub(Box<Expression>, Box<Expression>),
Mul(Box<Expression>, f64),
Div(Box<Expression>, f64),
}Expand description
Mathematical expression for CSS calc() values
Supports:
- Literals (absolute lengths)
- Percentages (relative to base value)
- Binary operations: +, -, *, /
- Nested expressions
§Examples
use fop_types::{Expression, Length, Percentage, expression::EvalContext};
// Parse calc(100% - 20pt)
let expr = Expression::parse("calc(100% - 20pt)").unwrap();
// Evaluate with context
let ctx = EvalContext {
base_width: Some(Length::from_pt(200.0)),
base_height: None,
font_size: Length::from_pt(12.0),
};
let result = expr.evaluate(&ctx).unwrap();
assert_eq!(result, Length::from_pt(180.0));Variants§
Literal(Length)
A literal length value
Percentage(Percentage)
A percentage value (needs base to resolve)
Add(Box<Expression>, Box<Expression>)
Addition of two expressions
Sub(Box<Expression>, Box<Expression>)
Subtraction of two expressions
Mul(Box<Expression>, f64)
Multiplication of expression by scalar
Div(Box<Expression>, f64)
Division of expression by scalar
Implementations§
Source§impl Expression
impl Expression
Sourcepub fn parse(input: &str) -> Result<Self>
pub fn parse(input: &str) -> Result<Self>
Parse a calc() expression from a string
§Examples
use fop_types::Expression;
let expr = Expression::parse("calc(100% - 20pt)").unwrap();
let expr2 = Expression::parse("calc(50% + 10mm)").unwrap();
let expr3 = Expression::parse("calc((100% - 40pt) / 2)").unwrap();Sourcepub fn evaluate(&self, context: &EvalContext) -> Result<Length>
pub fn evaluate(&self, context: &EvalContext) -> Result<Length>
Evaluate the expression to a concrete length
§Examples
use fop_types::{Expression, Length, expression::EvalContext};
let expr = Expression::parse("calc(100% - 20pt)").unwrap();
let ctx = EvalContext::with_width(Length::from_pt(200.0), Length::from_pt(12.0));
let result = expr.evaluate(&ctx).unwrap();
assert_eq!(result, Length::from_pt(180.0));Trait Implementations§
Source§impl Clone for Expression
impl Clone for Expression
Source§fn clone(&self) -> Expression
fn clone(&self) -> Expression
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for Expression
impl Debug for Expression
Source§impl Display for Expression
impl Display for Expression
Source§impl PartialEq for Expression
impl PartialEq for Expression
impl StructuralPartialEq for Expression
Auto Trait Implementations§
impl Freeze for Expression
impl RefUnwindSafe for Expression
impl Send for Expression
impl Sync for Expression
impl Unpin for Expression
impl UnsafeUnpin for Expression
impl UnwindSafe for Expression
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more