Skip to content

Commit

Permalink
Put all of serde behind feature flag
Browse files Browse the repository at this point in the history
This commit makes the "serde" dependency completely optional.

Related: parcel-bundler#357
  • Loading branch information
chinedufn committed Dec 20, 2022
1 parent 787f46f commit a4b1ba5
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 43 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ browserslist = ["browserslist-rs"]
bundler = ["dashmap", "rayon"]
cli = ["clap", "serde_json", "browserslist", "jemallocator"]
grid = []
serde = ["smallvec/serde", "cssparser/serde"]
with-serde = ["serde", "smallvec/serde", "cssparser/serde"]

[dependencies]
serde = { version = "1.0.123", features = ["derive"] }
serde = { version = "1.0.123", features = ["derive"], optional = true }
cssparser = "0.29.1"
parcel_selectors = { version = "0.24.9", path = "./selectors" }
itertools = "0.10.1"
Expand Down
2 changes: 1 addition & 1 deletion selectors/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2709,7 +2709,7 @@ pub mod tests {
use super::*;
use crate::builder::SelectorFlags;
use crate::parser;
use cssparser::{serialize_identifier, Parser as CssParser, ParserInput, ToCss, serialize_string};
use cssparser::{serialize_identifier, serialize_string, Parser as CssParser, ParserInput, ToCss};
use std::collections::HashMap;
use std::fmt;

Expand Down
6 changes: 3 additions & 3 deletions src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ use cssparser::AtRuleParser;
use dashmap::DashMap;
use parcel_sourcemap::SourceMap;
use rayon::prelude::*;
use serde::Serialize;
use std::{
collections::HashSet,
fs,
Expand Down Expand Up @@ -144,7 +143,8 @@ impl Drop for FileProvider {
}

/// An error that could occur during bundling.
#[derive(Debug, Serialize)]
#[derive(Debug)]
#[cfg_attr(feature = "serde", serde::Serialize)]
pub enum BundleErrorKind<'i, T: std::error::Error> {
/// A parser error occurred.
ParserError(ParserError<'i>),
Expand All @@ -155,7 +155,7 @@ pub enum BundleErrorKind<'i, T: std::error::Error> {
/// Unsupported media query boolean logic was encountered.
UnsupportedMediaBooleanLogic,
/// A custom resolver error.
ResolverError(#[serde(skip)] T),
ResolverError(#[cfg_attr(feature = "serde", serde(skip))] T),
}

impl<'i, T: std::error::Error> From<Error<ParserError<'i>>> for Error<BundleErrorKind<'i, T>> {
Expand Down
11 changes: 7 additions & 4 deletions src/css_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::selector::SelectorList;
use data_encoding::{Encoding, Specification};
use lazy_static::lazy_static;
use pathdiff::diff_paths;
#[cfg(feature = "serde")]
use serde::Serialize;
use smallvec::{smallvec, SmallVec};
use std::borrow::Cow;
Expand Down Expand Up @@ -164,8 +165,9 @@ pub enum Segment<'i> {
/// A referenced name within a CSS module, e.g. via the `composes` property.
///
/// See [CssModuleExport](CssModuleExport).
#[derive(PartialEq, Debug, Clone, Serialize)]
#[serde(tag = "type", rename_all = "lowercase")]
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename_all = "lowercase"))]
pub enum CssModuleReference {
/// A local reference.
Local {
Expand All @@ -187,8 +189,9 @@ pub enum CssModuleReference {
}

/// An exported value from a CSS module.
#[derive(PartialEq, Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct CssModuleExport {
/// The local (compiled) name for this export.
pub name: String,
Expand Down
21 changes: 13 additions & 8 deletions src/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::traits::ToCss;
use crate::values::url::Url;
use crate::visitor::Visit;
use cssparser::SourceLocation;
#[cfg(feature = "serde")]
use serde::Serialize;

/// Options for `analyze_dependencies` in `PrinterOptions`.
Expand All @@ -25,8 +26,9 @@ pub struct DependencyOptions {
}

/// A dependency.
#[derive(Serialize, Debug)]
#[serde(tag = "type", rename_all = "lowercase")]
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", rename_all = "lowercase"))]
pub enum Dependency {
/// An `@import` dependency.
Import(ImportDependency),
Expand All @@ -35,7 +37,8 @@ pub enum Dependency {
}

/// An `@import` dependency.
#[derive(Serialize, Debug)]
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct ImportDependency {
/// The url to import.
pub url: String,
Expand Down Expand Up @@ -87,7 +90,8 @@ impl ImportDependency {
}

/// A `url()` dependency.
#[derive(Serialize, Debug)]
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct UrlDependency {
/// The url of the dependency.
pub url: String,
Expand All @@ -110,8 +114,9 @@ impl UrlDependency {
}

/// Represents the range of source code where a dependency was found.
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct SourceRange {
/// The filename in which the dependency was found.
pub file_path: String,
Expand All @@ -122,8 +127,8 @@ pub struct SourceRange {
}

/// A line and column position within a source file.
#[derive(Serialize, Debug, Clone, Copy, PartialEq, Visit)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Visit)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Location {
/// The line number, starting from 1.
pub line: u32,
Expand Down
47 changes: 26 additions & 21 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use crate::rules::Location;
use crate::values::string::CowArcStr;
use cssparser::{BasicParseErrorKind, ParseError, ParseErrorKind};
use parcel_selectors::parser::SelectorParseErrorKind;
#[cfg(feature = "serde")]
use serde::Serialize;
use std::fmt;

/// An error with a source location.
#[derive(Debug, PartialEq, Clone, Serialize)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Error<T> {
/// The type of error that occurred.
pub kind: T,
Expand All @@ -31,8 +32,8 @@ impl<T: fmt::Display> fmt::Display for Error<T> {
impl<T: fmt::Display + fmt::Debug> std::error::Error for Error<T> {}

/// A line and column location within a source file.
#[derive(Debug, PartialEq, Clone, Serialize)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ErrorLocation {
/// The filename in which the error occurred.
pub filename: String,
Expand Down Expand Up @@ -60,8 +61,9 @@ impl fmt::Display for ErrorLocation {
}

/// A parser error.
#[derive(Debug, PartialEq, Serialize, Clone)]
#[serde(tag = "type", content = "value")]
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", content = "value"))]
pub enum ParserError<'i> {
/// An at rule body was invalid.
AtRuleBodyInvalid,
Expand Down Expand Up @@ -90,7 +92,7 @@ pub enum ParserError<'i> {
/// A `@namespace` rule was encountered after any rules besides `@charset`, `@import`, or `@layer`.
UnexpectedNamespaceRule,
/// An unexpected token was encountered.
UnexpectedToken(#[serde(skip)] Token<'i>),
UnexpectedToken(#[cfg_attr(feature = "serde", serde(skip))] Token<'i>),
/// Maximum nesting depth was reached.
MaximumNestingDepth,
}
Expand Down Expand Up @@ -164,45 +166,46 @@ impl<'i> ParserError<'i> {
}

/// A selector parsing error.
#[derive(Debug, PartialEq, Serialize, Clone)]
#[serde(tag = "type", content = "value")]
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "type", content = "value"))]
pub enum SelectorError<'i> {
/// An unexpected token was found in an attribute selector.
BadValueInAttr(#[serde(skip)] Token<'i>),
BadValueInAttr(#[cfg_attr(feature = "serde", serde(skip))] Token<'i>),
/// An unexpected token was found in a class selector.
ClassNeedsIdent(#[serde(skip)] Token<'i>),
ClassNeedsIdent(#[cfg_attr(feature = "serde", serde(skip))] Token<'i>),
/// A dangling combinator was found.
DanglingCombinator,
/// An empty selector.
EmptySelector,
/// A `|` was expected in an attribute selector.
ExpectedBarInAttr(#[serde(skip)] Token<'i>),
ExpectedBarInAttr(#[cfg_attr(feature = "serde", serde(skip))] Token<'i>),
/// A namespace was expected.
ExpectedNamespace(CowArcStr<'i>),
/// An unexpected token was encountered in a namespace.
ExplicitNamespaceUnexpectedToken(#[serde(skip)] Token<'i>),
ExplicitNamespaceUnexpectedToken(#[cfg_attr(feature = "serde", serde(skip))] Token<'i>),
/// An invalid pseudo class was encountered after a pseudo element.
InvalidPseudoClassAfterPseudoElement,
/// An invalid pseudo class was encountered after a `-webkit-scrollbar` pseudo element.
InvalidPseudoClassAfterWebKitScrollbar,
/// A `-webkit-scrollbar` state was encountered before a `-webkit-scrollbar` pseudo element.
InvalidPseudoClassBeforeWebKitScrollbar,
/// Invalid qualified name in attribute selector.
InvalidQualNameInAttr(#[serde(skip)] Token<'i>),
InvalidQualNameInAttr(#[cfg_attr(feature = "serde", serde(skip))] Token<'i>),
/// The current token is not allowed in this state.
InvalidState,
/// The selector is required to have the `&` nesting selector at the start.
MissingNestingPrefix,
/// The selector is missing a `&` nesting selector.
MissingNestingSelector,
/// No qualified name in attribute selector.
NoQualifiedNameInAttributeSelector(#[serde(skip)] Token<'i>),
NoQualifiedNameInAttributeSelector(#[cfg_attr(feature = "serde", serde(skip))] Token<'i>),
/// An Invalid token was encountered in a pseudo element.
PseudoElementExpectedIdent(#[serde(skip)] Token<'i>),
PseudoElementExpectedIdent(#[cfg_attr(feature = "serde", serde(skip))] Token<'i>),
/// An unexpected identifier was encountered.
UnexpectedIdent(CowArcStr<'i>),
/// An unexpected token was encountered inside an attribute selector.
UnexpectedTokenInAttributeSelector(#[serde(skip)] Token<'i>),
UnexpectedTokenInAttributeSelector(#[cfg_attr(feature = "serde", serde(skip))] Token<'i>),
/// An unsupported pseudo class or pseudo element was encountered.
UnsupportedPseudoClassOrElement(CowArcStr<'i>),
}
Expand Down Expand Up @@ -291,8 +294,9 @@ impl<T: fmt::Display + fmt::Debug> std::error::Error for ErrorWithLocation<T> {}
pub(crate) type MinifyError = ErrorWithLocation<MinifyErrorKind>;

/// A transformation error.
#[derive(Debug, PartialEq, Serialize)]
#[serde(tag = "type")]
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "type"))]
pub enum MinifyErrorKind {
/// A circular `@custom-media` rule was detected.
CircularCustomMedia {
Expand Down Expand Up @@ -337,8 +341,9 @@ impl MinifyErrorKind {
pub type PrinterError = Error<PrinterErrorKind>;

/// A printer error type.
#[derive(Debug, PartialEq, Serialize)]
#[serde(tag = "type")]
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", Serialize)]
#[cfg_attr(feature = "serde", serde(tag = "type"))]
pub enum PrinterErrorKind {
/// An ambiguous relative `url()` was encountered in a custom property declaration.
AmbiguousUrlInCustomProperty {
Expand Down
5 changes: 3 additions & 2 deletions src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ use media::MediaRule;
use namespace::NamespaceRule;
use nesting::NestingRule;
use page::PageRule;
#[cfg(feature = "serde")]
use serde::Serialize;
use std::collections::{HashMap, HashSet};
use style::StyleRule;
Expand All @@ -108,8 +109,8 @@ pub(crate) struct StyleContext<'a, 'i, T> {
}

/// A source location.
#[derive(PartialEq, Eq, Debug, Clone, Copy, Serialize)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Location {
/// The index of the source file within the source map.
pub source_index: u32,
Expand Down
4 changes: 3 additions & 1 deletion src/targets.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Browser target options.
// This file is autogenerated by build-prefixes.js. DO NOT EDIT!

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Browser versions to compile CSS for.
Expand All @@ -20,7 +21,8 @@ use serde::{Deserialize, Serialize};
/// ..Browsers::default()
/// };
/// ```
#[derive(Serialize, Debug, Deserialize, Clone, Copy, Default)]
#[derive(Debug, Clone, Copy, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub struct Browsers {
pub android: Option<u32>,
Expand Down
2 changes: 2 additions & 0 deletions src/values/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::visitor::{Visit, VisitTypes, Visitor};
use cssparser::{serialize_string, CowRcStr};
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer};
#[cfg(feature = "serde")]
use serde::{Serialize, Serializer};
use std::borrow::Borrow;
use std::cmp;
Expand Down Expand Up @@ -219,6 +220,7 @@ impl<'a> fmt::Debug for CowArcStr<'a> {
}
}

#[cfg(feature = "serde")]
impl<'a> Serialize for CowArcStr<'a> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.as_ref().serialize(serializer)
Expand Down

0 comments on commit a4b1ba5

Please sign in to comment.