Type Alias style::values::specified::calc::CalcNode

source ·
pub type CalcNode = GenericCalcNode<Leaf>;
Expand description

A calc node representation for specified values.

Aliased Type§

enum CalcNode {
    Leaf(Leaf),
    Negate(Box<GenericCalcNode<Leaf>, Global>),
    Invert(Box<GenericCalcNode<Leaf>, Global>),
    Sum(OwnedSlice<GenericCalcNode<Leaf>>),
    Product(OwnedSlice<GenericCalcNode<Leaf>>),
    MinMax(OwnedSlice<GenericCalcNode<Leaf>>, MinMaxOp),
    Clamp {
        min: Box<GenericCalcNode<Leaf>, Global>,
        center: Box<GenericCalcNode<Leaf>, Global>,
        max: Box<GenericCalcNode<Leaf>, Global>,
    },
    Round {
        strategy: RoundingStrategy,
        value: Box<GenericCalcNode<Leaf>, Global>,
        step: Box<GenericCalcNode<Leaf>, Global>,
    },
    ModRem {
        dividend: Box<GenericCalcNode<Leaf>, Global>,
        divisor: Box<GenericCalcNode<Leaf>, Global>,
        op: ModRemOp,
    },
    Hypot(OwnedSlice<GenericCalcNode<Leaf>>),
    Abs(Box<GenericCalcNode<Leaf>, Global>),
    Sign(Box<GenericCalcNode<Leaf>, Global>),
}

Variants§

§

Leaf(Leaf)

A leaf node.

§

Negate(Box<GenericCalcNode<Leaf>, Global>)

A node that negates its child, e.g. Negate(1) == -1.

§

Invert(Box<GenericCalcNode<Leaf>, Global>)

A node that inverts its child, e.g. Invert(10) == 1 / 10 == 0.1. The child must always resolve to a number unit.

§

Sum(OwnedSlice<GenericCalcNode<Leaf>>)

A sum node, representing a + b + c where a, b, and c are the arguments.

§

Product(OwnedSlice<GenericCalcNode<Leaf>>)

A product node, representing a * b * c where a, b, and c are the arguments.

§

MinMax(OwnedSlice<GenericCalcNode<Leaf>>, MinMaxOp)

A min or max function.

§

Clamp

Fields

§min: Box<GenericCalcNode<Leaf>, Global>

The minimum value.

§center: Box<GenericCalcNode<Leaf>, Global>

The central value.

§max: Box<GenericCalcNode<Leaf>, Global>

The maximum value.

A clamp() function.

§

Round

Fields

§strategy: RoundingStrategy

The rounding strategy.

§value: Box<GenericCalcNode<Leaf>, Global>

The value to round.

§step: Box<GenericCalcNode<Leaf>, Global>

The step value.

A round() function.

§

ModRem

Fields

§dividend: Box<GenericCalcNode<Leaf>, Global>

The dividend calculation.

§divisor: Box<GenericCalcNode<Leaf>, Global>

The divisor calculation.

§op: ModRemOp

Is the function mod or rem?

A mod() or rem() function.

§

Hypot(OwnedSlice<GenericCalcNode<Leaf>>)

A hypot() function

§

Abs(Box<GenericCalcNode<Leaf>, Global>)

An abs() function.

§

Sign(Box<GenericCalcNode<Leaf>, Global>)

A sign() function.

Implementations§

source§

impl CalcNode

source

fn parse_one<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, allowed_units: CalcUnits ) -> Result<Self, ParseError<'i>>

Tries to parse a single element in the expression, that is, a <length>, <angle>, <time>, <percentage>, <resolution>, etc.

May return a “complex” CalcNode, in the presence of a parenthesized expression, for example.

source

fn parse<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction, allowed_units: CalcUnits ) -> Result<Self, ParseError<'i>>

Parse a top-level calc expression, with all nested sub-expressions.

This is in charge of parsing, for example, 2 + 3 * 100%.

source

fn parse_angle_argument<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't> ) -> Result<CSSFloat, ParseError<'i>>

source

fn parse_number_argument<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't> ) -> Result<CSSFloat, ParseError<'i>>

source

fn parse_argument<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, allowed_units: CalcUnits ) -> Result<Self, ParseError<'i>>

source

fn parse_product<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, allowed_units: CalcUnits ) -> Result<Self, ParseError<'i>>

Parse a top-level calc expression, and all the products that may follow, and stop as soon as a non-product expression is found.

This should parse correctly:

  • 2
  • 2 * 2
  • 2 * 2 + 2 (but will leave the + 2 unparsed).
source

