use super::angle::impl_try_from_angle;
use super::calc::Calc;
use crate::error::{ParserError, PrinterError};
use crate::traits::private::AddInternal;
use crate::traits::{IsCompatible, Map, Op, Parse, ParseNumeric, Sign, ToCss, TrySign, Zero};
#[cfg(feature = "visitor")]
use crate::visitor::Visit;
use cssparser::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NumericRange {
All,
NonNegative,
}
impl NumericRange {
pub fn check(
&self,
value: f32,
location: SourceLocation,
) -> Result<f32, ParseError<'static, ParserError<'static>>> {
match self {
NumericRange::All => Ok(value),
NumericRange::NonNegative if value >= 0.0 => Ok(value),
_ => Err(location.new_custom_error(ParserError::InvalidValue)),
}
}
pub fn clamp(&self, value: f32) -> f32 {
match self {
NumericRange::All => value,
NumericRange::NonNegative => value.max(0.0),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(transparent))]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
pub struct NonNegative<T>(pub T);
impl<'i, T: ParseNumeric<'i>> Parse<'i> for NonNegative<T> {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
T::parse_with_range(input, NumericRange::NonNegative).map(Self)
}
}
impl<T: ToCss> ToCss for NonNegative<T> {
fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
self.0.to_css(dest)
}
}
impl<T: IsCompatible> IsCompatible for NonNegative<T> {
fn is_compatible(&self, browsers: crate::targets::Browsers) -> bool {
self.0.is_compatible(browsers)
}
}
impl<T: Zero> Zero for NonNegative<T> {
fn zero() -> Self {
Self(T::zero())
}
fn is_zero(&self) -> bool {
self.0.is_zero()
}
}
impl<T: TrySign> TrySign for NonNegative<T> {
fn try_sign(&self) -> Option<f32> {
self.0.try_sign()
}
}
pub type CSSNumber = f32;
impl<'i> Parse<'i> for CSSNumber {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
Self::parse_with_range(input, NumericRange::All)
}
}
impl<'i> ParseNumeric<'i> for CSSNumber {
fn parse_with_range<'t>(
input: &mut Parser<'i, 't>,
range: NumericRange,
) -> Result<Self, ParseError<'i, ParserError<'i>>> {
match input.try_parse(Calc::parse) {
Ok(Calc::Value(v)) => return Ok(range.clamp(*v)),
Ok(Calc::Number(n)) => return Ok(range.clamp(n)),
Ok(_) => return Err(input.new_custom_error(ParserError::InvalidValue)),
_ => {}
}
let location = input.current_source_location();
let number = input.expect_number()?;
range.check(number, location)
}
}
impl ToCss for CSSNumber {
fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
let number = *self;
if number != 0.0 && number.abs() < 1.0 {
let mut s = String::new();
cssparser::ToCss::to_css(self, &mut s)?;
if number < 0.0 {
dest.write_char('-')?;
dest.write_str(s.trim_start_matches("-").trim_start_matches("0"))?;
Ok(())
} else {
dest.write_str(s.trim_start_matches('0'))?;
Ok(())
}
} else {
cssparser::ToCss::to_css(self, dest)?;
Ok(())
}
}
}
impl std::convert::Into<Calc<CSSNumber>> for CSSNumber {
fn into(self) -> Calc<CSSNumber> {
Calc::Value(Box::new(self))
}
}
impl std::convert::From<Calc<CSSNumber>> for CSSNumber {
fn from(calc: Calc<CSSNumber>) -> CSSNumber {
match calc {
Calc::Value(v) => *v,
Calc::Number(n) => n,
_ => unreachable!(),
}
}
}
impl AddInternal for CSSNumber {
fn add(self, other: Self) -> Self {
self + other
}
}
impl Op for CSSNumber {
fn op<F: FnOnce(f32, f32) -> f32>(&self, to: &Self, op: F) -> Self {
op(*self, *to)
}
fn op_to<T, F: FnOnce(f32, f32) -> T>(&self, rhs: &Self, op: F) -> T {
op(*self, *rhs)
}
}
impl Map for CSSNumber {
fn map<F: FnOnce(f32) -> f32>(&self, op: F) -> Self {
op(*self)
}
}
impl Sign for CSSNumber {
fn sign(&self) -> f32 {
if *self == 0.0 {
return if f32::is_sign_positive(*self) { 0.0 } else { -0.0 };
}
self.signum()
}
}
impl Zero for CSSNumber {
fn zero() -> Self {
0.0
}
fn is_zero(&self) -> bool {
*self == 0.0
}
}
impl_try_from_angle!(CSSNumber);
pub type CSSInteger = i32;
impl<'i> Parse<'i> for CSSInteger {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
Self::parse_with_range(input, NumericRange::All)
}
}
impl<'i> ParseNumeric<'i> for CSSInteger {
fn parse_with_range<'t>(
input: &mut Parser<'i, 't>,
range: NumericRange,
) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let location = input.current_source_location();
let integer = input.expect_integer()?;
range.check(integer as f32, location)?;
Ok(integer)
}
}
impl ToCss for CSSInteger {
fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
cssparser::ToCss::to_css(self, dest)?;
Ok(())
}
}
impl Zero for CSSInteger {
fn zero() -> Self {
0
}
fn is_zero(&self) -> bool {
*self == 0
}
}