Skip to content

Commit

Permalink
feat: Implement IntoOwned for more types (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Oct 12, 2023
1 parent 9b3e6f4 commit 1795ac9
Show file tree
Hide file tree
Showing 62 changed files with 557 additions and 192 deletions.
23 changes: 21 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ nodejs = ["dep:serde"]
serde = ["dep:serde", "smallvec/serde", "cssparser/serde", "parcel_selectors/serde", "into_owned"]
sourcemap = ["parcel_sourcemap"]
visitor = ["lightningcss-derive"]
into_owned = ["lightningcss-derive"]
into_owned = ["static-self", "static-self/smallvec", "parcel_selectors/into_owned"]
substitute_variables = ["visitor", "into_owned"]

[dependencies]
Expand All @@ -68,6 +68,7 @@ dashmap = { version = "5.0.0", optional = true }
serde_json = { version = "1.0.78", optional = true }
lightningcss-derive = { version = "=1.0.0-alpha.41", path = "./derive", optional = true }
schemars = { version = "0.8.11", features = ["smallvec"], optional = true }
static-self = { version = "0.1.0", path = "static-self", optional = true }

[target.'cfg(target_os = "macos")'.dependencies]
jemallocator = { version = "0.3.2", features = ["disable_initial_exec_tls"], optional = true }
Expand Down
7 changes: 0 additions & 7 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ use syn::{
GenericParam, Generics, Ident, Member, Token, Type, Visibility,
};

mod into_owned;

#[proc_macro_derive(IntoOwned)]
pub fn derive_into_owned(input: TokenStream) -> TokenStream {
into_owned::derive_into_owned(input)
}

#[proc_macro_derive(Visit, attributes(visit, skip_visit, skip_type, visit_types))]
pub fn derive_visit_children(input: TokenStream) -> TokenStream {
let DeriveInput {
Expand Down
4 changes: 4 additions & 0 deletions selectors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ path = "lib.rs"
[features]
bench = []
jsonschema = ["serde", "schemars"]
into_owned = ["static-self"]
smallvec = ["static-self/smallvec"]
serde = ["dep:serde", "smallvec/serde"]

[dependencies]
bitflags = "2.2.1"
Expand All @@ -28,6 +31,7 @@ precomputed-hash = "0.1"
smallvec = "1.0"
serde = { version = "1.0.123", features = ["derive"], optional = true }
schemars = { version = "0.8.11", features = ["smallvec"], optional = true }
static-self = { version = "0.1.0", path = "../static-self", optional = true }

[build-dependencies]
phf_codegen = "0.10"
26 changes: 26 additions & 0 deletions selectors/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ pub struct AttrSelectorWithOptionalNamespace<'i, Impl: SelectorImpl<'i>> {
pub never_matches: bool,
}

#[cfg(feature = "into_owned")]
impl<'any, 'i, Impl: SelectorImpl<'i>, NewSel> static_self::IntoOwned<'any>
for AttrSelectorWithOptionalNamespace<'i, Impl>
where
Impl: static_self::IntoOwned<'any, Owned = NewSel>,
NewSel: SelectorImpl<'any>,
Impl::LocalName: static_self::IntoOwned<'any, Owned = NewSel::LocalName>,
Impl::NamespacePrefix: static_self::IntoOwned<'any, Owned = NewSel::NamespacePrefix>,
Impl::NamespaceUrl: static_self::IntoOwned<'any, Owned = NewSel::NamespaceUrl>,
Impl::AttrValue: static_self::IntoOwned<'any, Owned = NewSel::AttrValue>,
{
type Owned = AttrSelectorWithOptionalNamespace<'any, NewSel>;

fn into_owned(self) -> Self::Owned {
AttrSelectorWithOptionalNamespace {
namespace: self.namespace.into_owned(),
local_name: self.local_name.into_owned(),
local_name_lower: self.local_name_lower.into_owned(),
operation: self.operation.into_owned(),
never_matches: self.never_matches,
}
}
}

impl<'i, Impl: SelectorImpl<'i>> AttrSelectorWithOptionalNamespace<'i, Impl> {
pub fn namespace(&self) -> Option<NamespaceConstraint<&Impl::NamespaceUrl>> {
self.namespace.as_ref().map(|ns| match ns {
Expand All @@ -35,6 +59,7 @@ impl<'i, Impl: SelectorImpl<'i>> AttrSelectorWithOptionalNamespace<'i, Impl> {
derive(schemars::JsonSchema),
schemars(rename = "NamespaceConstraint")
)]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
pub enum NamespaceConstraint<NamespaceUrl> {
Any,

Expand All @@ -43,6 +68,7 @@ pub enum NamespaceConstraint<NamespaceUrl> {
}

#[derive(Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
pub enum ParsedAttrSelectorOperation<AttrValue> {
Exists,
WithValue {
Expand Down
2 changes: 2 additions & 0 deletions selectors/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ extern crate schemars;
#[cfg(feature = "serde")]
extern crate serde;
extern crate smallvec;
#[cfg(feature = "into_owned")]
extern crate static_self;

pub mod attr;
pub mod bloom;
Expand Down
133 changes: 133 additions & 0 deletions selectors/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,20 @@ pub struct SelectorList<'i, Impl: SelectorImpl<'i>>(
#[cfg_attr(feature = "serde", serde(borrow))] pub SmallVec<[Selector<'i, Impl>; 1]>,
);

#[cfg(feature = "into_owned")]
impl<'any, 'i, Impl: SelectorImpl<'i>, NewSel> static_self::IntoOwned<'any> for SelectorList<'i, Impl>
where
Impl: static_self::IntoOwned<'any, Owned = NewSel>,
NewSel: SelectorImpl<'any>,
Component<'i, Impl>: static_self::IntoOwned<'any, Owned = Component<'any, NewSel>>,
{
type Owned = SelectorList<'any, NewSel>;

fn into_owned(self) -> Self::Owned {
SelectorList(self.0.into_owned())
}
}

/// How to treat invalid selectors in a selector list.
pub enum ParseErrorRecovery {
/// Discard the entire selector list, this is the default behavior for
Expand Down Expand Up @@ -696,6 +710,20 @@ pub fn namespace_empty_string<'i, Impl: SelectorImpl<'i>>() -> Impl::NamespaceUr
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Selector<'i, Impl: SelectorImpl<'i>>(SpecificityAndFlags, Vec<Component<'i, Impl>>);

#[cfg(feature = "into_owned")]
impl<'any, 'i, Impl: SelectorImpl<'i>, NewSel> static_self::IntoOwned<'any> for Selector<'i, Impl>
where
Impl: static_self::IntoOwned<'any, Owned = NewSel>,
NewSel: SelectorImpl<'any>,
Component<'i, Impl>: static_self::IntoOwned<'any, Owned = Component<'any, NewSel>>,
{
type Owned = Selector<'any, NewSel>;

fn into_owned(self) -> Self::Owned {
Selector(self.0, self.1.into_owned())
}
}

impl<'i, Impl: SelectorImpl<'i>> Selector<'i, Impl> {
#[inline]
pub fn specificity(&self) -> u32 {
Expand Down Expand Up @@ -1117,6 +1145,7 @@ impl<'a, 'i, Impl: SelectorImpl<'i>> Iterator for AncestorIter<'a, 'i, Impl> {
serde(rename_all = "kebab-case")
)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
pub enum Combinator {
Child, // >
Descendant, // space
Expand Down Expand Up @@ -1179,6 +1208,7 @@ impl Combinator {

/// An enum for the different types of :nth- pseudoclasses
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
pub enum NthType {
Child,
LastChild,
Expand Down Expand Up @@ -1212,6 +1242,7 @@ impl NthType {
/// nth-child(An+B)).
/// https://www.w3.org/TR/selectors-3/#nth-child-pseudo
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
pub struct NthSelectorData {
pub ty: NthType,
pub is_function: bool,
Expand Down Expand Up @@ -1312,6 +1343,20 @@ impl NthSelectorData {
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct NthOfSelectorData<'i, Impl: SelectorImpl<'i>>(NthSelectorData, Box<[Selector<'i, Impl>]>);

#[cfg(feature = "into_owned")]
impl<'any, 'i, Impl: SelectorImpl<'i>, NewSel> static_self::IntoOwned<'any> for NthOfSelectorData<'i, Impl>
where
Impl: static_self::IntoOwned<'any, Owned = NewSel>,
NewSel: SelectorImpl<'any>,
Component<'i, Impl>: static_self::IntoOwned<'any, Owned = Component<'any, NewSel>>,
{
type Owned = NthOfSelectorData<'any, NewSel>;

fn into_owned(self) -> Self::Owned {
NthOfSelectorData(self.0, self.1.into_owned())
}
}

impl<'i, Impl: SelectorImpl<'i>> NthOfSelectorData<'i, Impl> {
/// Returns selector data for :nth-{,last-}{child,of-type}(An+B [of S])
#[inline]
Expand Down Expand Up @@ -1431,6 +1476,77 @@ pub enum Component<'i, Impl: SelectorImpl<'i>> {
Nesting,
}

#[cfg(feature = "into_owned")]
impl<'any, 'i, Impl: SelectorImpl<'i>, NewSel> static_self::IntoOwned<'any> for Component<'i, Impl>
where
Impl: static_self::IntoOwned<'any, Owned = NewSel>,
NewSel: SelectorImpl<'any>,
Impl::NamespaceUrl: static_self::IntoOwned<'any, Owned = NewSel::NamespaceUrl>,
Impl::NamespacePrefix: static_self::IntoOwned<'any, Owned = NewSel::NamespacePrefix>,
Impl::Identifier: static_self::IntoOwned<'any, Owned = NewSel::Identifier>,
Impl::LocalName: static_self::IntoOwned<'any, Owned = NewSel::LocalName>,
Impl::AttrValue: static_self::IntoOwned<'any, Owned = NewSel::AttrValue>,
Impl::NonTSPseudoClass: static_self::IntoOwned<'any, Owned = NewSel::NonTSPseudoClass>,
Impl::PseudoElement: static_self::IntoOwned<'any, Owned = NewSel::PseudoElement>,
Impl::VendorPrefix: static_self::IntoOwned<'any, Owned = NewSel::VendorPrefix>,
{
type Owned = Component<'any, NewSel>;

fn into_owned(self) -> Self::Owned {
match self {
Component::Combinator(c) => Component::Combinator(c.into_owned()),
Component::ExplicitAnyNamespace => Component::ExplicitAnyNamespace,
Component::ExplicitNoNamespace => Component::ExplicitNoNamespace,
Component::DefaultNamespace(c) => Component::DefaultNamespace(c.into_owned()),
Component::Namespace(a, b) => Component::Namespace(a.into_owned(), b.into_owned()),
Component::ExplicitUniversalType => Component::ExplicitUniversalType,
Component::LocalName(c) => Component::LocalName(c.into_owned()),
Component::ID(c) => Component::ID(c.into_owned()),
Component::Class(c) => Component::Class(c.into_owned()),
Component::AttributeInNoNamespaceExists {
local_name,
local_name_lower,
} => Component::AttributeInNoNamespaceExists {
local_name: local_name.into_owned(),
local_name_lower: local_name_lower.into_owned(),
},
Component::AttributeInNoNamespace {
local_name,
operator,
value,
case_sensitivity,
never_matches,
} => {
let value = value.into_owned();
Component::AttributeInNoNamespace {
local_name: local_name.into_owned(),
operator,
value,
case_sensitivity,
never_matches,
}
}
Component::AttributeOther(c) => Component::AttributeOther(c.into_owned()),
Component::Negation(c) => Component::Negation(c.into_owned()),
Component::Root => Component::Root,
Component::Empty => Component::Empty,
Component::Scope => Component::Scope,
Component::Nth(c) => Component::Nth(c.into_owned()),
Component::NthOf(c) => Component::NthOf(c.into_owned()),
Component::NonTSPseudoClass(c) => Component::NonTSPseudoClass(c.into_owned()),
Component::Slotted(c) => Component::Slotted(c.into_owned()),
Component::Part(c) => Component::Part(c.into_owned()),
Component::Host(c) => Component::Host(c.into_owned()),
Component::Where(c) => Component::Where(c.into_owned()),
Component::Is(c) => Component::Is(c.into_owned()),
Component::Any(a, b) => Component::Any(a.into_owned(), b.into_owned()),
Component::Has(c) => Component::Has(c.into_owned()),
Component::PseudoElement(c) => Component::PseudoElement(c.into_owned()),
Component::Nesting => Component::Nesting,
}
}
}

impl<'i, Impl: SelectorImpl<'i>> Component<'i, Impl> {
/// Returns true if this is a combinator.
pub fn is_combinator(&self) -> bool {
Expand Down Expand Up @@ -1578,6 +1694,23 @@ pub struct LocalName<'i, Impl: SelectorImpl<'i>> {
pub lower_name: Impl::LocalName,
}

#[cfg(feature = "into_owned")]
impl<'any, 'i, Impl: SelectorImpl<'i>, NewSel> static_self::IntoOwned<'any> for LocalName<'i, Impl>
where
Impl: static_self::IntoOwned<'any, Owned = NewSel>,
NewSel: SelectorImpl<'any>,
Impl::LocalName: static_self::IntoOwned<'any, Owned = NewSel::LocalName>,
{
type Owned = LocalName<'any, NewSel>;

fn into_owned(self) -> Self::Owned {
LocalName {
name: self.name.into_owned(),
lower_name: self.lower_name.into_owned(),
}
}
}

impl<'i, Impl: SelectorImpl<'i>> Debug for Selector<'i, Impl> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("Selector(")?;
Expand Down
2 changes: 1 addition & 1 deletion src/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use cssparser::*;
/// with storing a boolean along with each property.
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(feature = "visitor", derive(Visit), visit(visit_declaration_block, PROPERTIES))]
#[cfg_attr(feature = "into_owned", derive(lightningcss_derive::IntoOwned))]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::properties::custom::Token;
use crate::rules::Location;
#[cfg(feature = "into_owned")]
use crate::traits::IntoOwned;
use static_self::IntoOwned;
use crate::values::string::CowArcStr;
use cssparser::{BasicParseErrorKind, ParseError, ParseErrorKind};
use parcel_selectors::parser::SelectorParseErrorKind;
Expand Down Expand Up @@ -68,7 +68,7 @@ impl fmt::Display for ErrorLocation {

/// A parser error.
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "into_owned", derive(lightningcss_derive::IntoOwned))]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
#[cfg_attr(any(feature = "serde", feature = "nodejs"), derive(Serialize))]
#[cfg_attr(any(feature = "serde", feature = "nodejs"), serde(tag = "type", content = "value"))]
pub enum ParserError<'i> {
Expand Down Expand Up @@ -186,7 +186,7 @@ impl<'i> ParserError<'i> {

/// A selector parsing error.
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "into_owned", derive(lightningcss_derive::IntoOwned))]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
#[cfg_attr(any(feature = "serde", feature = "nodejs"), derive(Serialize))]
#[cfg_attr(any(feature = "serde", feature = "nodejs"), serde(tag = "type", content = "value"))]
pub enum SelectorError<'i> {
Expand Down

0 comments on commit 1795ac9

Please sign in to comment.