fn try_resolve<'i, 't, F>( input: &Parser<'i, 't>, closure: F ) -> Result<CSSFloat, ParseError<'i>>where F: FnOnce() -> Result<CSSFloat, ()>,

source

pub fn into_length_or_percentage( self, clamping_mode: AllowedNumericType ) -> Result<CalcLengthPercentage, ()>

Tries to simplify this expression into a <length> or <percentage> value.

source

fn to_time(&self, clamping_mode: Option<AllowedNumericType>) -> Result<Time, ()>

Tries to simplify this expression into a <time> value.

source

fn to_resolution(&self) -> Result<Resolution, ()>

Tries to simplify the expression into a <resolution> value.

source

fn to_angle(&self) -> Result<Angle, ()>

Tries to simplify this expression into an Angle value.

source

fn to_number(&self) -> Result<CSSFloat, ()>

Tries to simplify this expression into a <number> value.

source

fn to_percentage(&self) -> Result<CSSFloat, ()>

Tries to simplify this expression into a <percentage> value.

source

pub fn math_function<'i>( _: &ParserContext<'_>, name: &CowRcStr<'i>, location: SourceLocation ) -> Result<MathFunction, ParseError<'i>>

Given a function name, and the location from where the token came from, return a mathematical function corresponding to that name or an error.

source

pub fn parse_integer<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction ) -> Result<CSSInteger, ParseError<'i>>

Convenience parsing function for integers.

source

pub fn parse_length_or_percentage<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, clamping_mode: AllowedNumericType, function: MathFunction ) -> Result<CalcLengthPercentage, ParseError<'i>>

Convenience parsing function for <length> | <percentage>.

source

pub fn parse_percentage<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction ) -> Result<CSSFloat, ParseError<'i>>

Convenience parsing function for percentages.

source

pub fn parse_length<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, clamping_mode: AllowedNumericType, function: MathFunction ) -> Result<CalcLengthPercentage, ParseError<'i>>

Convenience parsing function for <length>.

source

pub fn parse_number<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction ) -> Result<CSSFloat, ParseError<'i>>

Convenience parsing function for <number>.

source

pub fn parse_angle<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction ) -> Result<Angle, ParseError<'i>>

Convenience parsing function for <angle>.

source

pub fn parse_time<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, clamping_mode: AllowedNumericType, function: MathFunction ) -> Result<Time, ParseError<'i>>

Convenience parsing function for <time>.

source

pub fn parse_resolution<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction ) -> Result<Resolution, ParseError<'i>>

Convenience parsing function for <resolution>.

source

pub fn parse_number_or_percentage<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction ) -> Result<NumberOrPercentage, ParseError<'i>>

Convenience parsing function for <number> or <percentage>.

source

pub fn parse_angle_or_number<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction ) -> Result<AngleOrNumber, ParseError<'i>>

Convenience parsing function for <number> or <angle>.

source§

impl<L: CalcNodeLeaf> CalcNode<L>

source

fn dummy() -> Self

Create a dummy CalcNode that can be used to do replacements of other nodes.

source

fn coerce_to_value(&mut self, value: f32)

Change all the leaf nodes to have the given value. This is useful when you have calc(1px * nan) and you want to replace the product node with calc(nan), in which case the unit will be retained.

source

pub fn is_product_distributive(&self) -> bool

Return true if a product is distributive over this node. Is distributive: (2 + 3) * 4 = 8 + 12 Not distributive: sign(2 + 3) * 4 != sign(8 + 12)

source

pub fn unit(&self) -> Result<CalcUnits, ()>

If the node has a valid unit outcome, then return it, otherwise fail.

source

pub fn negate(&mut self)

Negate the node inline. If the node is distributive, it is replaced by the result, otherwise the node is wrapped in a [Negate] node.

source

fn sort_key(&self) -> SortKey

source

pub fn as_leaf(&self) -> Option<&L>

Returns the leaf if we can (if simplification has allowed it).

source

fn try_sum_in_place(&mut self, other: &Self) -> Result<(), ()>

Tries to merge one node into another using the sum, that is, perform x + y.

source

pub fn try_product_in_place(&mut self, other: &mut Self) -> bool

Tries to merge one node into another using the product, that is, perform x * y.

source

fn try_op<O>(&self, other: &Self, op: O) -> Result<Self, ()>where O: Fn(f32, f32) -> f32,

Tries to apply a generic arithmetic operator

source

pub fn map(&mut self, op: impl FnMut(f32) -> f32)

Map the value of this node with the given operation.

source

