Skip to content

Commit ff02e81

Browse files
authored
Serde support (parcel-bundler#176)
1 parent b1afb80 commit ff02e81

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+987
-41
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ jobs:
1818
- uses: dtolnay/rust-toolchain@stable
1919
- uses: Swatinem/rust-cache@v1
2020
- run: cargo fmt
21-
- run: cargo test
21+
- run: cargo test --all-features

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@ indoc = "1.0.3"
5252
assert_cmd = "2.0"
5353
assert_fs = "1.0"
5454
predicates = "2.1"
55+
serde_json = "1.0.78"
5556

5657
[features]
5758
default = ["grid"]
5859
cli = ["clap", "serde_json", "pathdiff", "browserslist-rs", "jemallocator"]
5960
grid = []
61+
serde = ["smallvec/serde", "cssparser/serde"]
6062

6163
[[test]]
6264
name = "cli_integration_tests"

examples/serialize.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
fn main() {
2+
parse();
3+
}
4+
5+
#[cfg(feature = "serde")]
6+
fn parse() {
7+
use parcel_css::stylesheet::{ParserOptions, StyleSheet};
8+
use std::{env, fs};
9+
10+
let args: Vec<String> = env::args().collect();
11+
let contents = fs::read_to_string(&args[1]).unwrap();
12+
let stylesheet = StyleSheet::parse(&args[1], &contents, ParserOptions::default()).unwrap();
13+
let json = serde_json::to_string(&stylesheet).unwrap();
14+
println!("{}", json);
15+
}
16+
17+
#[cfg(not(feature = "serde"))]
18+
fn parse() {
19+
panic!("serde feature is not enabled")
20+
}

src/declaration.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ use cssparser::*;
4141
/// and a list of normal declarations. This reduces memory usage compared
4242
/// with storing a boolean along with each property.
4343
#[derive(Debug, PartialEq, Clone)]
44+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
4445
pub struct DeclarationBlock<'i> {
4546
/// A list of `!important` declarations in the block.
47+
#[cfg_attr(feature = "serde", serde(borrow))]
4648
pub important_declarations: Vec<Property<'i>>,
4749
/// A list of normal declarations in the block.
4850
pub declarations: Vec<Property<'i>>,

src/dependencies.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ impl ImportDependency {
6262
media,
6363
loc: SourceRange::new(
6464
filename,
65-
SourceLocation {
66-
line: rule.loc.line,
65+
Location {
66+
line: rule.loc.line + 1,
6767
column: rule.loc.column,
6868
},
6969
8,
@@ -109,24 +109,34 @@ pub struct SourceRange {
109109
}
110110

111111
/// A line and column position within a source file.
112-
#[derive(Serialize)]
112+
#[derive(Serialize, Debug, Clone, Copy, PartialEq)]
113+
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
113114
pub struct Location {
114-
/// The line number, starting from 0.
115+
/// The line number, starting from 1.
115116
pub line: u32,
116117
/// The column number, starting from 1.
117118
pub column: u32,
118119
}
119120

121+
impl From<SourceLocation> for Location {
122+
fn from(loc: SourceLocation) -> Location {
123+
Location {
124+
line: loc.line + 1,
125+
column: loc.column,
126+
}
127+
}
128+
}
129+
120130
impl SourceRange {
121-
fn new(filename: &str, loc: SourceLocation, offset: u32, len: usize) -> SourceRange {
131+
fn new(filename: &str, loc: Location, offset: u32, len: usize) -> SourceRange {
122132
SourceRange {
123133
file_path: filename.into(),
124134
start: Location {
125-
line: loc.line + 1,
135+
line: loc.line,
126136
column: loc.column + offset,
127137
},
128138
end: Location {
129-
line: loc.line + 1,
139+
line: loc.line,
130140
column: loc.column + offset + (len as u32) - 1,
131141
},
132142
}

src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::fmt;
1010

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

3233
/// A line and column location within a source file.
3334
#[derive(Debug, PartialEq, Clone)]
35+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
3436
pub struct ErrorLocation {
3537
/// The filename in which the error occurred.
3638
pub filename: String,

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14291,7 +14291,7 @@ mod tests {
1429114291
);
1429214292
error_test(
1429314293
"@-moz-document url-prefix(\"foo\") {}",
14294-
ParserError::UnexpectedToken(crate::properties::custom::Token::QuotedString("foo".into())),
14294+
ParserError::UnexpectedToken(crate::properties::custom::Token::String("foo".into())),
1429514295
);
1429614296
}
1429714297

src/macros.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ macro_rules! enum_property {
1010
) => {
1111
$(#[$outer])*
1212
#[derive(Debug, Clone, Copy, PartialEq)]
13+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(rename_all = "lowercase"))]
1314
$vis enum $name {
1415
$(
1516
$(#[$meta])*
@@ -66,9 +67,11 @@ macro_rules! enum_property {
6667
) => {
6768
$(#[$outer])*
6869
#[derive(Debug, Clone, Copy, PartialEq)]
70+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6971
$vis enum $name {
7072
$(
7173
$(#[$meta])*
74+
#[cfg_attr(feature = "serde", serde(rename = $str))]
7275
$id,
7376
)+
7477
}
@@ -319,6 +322,7 @@ macro_rules! define_shorthand {
319322
) => {
320323
$(#[$outer])*
321324
#[derive(Debug, Clone, PartialEq)]
325+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
322326
pub struct $name$(<$l>)? {
323327
$(
324328
$(#[$meta])*
@@ -532,6 +536,7 @@ macro_rules! define_list_shorthand {
532536
) => {
533537
$(#[$outer])*
534538
#[derive(Debug, Clone, PartialEq)]
539+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
535540
pub struct $name$(<$l>)? {
536541
$(
537542
$(#[$meta])*

src/media_query.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ use std::collections::{HashMap, HashSet};
1616

1717
/// A [media query list](https://drafts.csswg.org/mediaqueries/#mq-list).
1818
#[derive(Clone, Debug, PartialEq)]
19+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1920
pub struct MediaList<'i> {
2021
/// The list of media queries.
22+
#[cfg_attr(feature = "serde", serde(borrow))]
2123
pub media_queries: Vec<MediaQuery<'i>>,
2224
}
2325

@@ -141,6 +143,11 @@ enum_property! {
141143

142144
/// A [media type](https://drafts.csswg.org/mediaqueries/#media-types) within a media query.
143145
#[derive(Clone, Debug, PartialEq)]
146+
#[cfg_attr(
147+
feature = "serde",
148+
derive(serde::Serialize, serde::Deserialize),
149+
serde(tag = "type", content = "value", rename_all = "kebab-case")
150+
)]
144151
pub enum MediaType<'i> {
145152
/// Matches all devices.
146153
All,
@@ -150,6 +157,7 @@ pub enum MediaType<'i> {
150157
/// Matches all devices that aren’t matched by print.
151158
Screen,
152159
/// An unknown media type.
160+
#[cfg_attr(feature = "serde", serde(borrow))]
153161
Custom(CowArcStr<'i>),
154162
}
155163

@@ -167,10 +175,12 @@ impl<'i> Parse<'i> for MediaType<'i> {
167175

168176
/// A [media query](https://drafts.csswg.org/mediaqueries/#media).
169177
#[derive(Clone, Debug, PartialEq)]
178+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
170179
pub struct MediaQuery<'i> {
171180
/// The qualifier for this query.
172181
pub qualifier: Option<Qualifier>,
173182
/// The media type for this query, that can be known, unknown, or "all".
183+
#[cfg_attr(feature = "serde", serde(borrow))]
174184
pub media_type: MediaType<'i>,
175185
/// The condition that this media query contains. This cannot have `or`
176186
/// in the first level.
@@ -354,8 +364,14 @@ enum_property! {
354364

355365
/// Represents a media condition.
356366
#[derive(Clone, Debug, PartialEq)]
367+
#[cfg_attr(
368+
feature = "serde",
369+
derive(serde::Serialize, serde::Deserialize),
370+
serde(tag = "type", content = "value", rename_all = "kebab-case")
371+
)]
357372
pub enum MediaCondition<'i> {
358373
/// A media feature, implicitly parenthesized.
374+
#[cfg_attr(feature = "serde", serde(borrow))]
359375
Feature(MediaFeature<'i>),
360376
/// A negation of a condition.
361377
Not(Box<MediaCondition<'i>>),
@@ -460,6 +476,11 @@ impl<'i> ToCss for MediaCondition<'i> {
460476

461477
/// A [comparator](https://drafts.csswg.org/mediaqueries/#typedef-mf-comparison) within a media query.
462478
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
479+
#[cfg_attr(
480+
feature = "serde",
481+
derive(serde::Serialize, serde::Deserialize),
482+
serde(tag = "type", content = "value", rename_all = "kebab-case")
483+
)]
463484
pub enum MediaFeatureComparison {
464485
/// `=`
465486
Equal,
@@ -511,10 +532,16 @@ impl MediaFeatureComparison {
511532

512533
/// A [media feature](https://drafts.csswg.org/mediaqueries/#typedef-media-feature)
513534
#[derive(Clone, Debug, PartialEq)]
535+
#[cfg_attr(
536+
feature = "serde",
537+
derive(serde::Serialize, serde::Deserialize),
538+
serde(tag = "type", content = "value", rename_all = "kebab-case")
539+
)]
514540
pub enum MediaFeature<'i> {
515541
/// A plain media feature, e.g. `(min-width: 240px)`.
516542
Plain {
517543
/// The name of the feature.
544+
#[cfg_attr(feature = "serde", serde(borrow))]
518545
name: CowArcStr<'i>,
519546
/// The feature value.
520547
value: MediaFeatureValue<'i>,
@@ -707,6 +734,11 @@ where
707734
///
708735
/// See [MediaFeature](MediaFeature).
709736
#[derive(Clone, Debug, PartialEq)]
737+
#[cfg_attr(
738+
feature = "serde",
739+
derive(serde::Serialize, serde::Deserialize),
740+
serde(tag = "type", content = "value", rename_all = "kebab-case")
741+
)]
710742
pub enum MediaFeatureValue<'i> {
711743
/// A length value.
712744
Length(Length),
@@ -717,6 +749,7 @@ pub enum MediaFeatureValue<'i> {
717749
/// A ratio.
718750
Ratio(Ratio),
719751
/// An indentifier.
752+
#[cfg_attr(feature = "serde", serde(borrow))]
720753
Ident(CowArcStr<'i>),
721754
}
722755

0 commit comments

Comments
 (0)