use super::prelude::*;
use crate::{AngleOrZero, Length, LengthPercentage, NoneOr, NumberOrPercentage};
use css_parse::BumpBox;
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
#[derive(csskit_derives::NodeWithMetadata)]
pub enum TransformFunction<'a> {
Matrix(MatrixFunction),
Matrix3d(BumpBox<'a, Matrix3dFunction>),
Translate(TranslateFunction),
Translate3d(Translate3dFunction),
TranslateX(TranslatexFunction),
TranslateY(TranslateyFunction),
TranslateZ(TranslatezFunction),
Scale(ScaleFunction),
Scale3d(Scale3dFunction),
ScaleX(ScalexFunction),
ScaleY(ScaleyFunction),
ScaleZ(ScalezFunction),
Rotate(RotateFunction),
Rotate3d(Rotate3dFunction),
RotateX(RotatexFunction),
RotateY(RotateyFunction),
RotateZ(RotatezFunction),
Skew(SkewFunction),
SkewX(SkewxFunction),
SkewY(SkewyFunction),
Perspective(PerspectiveFunction),
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct MatrixFunction {
#[atom(CssAtomSet::Matrix)]
pub name: T![Function],
pub params: MatrixFunctionParams,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
pub struct MatrixFunctionParams(
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
);
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct Matrix3dFunction {
#[atom(CssAtomSet::Matrix3d)]
pub name: T![Function],
pub params: Matrix3dFunctionParams,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[allow(clippy::type_complexity)] pub struct Matrix3dFunctionParams(
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
);
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct TranslateFunction {
#[atom(CssAtomSet::Translate)]
pub name: T![Function],
pub x: LengthPercentage,
pub comma: Option<T![,]>,
pub y: Option<LengthPercentage>,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct Translate3dFunction {
#[atom(CssAtomSet::Translate3d)]
pub name: T![Function],
pub params: Translate3dFunctionParams,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
pub struct Translate3dFunctionParams(
pub LengthPercentage,
pub Option<T![,]>,
pub LengthPercentage,
pub Option<T![,]>,
pub Length,
);
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct TranslatexFunction {
#[atom(CssAtomSet::Translatex)]
pub name: T![Function],
pub params: LengthPercentage,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct TranslateyFunction {
#[atom(CssAtomSet::Translatey)]
pub name: T![Function],
pub params: LengthPercentage,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct TranslatezFunction {
#[atom(CssAtomSet::Translatez)]
pub name: T![Function],
pub params: Length,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct ScaleFunction {
#[atom(CssAtomSet::Scale)]
pub name: T![Function],
pub params: (NumberOrPercentage, Option<T![,]>, Option<NumberOrPercentage>),
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct Scale3dFunction {
#[atom(CssAtomSet::Scale3d)]
pub name: T![Function],
pub params: Scale3dFunctionParams,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
pub struct Scale3dFunctionParams(
pub NumberOrPercentage,
pub Option<T![,]>,
pub NumberOrPercentage,
pub Option<T![,]>,
pub NumberOrPercentage,
);
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct ScalexFunction {
#[atom(CssAtomSet::Scalex)]
pub name: T![Function],
pub params: NumberOrPercentage,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct ScaleyFunction {
#[atom(CssAtomSet::Scaley)]
pub name: T![Function],
pub params: NumberOrPercentage,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct ScalezFunction {
#[atom(CssAtomSet::Scalez)]
pub name: T![Function],
pub params: NumberOrPercentage,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct RotateFunction {
#[atom(CssAtomSet::Rotate)]
pub name: T![Function],
pub params: AngleOrZero,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct Rotate3dFunction {
#[atom(CssAtomSet::Rotate3d)]
pub name: T![Function],
pub params: Rotate3dFunctionParams,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
pub struct Rotate3dFunctionParams(
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub T![Number],
pub Option<T![,]>,
pub AngleOrZero,
);
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct RotatexFunction {
#[atom(CssAtomSet::Rotatex)]
pub name: T![Function],
pub params: AngleOrZero,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct RotateyFunction {
#[atom(CssAtomSet::Rotatey)]
pub name: T![Function],
pub params: AngleOrZero,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct RotatezFunction {
#[atom(CssAtomSet::Rotatez)]
pub name: T![Function],
pub params: AngleOrZero,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct SkewFunction {
#[atom(CssAtomSet::Skew)]
pub name: T![Function],
pub params: (AngleOrZero, Option<T![,]>, Option<AngleOrZero>),
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct SkewxFunction {
#[atom(CssAtomSet::Skewx)]
pub name: T![Function],
pub params: AngleOrZero,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct SkewyFunction {
#[atom(CssAtomSet::Skewy)]
pub name: T![Function],
pub params: AngleOrZero,
pub close: T![')'],
}
#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct PerspectiveFunction {
#[atom(CssAtomSet::Perspective)]
pub name: T![Function],
pub params: NoneOr<Length>,
pub close: T![')'],
}
#[cfg(test)]
mod tests {
use super::*;
use crate::CssAtomSet;
use css_parse::{assert_parse, assert_parse_error, assert_parse_span};
#[test]
fn size_test() {
assert_eq!(std::mem::size_of::<TransformFunction>(), 176);
}
#[test]
fn test_writes() {
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "matrix(1,2,3,4,5,6)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "matrix(1 2 3 4 5 6)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "matrix(0,0,0,0,0,0)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "matrix(-1,-2,-3,-4,-5,-6)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "matrix(1.5,2.5,3.5,4.5,5.5,6.5)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "translate(10px)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "translate(10px,20px)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "translate(45%)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "translate(2rem)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "translateX(1rem)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "translateY(1rem)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "scale(1,2)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "scale(0,0)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "scale(1)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "scale(1.5,2.5)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "scaleX(2)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "scaleY(2)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "rotate(45deg)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "rotate(0)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "rotate(2turn)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "rotate(20rad)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "skew(1deg,2deg)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "skew(0,0)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "skew(1deg)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "skewX(1deg)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "skewX(0)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "skewY(1deg)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "skewY(0)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "scale3d(10%,10%,10%)");
assert_parse!(CssAtomSet::ATOMS, TransformFunction, "rotate3d(1,2,3,10deg)");
}
#[test]
fn test_span() {
assert_parse_span!(
CssAtomSet::ATOMS,
TransformFunction,
r#"
matrix(1,2,3,4,5,6) translate(0)
^^^^^^^^^^^^^^^^^^^
"#
);
assert_parse_span!(
CssAtomSet::ATOMS,
TransformFunction,
r#"
translate(0) foo
^^^^^^^^^^^^
"#
);
assert_parse_span!(
CssAtomSet::ATOMS,
TranslateFunction,
r#"
translate(0) bar
^^^^^^^^^^^^
"#
);
}
#[test]
fn test_errors() {
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "matrix()");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "matrix(1)");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "matrix(1,2)");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "matrix(one,two,three,four,five,size)");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "translate()");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "translate(foo)");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "translateX()");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "translateX(foo)");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "translateY()");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "translateY(foo)");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "scale()");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "scale(foo)");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "scaleX()");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "scaleX(foo)");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "scaleY()");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "scaleY(foo)");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "rotate()");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "rotate(45px)");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "rotate(all the way around)");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "skew()");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "skew(foo)");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "skewX()");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "skewX(foo)");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "skewY()");
assert_parse_error!(CssAtomSet::ATOMS, TransformFunction, "skewY(foo)");
}
}