use crate::context::PropertyHandlerContext;
use crate::declaration::{DeclarationBlock, DeclarationList};
use crate::error::{ParserError, PrinterError};
use crate::printer::Printer;
use crate::properties::{Property, PropertyId};
use crate::stylesheet::{ParserOptions, PrinterOptions};
use crate::targets::{Browsers, Targets};
use crate::vendor_prefix::VendorPrefix;
use cssparser::*;
#[cfg(feature = "into_owned")]
pub use static_self::IntoOwned;
pub trait Parse<'i>: Sized {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>>;
fn parse_string(input: &'i str) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let mut input = ParserInput::new(input);
let mut parser = Parser::new(&mut input);
let result = Self::parse(&mut parser)?;
parser.expect_exhausted()?;
Ok(result)
}
}
pub(crate) use lightningcss_derive::Parse;
impl<'i, T: Parse<'i>> Parse<'i> for Option<T> {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
Ok(input.try_parse(T::parse).ok())
}
}
impl<'i, T: Parse<'i>> Parse<'i> for Box<T> {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
Ok(Box::new(T::parse(input)?))
}
}
pub trait ParseWithOptions<'i>: Sized {
fn parse_with_options<'t>(
input: &mut Parser<'i, 't>,
options: &ParserOptions<'_, 'i>,
) -> Result<Self, ParseError<'i, ParserError<'i>>>;
fn parse_string_with_options(
input: &'i str,
options: ParserOptions<'_, 'i>,
) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let mut input = ParserInput::new(input);
let mut parser = Parser::new(&mut input);
Self::parse_with_options(&mut parser, &options)
}
}
impl<'i, T: Parse<'i>> ParseWithOptions<'i> for T {
#[inline]
fn parse_with_options<'t>(
input: &mut Parser<'i, 't>,
_options: &ParserOptions,
) -> Result<Self, ParseError<'i, ParserError<'i>>> {
T::parse(input)
}
}
pub trait ToCss {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write;
#[inline]
fn to_css_string(&self, options: PrinterOptions) -> Result<String, PrinterError> {
let mut s = String::new();
let mut printer = Printer::new(&mut s, options);
self.to_css(&mut printer)?;
Ok(s)
}
}
pub(crate) use lightningcss_derive::ToCss;
impl<'a, T> ToCss for &'a T
where
T: ToCss + ?Sized,
{
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
(*self).to_css(dest)
}
}
impl<T: ToCss> ToCss for Box<T> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
(**self).to_css(dest)
}
}
impl<T: ToCss> ToCss for Option<T> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
if let Some(v) = self {
v.to_css(dest)?;
}
Ok(())
}
}
pub(crate) trait PropertyHandler<'i>: Sized {
fn handle_property(
&mut self,
property: &Property<'i>,
dest: &mut DeclarationList<'i>,
context: &mut PropertyHandlerContext<'i, '_>,
) -> bool;
fn finalize(&mut self, dest: &mut DeclarationList<'i>, context: &mut PropertyHandlerContext<'i, '_>);
}
pub(crate) mod private {
pub trait TryAdd<T> {
fn try_add(&self, other: &T) -> Option<T>;
}
pub trait AddInternal {
fn add(self, other: Self) -> Self;
}
}
pub(crate) trait FromStandard<T>: Sized {
fn from_standard(val: &T) -> Option<Self>;
}
pub(crate) trait FallbackValues: Sized {
fn get_fallbacks(&mut self, targets: Targets) -> Vec<Self>;
}
pub(crate) trait Shorthand<'i>: Sized {
fn from_longhands(decls: &DeclarationBlock<'i>, vendor_prefix: VendorPrefix) -> Option<(Self, bool)>;
fn longhands(vendor_prefix: VendorPrefix) -> Vec<PropertyId<'static>>;
fn longhand(&self, property_id: &PropertyId) -> Option<Property<'i>>;
fn set_longhand(&mut self, property: &Property<'i>) -> Result<(), ()>;
}
pub trait Op {
fn op<F: FnOnce(f32, f32) -> f32>(&self, rhs: &Self, op: F) -> Self;
fn op_to<T, F: FnOnce(f32, f32) -> T>(&self, rhs: &Self, op: F) -> T;
}
macro_rules! impl_op {
($t: ty, $trait: ident $(:: $x: ident)*, $op: ident) => {
impl $trait$(::$x)* for $t {
type Output = $t;
fn $op(self, rhs: Self) -> Self::Output {
self.op(&rhs, $trait$(::$x)*::$op)
}
}
};
}
pub(crate) use impl_op;
use smallvec::SmallVec;
pub trait TryOp: Sized {
fn try_op<F: FnOnce(f32, f32) -> f32>(&self, rhs: &Self, op: F) -> Option<Self>;
fn try_op_to<T, F: FnOnce(f32, f32) -> T>(&self, rhs: &Self, op: F) -> Option<T>;
}
impl<T: Op> TryOp for T {
fn try_op<F: FnOnce(f32, f32) -> f32>(&self, rhs: &Self, op: F) -> Option<Self> {
Some(self.op(rhs, op))
}
fn try_op_to<U, F: FnOnce(f32, f32) -> U>(&self, rhs: &Self, op: F) -> Option<U> {
Some(self.op_to(rhs, op))
}
}
pub trait Map {
fn map<F: FnOnce(f32) -> f32>(&self, op: F) -> Self;
}
pub trait TryMap: Sized {
fn try_map<F: FnOnce(f32) -> f32>(&self, op: F) -> Option<Self>;
}
impl<T: Map> TryMap for T {
fn try_map<F: FnOnce(f32) -> f32>(&self, op: F) -> Option<Self> {
Some(self.map(op))
}
}
pub trait Sign {
fn sign(&self) -> f32;
fn is_sign_positive(&self) -> bool {
f32::is_sign_positive(self.sign())
}
fn is_sign_negative(&self) -> bool {
f32::is_sign_negative(self.sign())
}
}
pub trait TrySign {
fn try_sign(&self) -> Option<f32>;
fn is_sign_positive(&self) -> bool {
self.try_sign().map_or(false, |s| f32::is_sign_positive(s))
}
fn is_sign_negative(&self) -> bool {
self.try_sign().map_or(false, |s| f32::is_sign_negative(s))
}
}
impl<T: Sign> TrySign for T {
fn try_sign(&self) -> Option<f32> {
Some(self.sign())
}
}
pub trait Zero {
fn zero() -> Self;
fn is_zero(&self) -> bool;
}
pub trait IsCompatible {
fn is_compatible(&self, browsers: Browsers) -> bool;
}
impl<T: IsCompatible> IsCompatible for SmallVec<[T; 1]> {
fn is_compatible(&self, browsers: Browsers) -> bool {
self.iter().all(|v| v.is_compatible(browsers))
}
}
impl<T: IsCompatible> IsCompatible for Vec<T> {
fn is_compatible(&self, browsers: Browsers) -> bool {
self.iter().all(|v| v.is_compatible(browsers))
}
}
pub trait AtRuleParser<'i>: Sized {
type Prelude;
type AtRule;
type Error: 'i;
fn parse_prelude<'t>(
&mut self,
name: CowRcStr<'i>,
input: &mut Parser<'i, 't>,
options: &ParserOptions<'_, 'i>,
) -> Result<Self::Prelude, ParseError<'i, Self::Error>> {
let _ = name;
let _ = input;
let _ = options;
Err(input.new_error(BasicParseErrorKind::AtRuleInvalid(name)))
}
fn rule_without_block(
&mut self,
prelude: Self::Prelude,
start: &ParserState,
options: &ParserOptions<'_, 'i>,
is_nested: bool,
) -> Result<Self::AtRule, ()> {
let _ = prelude;
let _ = start;
let _ = options;
let _ = is_nested;
Err(())
}
fn parse_block<'t>(
&mut self,
prelude: Self::Prelude,
start: &ParserState,
input: &mut Parser<'i, 't>,
options: &ParserOptions<'_, 'i>,
is_nested: bool,
) -> Result<Self::AtRule, ParseError<'i, Self::Error>> {
let _ = prelude;
let _ = start;
let _ = input;
let _ = options;
let _ = is_nested;
Err(input.new_error(BasicParseErrorKind::AtRuleBodyInvalid))
}
}