Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ permissions:
jobs:
codspeed:
name: ${{ matrix.package }} / ${{ matrix.bench }}
runs-on: ubuntu-latest
# The pinned CodSpeed simulation runner does not support Ubuntu 26.04.
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
Expand Down
58 changes: 29 additions & 29 deletions c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ impl<'i> CssError<'i> {
}

let string: String = match &self.kind {
ErrorKind::ParserError(err) => err.to_string().into(),
ErrorKind::MinifyError(err) => err.to_string().into(),
ErrorKind::PrinterError(err) => err.to_string().into(),
ErrorKind::PatternParseError(err) => err.to_string().into(),
ErrorKind::BrowserslistError(err) => err.to_string().into(),
ErrorKind::SourceMapError(err) => err.to_string().into(),
ErrorKind::ParserError(err) => err.to_string(),
ErrorKind::MinifyError(err) => err.to_string(),
ErrorKind::PrinterError(err) => err.to_string(),
ErrorKind::PatternParseError(err) => err.to_string(),
ErrorKind::BrowserslistError(err) => err.to_string(),
ErrorKind::SourceMapError(err) => err.to_string(),
};

self.message = Some(CString::new(string).unwrap());
Expand Down Expand Up @@ -98,8 +98,8 @@ pub struct Targets {
samsung: u32,
}