pub fn map_leaves<O, F>(&self, map: F) -> CalcNode<O>where O: CalcNodeLeaf, F: FnMut(&L) -> O,

Convert this CalcNode into a CalcNode with a different leaf kind.

source

fn map_leaves_internal<O, F>(&self, map: &mut F) -> CalcNode<O>where O: CalcNodeLeaf, F: FnMut(&L) -> O,

source

pub fn resolve(&self) -> Result<L, ()>

Resolve this node into a value.

source

pub fn resolve_map<F>(&self, leaf_to_output_fn: F) -> Result<L, ()>where F: FnMut(&L) -> Result<L, ()>,

Resolve this node into a value, given a function that maps the leaf values.

source

fn resolve_internal<F>(&self, leaf_to_output_fn: &mut F) -> Result<L, ()>where F: FnMut(&L) -> Result<L, ()>,

source

fn is_negative_leaf(&self) -> bool

source

fn is_zero_leaf(&self) -> bool

source

fn is_infinite_leaf(&self) -> bool

source

pub fn visit_depth_first(&mut self, f: impl FnMut(&mut Self))

Visits all the nodes in this calculation tree recursively, starting by the leaves and bubbling all the way up.

This is useful for simplification, but can also be used for validation and such.

source

fn visit_depth_first_internal(&mut self, f: &mut impl FnMut(&mut Self))

source

pub fn simplify_and_sort_direct_children(&mut self)

This function simplifies and sorts the calculation of the specified node. It simplifies directly nested nodes while assuming that all nodes below it have already been simplified. It is recommended to use this function in combination with visit_depth_first().

This function is necessary only if the node needs to be preserved after parsing, specifically for <length-percentage> cases where the calculation contains percentages or relative units. Otherwise, the node can be evaluated using resolve(), which will automatically provide a simplified value.

https://drafts.csswg.org/css-values-4/#calc-simplification

source

pub fn simplify_and_sort(&mut self)

Simplifies and sorts the kids in the whole calculation subtree.

source

fn to_css_impl<W>( &self, dest: &mut CssWriter<'_, W>, level: ArgumentLevel ) -> Resultwhere W: Write,

source

fn compare( &self, other: &Self, basis_positive: PositivePercentageBasis ) -> Option<Ordering>

source

fn gt(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool

Return whether a leaf is greater than another.

source

fn lt(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool

Return whether a leaf is less than another.

source

fn lte(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool

Return whether a leaf is smaller or equal than another.

Trait Implementations§

source§

impl<L: Clone> Clone for GenericCalcNode<L>

source§

fn clone(&self) -> GenericCalcNode<L>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<L: Debug> Debug for GenericCalcNode<L>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de, L> Deserialize<'de> for GenericCalcNode<L>where L: Deserialize<'de>,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<L> MallocSizeOf for GenericCalcNode<L>where L: MallocSizeOf,

source§

fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize

Measure the heap usage of all descendant heap-allocated structures, but not the space taken up by the value itself.
source§

impl<L: PartialEq> PartialEq<GenericCalcNode<L>> for GenericCalcNode<L>

source§

fn eq(&self, other: &GenericCalcNode<L>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<L> Serialize for GenericCalcNode<L>where L: Serialize,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<L> ToAnimatedZero for GenericCalcNode<L>where L: ToAnimatedZero,

source§

fn to_animated_zero(&self) -> Result<Self, ()>

Returns a value that, when added with an underlying value, will produce the underlying value. This is used for SMIL animation’s “by-animation” where SMIL first interpolates from the zero value to the ‘by’ value, and then adds the result to the underlying value. Read more
source§

impl<L: CalcNodeLeaf> ToCss for CalcNode<L>

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Resultwhere W: Write,

source§

fn to_css_string(&self) -> String

Serialize self in CSS syntax and return a string. Read more
source§

impl<L> ToResolvedValue for GenericCalcNode<L>where L: ToResolvedValue,

§

type ResolvedValue = GenericCalcNode<<L as ToResolvedValue>::ResolvedValue>

The resolved value type we’re going to be converted to.
source§

fn from_resolved_value(from: Self::ResolvedValue) -> Self

Convert a resolved value to resolved value form.
source§

fn to_resolved_value(self, context: &Context<'_>) -> Self::ResolvedValue

Convert a resolved value to a resolved value.
source§

impl<L> ToShmem for GenericCalcNode<L>where L: ToShmem,

source§

fn to_shmem(&self, builder: &mut SharedMemoryBuilder) -> Result<Self>

Clones this value into a form suitable for writing into a SharedMemoryBuilder. Read more
source§

impl<L> StructuralPartialEq for GenericCalcNode<L>