impl Into<Browsers> for Targets {
fn into(self) -> Browsers {
impl From<Targets> for Browsers {
fn from(val: Targets) -> Self {
macro_rules! browser {
($val: expr) => {
if $val > 0 {
Expand All @@ -111,15 +111,15 @@ impl Into<Browsers> for Targets {
}

Browsers {
android: browser!(self.android),
chrome: browser!(self.chrome),
edge: browser!(self.edge),
firefox: browser!(self.firefox),
ie: browser!(self.ie),
ios_saf: browser!(self.ios_saf),
opera: browser!(self.opera),
safari: browser!(self.safari),
samsung: browser!(self.samsung),
android: browser!(val.android),
chrome: browser!(val.chrome),
edge: browser!(val.edge),
firefox: browser!(val.firefox),
ie: browser!(val.ie),
ios_saf: browser!(val.ios_saf),
opera: browser!(val.opera),
safari: browser!(val.safari),
samsung: browser!(val.samsung),
}
}
}
Expand Down Expand Up @@ -172,18 +172,18 @@ pub struct TransformOptions {
unused_symbols_len: usize,
}

impl Into<MinifyOptions> for TransformOptions {
fn into(self) -> MinifyOptions {
impl From<TransformOptions> for MinifyOptions {
fn from(val: TransformOptions) -> Self {
let mut unused_symbols = HashSet::new();
let slice = unsafe { std::slice::from_raw_parts(self.unused_symbols, self.unused_symbols_len) };
let slice = unsafe { std::slice::from_raw_parts(val.unused_symbols, val.unused_symbols_len) };
for symbol in slice {
let string = unsafe { std::str::from_utf8_unchecked(CStr::from_ptr(*symbol).to_bytes()).to_owned() };
unused_symbols.insert(string);
}

MinifyOptions {
targets: if self.targets != Targets::default() {
Some(self.targets.into()).into()
targets: if val.targets != Targets::default() {
Some(val.targets.into()).into()
} else {
Default::default()
},
Expand Down Expand Up @@ -226,8 +226,8 @@ impl Default for PseudoClasses {
}
}

impl<'a> Into<lightningcss::printer::PseudoClasses<'a>> for PseudoClasses {
fn into(self) -> lightningcss::printer::PseudoClasses<'a> {
impl<'a> From<PseudoClasses> for lightningcss::printer::PseudoClasses<'a> {
fn from(val: PseudoClasses) -> Self {
macro_rules! pc {
($ptr: expr) => {
if $ptr.is_null() {
Expand All @@ -239,11 +239,11 @@ impl<'a> Into<lightningcss::printer::PseudoClasses<'a>> for PseudoClasses {
}

lightningcss::printer::PseudoClasses {
hover: pc!(self.hover),
active: pc!(self.active),
focus: pc!(self.focus),
focus_visible: pc!(self.focus_visible),
focus_within: pc!(self.focus_within),
hover: pc!(val.hover),
active: pc!(val.active),
focus: pc!(val.focus),
focus_visible: pc!(val.focus_visible),
focus_within: pc!(val.focus_within),
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions derive/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn derive_parse(input: TokenStream) -> TokenStream {
let (impl_generics, _, where_clause) = generics.split_for_impl();

let imp = match &data {
Data::Enum(data) => derive_enum(&data, &ident, &opts),
Data::Enum(data) => derive_enum(data, &ident, &opts),
_ => todo!(),
};

Expand Down Expand Up @@ -171,7 +171,7 @@ fn derive_enum(data: &DataEnum, ident: &Ident, opts: &CssOptions) -> TokenStream
#idents
};

output.into()
output
}

pub struct CssOptions {
Expand Down Expand Up @@ -201,10 +201,8 @@ impl Parse for CssOptions {
let _: Token![=] = input.parse()?;
let v: Ident = input.parse()?;

if k == "case" {
if v == "lower" {
case = convert_case::Case::Flat;
}
if k == "case" && v == "lower" {
case = convert_case::Case::Flat;
}
}

Expand Down
4 changes: 2 additions & 2 deletions derive/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn derive(
impl_generics.params.clear();
generic
} else {
&generics
generics
};

if impl_generics.lifetimes().next().is_none() {
Expand Down Expand Up @@ -249,7 +249,7 @@ fn derive(
output.into()
}

fn skip_type(attrs: &Vec<Attribute>) -> bool {
fn skip_type(attrs: &[Attribute]) -> bool {
attrs.iter().any(|attr| attr.path.is_ident("skip_type"))
}

Expand Down
53 changes: 22 additions & 31 deletions examples/custom_at_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<'i> AtRuleParser<'i> for TailwindAtRuleParser {
"tailwind" => {
let location = input.current_source_location();
let ident = input.expect_ident()?;
let directive = match_ignore_ascii_case! { &*ident,
let directive = match_ignore_ascii_case! { ident,
"base" => TailwindDirective::Base,
"components" => TailwindDirective::Components,
"utilities" => TailwindDirective::Utilities,
Expand Down Expand Up @@ -164,23 +164,17 @@ impl<'i, 'a> Visitor<'i, AtRule> for StyleRuleCollector<'i, 'a> {
}

fn visit_rule(&mut self, rule: &mut rspack_lightningcss::rules::CssRule<'i, AtRule>) -> Result<(), Self::Error> {
match rule {
CssRule::Style(rule) => {
for selector in rule.selectors.0.iter() {
if selector.len() != 1 {
continue; // TODO
}
for component in selector.iter_raw_match_order() {
match component {
Component::Class(name) => {
self.rules.insert(name.0.to_string(), rule.declarations.clone());
}
_ => {}
}
if let CssRule::Style(rule) = rule {
for selector in rule.selectors.0.iter() {
if selector.len() != 1 {
continue; // TODO
}
for component in selector.iter_raw_match_order() {
if let Component::Class(name) = component {
self.rules.insert(name.0.to_string(), rule.declarations.clone());
}
}
}
_ => {}
}

rule.visit_children(self)
Expand Down Expand Up @@ -241,9 +235,8 @@ impl<'a, 'i> Visitor<'i, AtRule> for ApplyVisitor<'a, 'i> {
&mut self,
length: &mut rspack_lightningcss::values::length::LengthValue,
) -> Result<(), Self::Error> {
match length {
LengthValue::Px(px) => *length = LengthValue::Rem(*px / 16.0),
_ => {}
if let LengthValue::Px(px) = length {
*length = LengthValue::Rem(*px / 16.0)
}

Ok(())
Expand All @@ -259,11 +252,8 @@ impl<'a, 'i> Visitor<'i, AtRule> for ApplyVisitor<'a, 'i> {

fn visit_selector(&mut self, selector: &mut Selector<'i>) -> Result<(), Self::Error> {
for c in selector.iter_mut_raw_match_order() {
match c {
Component::Class(c) => {
*c = format!("tw-{}", c).into();
}
_ => {}
if let Component::Class(c) = c {
*c = format!("tw-{}", c).into();
}
}

Expand All @@ -272,14 +262,15 @@ impl<'a, 'i> Visitor<'i, AtRule> for ApplyVisitor<'a, 'i> {

fn visit_token(&mut self, token: &mut TokenOrValue<'i>) -> Result<(), Self::Error> {
match token {
TokenOrValue::Function(f) if f.name == "theme" => match f.arguments.0.first() {
Some(TokenOrValue::Token(Token::String(s))) => match s.as_ref() {
"blue-500" => *token = TokenOrValue::Color(CssColor::RGBA(RGBA::new(0, 0, 255, 1.0))),
"red-500" => *token = TokenOrValue::Color(CssColor::RGBA(RGBA::new(255, 0, 0, 1.0))),
_ => {}
},
_ => {}
},
TokenOrValue::Function(f) if f.name == "theme" => {
if let Some(TokenOrValue::Token(Token::String(s))) = f.arguments.0.first() {
match s.as_ref() {
"blue-500" => *token = TokenOrValue::Color(CssColor::RGBA(RGBA::new(0, 0, 255, 1.0))),
"red-500" => *token = TokenOrValue::Color(CssColor::RGBA(RGBA::new(255, 0, 0, 1.0))),
_ => {}
}
}
}
_ => {}
}

Expand Down
26 changes: 13 additions & 13 deletions napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ mod bundle {
use super::*;
use crossbeam_channel::{self, Receiver, Sender};
use lightningcss::bundler::{FileProvider, ResolveResult};
use napi::{Env, JsBoolean, JsFunction, JsString, NapiRaw};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use napi::{Env, JsFunction, JsString, NapiRaw};
use std::path::Path;

use std::sync::Mutex;
use threadsafe_function::{ThreadSafeCallContext, ThreadsafeFunction, ThreadsafeFunctionCallMode};

Expand Down Expand Up @@ -649,14 +649,14 @@ struct OwnedPseudoClasses {
pub focus_within: Option<String>,
}

impl<'a> Into<PseudoClasses<'a>> for &'a OwnedPseudoClasses {
fn into(self) -> PseudoClasses<'a> {
impl<'a> From<&'a OwnedPseudoClasses> for PseudoClasses<'a> {
fn from(val: &'a OwnedPseudoClasses) -> Self {
PseudoClasses {
hover: self.hover.as_deref(),
active: self.active.as_deref(),
focus: self.focus.as_deref(),
focus_visible: self.focus_visible.as_deref(),
focus_within: self.focus_within.as_deref(),
hover: val.hover.as_deref(),
active: val.active.as_deref(),
focus: val.focus.as_deref(),
focus_visible: val.focus_visible.as_deref(),
focus_within: val.focus_within.as_deref(),
}
}
}
Expand Down Expand Up @@ -710,7 +710,7 @@ fn compile<'i>(
);

let mut stylesheet = StyleSheet::parse_with(
&code,
code,
ParserOptions {
filename: filename.clone(),
flags,
Expand Down Expand Up @@ -1000,7 +1000,7 @@ fn compile_attr<'i>(
let res = {
let filename = config.filename.clone().unwrap_or_default();
let mut attr = StyleAttribute::parse(
&code,
code,
ParserOptions {
filename,
error_recovery: config.error_recovery,
Expand Down Expand Up @@ -1070,7 +1070,7 @@ impl<'i, E: std::error::Error> std::fmt::Display for CompileError<'i, E> {
CompileError::PrinterError(err) => err.kind.fmt(f),
CompileError::BundleError(err) => err.kind.fmt(f),
CompileError::PatternError(err) => err.fmt(f),
CompileError::SourceMapError(err) => write!(f, "{}", err.to_string()), // TODO: switch to `fmt::Display` once parcel_sourcemap supports this
CompileError::SourceMapError(err) => write!(f, "{}", err), // TODO: switch to `fmt::Display` once parcel_sourcemap supports this
#[cfg(feature = "visitor")]
CompileError::JsError(err) => std::fmt::Debug::fmt(&err, f),
}
Expand Down
52 changes: 5 additions & 47 deletions napi/src/threadsafe_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,53 +39,11 @@ impl From<ThreadsafeFunctionCallMode> for sys::napi_threadsafe_function_call_mod

/// Communicate with the addon's main thread by invoking a JavaScript function from other threads.
///
/// ## Example
/// An example of using `ThreadsafeFunction`:
///
/// ```rust
/// #[macro_use]
/// extern crate napi_derive;
///
/// use std::thread;
///
/// use napi::{
/// threadsafe_function::{
/// ThreadSafeCallContext, ThreadsafeFunctionCallMode, ThreadsafeFunctionReleaseMode,
/// },
/// CallContext, Error, JsFunction, JsNumber, JsUndefined, Result, Status,
/// };
///
/// #[js_function(1)]
/// pub fn test_threadsafe_function(ctx: CallContext) -> Result<JsUndefined> {
/// let func = ctx.get::<JsFunction>(0)?;
///
/// let tsfn =
/// ctx
/// .env
/// .create_threadsafe_function(&func, 0, |ctx: ThreadSafeCallContext<Vec<u32>>| {
/// ctx.value
/// .iter()
/// .map(|v| ctx.env.create_uint32(*v))
/// .collect::<Result<Vec<JsNumber>>>()
/// })?;
///
/// let tsfn_cloned = tsfn.clone();
///
/// thread::spawn(move || {
/// let output: Vec<u32> = vec![0, 1, 2, 3];
/// // It's okay to call a threadsafe function multiple times.
/// tsfn.call(Ok(output.clone()), ThreadsafeFunctionCallMode::Blocking);
/// });
///
/// thread::spawn(move || {
/// let output: Vec<u32> = vec![3, 2, 1, 0];
/// // It's okay to call a threadsafe function multiple times.
/// tsfn_cloned.call(Ok(output.clone()), ThreadsafeFunctionCallMode::NonBlocking);
/// });
///
/// ctx.env.get_undefined()
/// }
/// ```
/// This internal helper is created with a raw Node environment and function,
/// a queue size, and a callback that returns `Result<()>`. The callback runs on
/// the main thread with a `ThreadSafeCallContext<T>`, and handles JavaScript
/// conversion itself. Worker threads can clone the handle and pass a value of
/// type `T` directly to `call` with the desired blocking mode.
pub struct ThreadsafeFunction<T: 'static> {
raw_tsfn: sys::napi_threadsafe_function,
aborted: Arc<AtomicBool>,
Expand Down
5 changes: 2 additions & 3 deletions napi/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,8 @@ fn call_visitor<V: Serialize + Deserialize<'static>>(
let js_value = env.to_js_value(value)?;
let res = visit.call(None, &[js_value])?;
let new_value: Option<V> = env.from_js_value(res).map(serde_detach::detach)?;
match new_value {
Some(new_value) => *value = new_value,
None => {}
if let Some(new_value) = new_value {
*value = new_value
}

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ edition = "2021"
publish = false

[lib]
# The addon links against Node.js at runtime and has no Rust unit tests.
test = false
crate-type = ["cdylib"]

[dependencies]
Expand Down
Loading
Loading