Skip to content

Commit bc125dc

Browse files
committed
make encoding-impl optional
1 parent a0ff3c2 commit bc125dc

File tree

29 files changed

+168
-148
lines changed

29 files changed

+168
-148
lines changed

Cargo.lock

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

crates/ast_node/src/encoding/decode.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn expand(
2626
let #name = #value;
2727
},
2828
None => {
29-
let name = format!("unit{}", idx);
29+
let name = format!("unit{idx}");
3030
syn::parse_quote!{
3131
let #name = #value;
3232
}
@@ -39,7 +39,7 @@ pub fn expand(
3939
match field.ident.as_ref() {
4040
Some(name) => syn::parse_quote!(#name),
4141
None => {
42-
let name = format!("unit{}", idx);
42+
let name = format!("unit{idx}");
4343
syn::parse_quote!(#name)
4444
}
4545
}
@@ -119,11 +119,11 @@ pub fn expand(
119119
let fields = iter
120120
.enumerate()
121121
.map(|(idx, field)| -> syn::Arm {
122-
let idx: u32 = idx.try_into().expect("enum tag max 32bit");
123122
let idx = idx + 1; // skip zero
123+
let idx: u32 = idx.try_into().expect("enum tags must not exceed 32 bits");
124124
let name = &field.ident;
125125

126-
assert!(field.discriminant.is_none(), "custom discriminant unsupport");
126+
assert!(field.discriminant.is_none(), "custom discriminant is not allowed");
127127
assert!(!is_unknown(&field.attrs), "unknown member must be first");
128128

129129
match &field.fields {
@@ -132,8 +132,8 @@ pub fn expand(
132132
panic!("enum member only allows one field");
133133
}
134134

135-
if *is_unit.get_or_insert(false) != false {
136-
panic!("The number of fields in member must be consistent");
135+
if *is_unit.get_or_insert(false) {
136+
panic!("the number of fields in member must be consistent");
137137
}
138138
let val_ty = &fields.unnamed[0].ty;
139139
let value: syn::Expr = match is_with(&field.attrs) {
@@ -149,8 +149,8 @@ pub fn expand(
149149
}
150150
},
151151
syn::Fields::Unit => {
152-
if *is_unit.get_or_insert(true) != true {
153-
panic!("The number of fields in member must be consistent");
152+
if !*is_unit.get_or_insert(true) {
153+
panic!("the number of fields in member must be consistent");
154154
}
155155
assert!(is_with(&field.attrs).is_none(), "unit member is not allowed with type");
156156

crates/ast_node/src/encoding/encode.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn expand(
5555
iter.next_if(|variant| is_unknown(&variant.attrs))
5656
{
5757
let name = &unknown.ident;
58-
assert!(unknown.discriminant.is_none(), "custom discriminant unsupport");
58+
assert!(unknown.discriminant.is_none(), "custom discriminant is not allowed");
5959
assert!(is_with(&unknown.attrs).is_none(), "unknown member is not allowed with type");
6060

6161
match &unknown.fields {
@@ -94,21 +94,21 @@ pub fn expand(
9494
let fields = iter
9595
.enumerate()
9696
.map(|(idx, field)| -> syn::Arm {
97-
let idx: u32 = idx.try_into().expect("max 32bit");
9897
let idx = idx + 1; // skip zero
98+
let idx: u32 = idx.try_into().expect("enum tags must not exceed 32 bits");
9999
let name = &field.ident;
100100

101+
assert!(field.discriminant.is_none(), "custom discriminant is not allowed");
101102
assert!(!is_unknown(&field.attrs), "unknown member must be first: {:?}", field.attrs.len());
102-
assert!(field.discriminant.is_none(), "custom discriminant unsupport");
103103

104104
match &field.fields {
105105
syn::Fields::Unnamed(fields) => {
106106
if fields.unnamed.len() != 1 {
107107
panic!("enum member only allows one field");
108108
}
109109

110-
if *is_unit.get_or_insert(false) != false {
111-
panic!("The number of fields in member must be consistent");
110+
if *is_unit.get_or_insert(false) {
111+
panic!("the number of fields in member must be consistent");
112112
}
113113

114114
let value: syn::Stmt = match is_with(&field.attrs) {
@@ -129,8 +129,8 @@ pub fn expand(
129129
}
130130
},
131131
syn::Fields::Unit => {
132-
if *is_unit.get_or_insert(true) != true {
133-
panic!("The number of fields in member must be consistent");
132+
if !*is_unit.get_or_insert(true) {
133+
panic!("the number of fields in member must be consistent");
134134
}
135135
assert!(is_with(&field.attrs).is_none(), "unit member is not allowed with type");
136136

crates/ast_node/src/encoding/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ pub(crate) mod decode;
44
pub(crate) fn is_unknown(attrs: &[syn::Attribute]) -> bool {
55
attrs.iter()
66
.filter(|attr| attr.path().is_ident("encoding"))
7-
.find(|attr| {
7+
.any(|attr| {
88
let mut is_unknown = false;
99
attr.parse_nested_meta(|meta| {
1010
is_unknown |= meta.path.is_ident("unknown");
1111
Ok(())
1212
}).unwrap();
1313
is_unknown
1414
})
15-
.is_some()
1615
}
1716

1817
fn is_with(attrs: &[syn::Attribute]) -> Option<syn::Path> {

crates/ast_node/src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ pub fn ast_node(
199199
None
200200
};
201201

202-
let unknown: syn::Variant = if data.variants.iter().all(|variant| variant.fields.len() == 0) {
202+
let unknown: syn::Variant = if data.variants.iter().all(|variant| variant.fields.is_empty()) {
203203
syn::parse_quote!{
204204
#[cfg(feature = "unknown")]
205205
#[from_variant(ignore)]
@@ -263,7 +263,10 @@ pub fn ast_node(
263263
feature = "serde-impl",
264264
serde(untagged)
265265
)]
266-
#[derive(::swc_common::Encode, ::swc_common::Decode)]
266+
#[cfg_attr(
267+
feature = "encoding-impl",
268+
derive(::swc_common::Encode, ::swc_common::Decode)
269+
)]
267270
#input
268271
));
269272
}
@@ -338,7 +341,10 @@ pub fn ast_node(
338341
serde(rename_all = "camelCase")
339342
)]
340343
#serde_rename
341-
#[derive(::swc_common::Encode, ::swc_common::Decode)]
344+
#[cfg_attr(
345+
feature = "encoding-impl",
346+
derive(::swc_common::Encode, ::swc_common::Decode)
347+
)]
342348
#input
343349
));
344350

crates/ast_node/src/spanned.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,14 @@ impl Parse for InputFieldAttr {
3030
fn is_unknown(attrs: &[syn::Attribute]) -> bool {
3131
attrs.iter()
3232
.filter(|attr| attr.path().is_ident("span"))
33-
.find(|attr| {
33+
.any(|attr| {
3434
let mut is_unknown = false;
3535
attr.parse_nested_meta(|meta| {
3636
is_unknown |= meta.path.is_ident("unknown");
3737
Ok(())
3838
}).unwrap();
3939
is_unknown
4040
})
41-
.is_some()
4241
}
4342

4443
impl MyField {

crates/from_variant/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ pub fn derive_from_variant(input: proc_macro::TokenStream) -> proc_macro::TokenS
2222
fn is_ignored(attrs: &[syn::Attribute]) -> bool {
2323
attrs.iter()
2424
.filter(|attr| attr.path().is_ident("from_variant"))
25-
.find(|attr| {
25+
.any(|attr| {
2626
let mut is_unknown = false;
2727
attr.parse_nested_meta(|meta| {
2828
is_unknown |= meta.path.is_ident("ignore");
2929
Ok(())
3030
}).unwrap();
3131
is_unknown
3232
})
33-
.is_some()
3433
}
3534

3635
fn derive(

crates/swc_atoms/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ bench = false
1414
[features]
1515
__rkyv = []
1616
rkyv-impl = ["__rkyv", "rkyv", "bytecheck", "rancor"]
17+
encoding-impl = ["cbor4ii"]
1718
shrink-to-fit = ["dep:shrink-to-fit"]
1819

1920
[dependencies]
@@ -25,7 +26,6 @@ rancor = { workspace = true, optional = true }
2526
rkyv = { workspace = true, optional = true }
2627
serde = { workspace = true }
2728
shrink-to-fit = { workspace = true, optional = true }
29+
cbor4ii = { workspace = true, features = [ "use_std" ], optional = true }
2830

2931
hstr = { version = "2.0.1", path = "../hstr" }
30-
31-
cbor4ii = { workspace = true, features = [ "use_std" ] }

crates/swc_atoms/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ mod fast;
3434
#[cfg_attr(feature = "rkyv-impl", repr(C))]
3535
pub struct Atom(hstr::Atom);
3636

37+
#[cfg(feature = "encoding-impl")]
3738
impl cbor4ii::core::enc::Encode for Atom {
3839
#[inline]
3940
fn encode<W: cbor4ii::core::enc::Write>(&self, writer: &mut W) -> Result<(), cbor4ii::core::enc::Error<W::Error>> {
4041
self.as_str().encode(writer)
4142
}
4243
}
4344

45+
#[cfg(feature = "encoding-impl")]
4446
impl<'de> cbor4ii::core::dec::Decode<'de> for Atom {
4547
#[inline]
4648
fn decode<R: cbor4ii::core::dec::Read<'de>>(reader: &mut R) -> Result<Self, cbor4ii::core::dec::Error<R::Error>> {

crates/swc_common/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ rkyv-impl = [
4242
"bytecheck",
4343
"rancor",
4444
]
45+
encoding-impl = ["cbor4ii", "swc_atoms/encoding-impl"]
4546

4647
shrink-to-fit = ["dep:shrink-to-fit", "swc_atoms/shrink-to-fit"]
4748

@@ -67,6 +68,7 @@ termcolor = { workspace = true, optional = true }
6768
tracing = { workspace = true }
6869
unicode-width = { workspace = true }
6970
url = { workspace = true }
71+
cbor4ii = { workspace = true, features = [ "use_std" ], optional = true }
7072

7173
ast_node = { version = "3.0.4", path = "../ast_node" }
7274
better_scoped_tls = { version = "1.0.1", path = "../better_scoped_tls" }
@@ -75,8 +77,6 @@ swc_atoms = { version = "7.0.0", path = "../swc_atoms" }
7577
swc_eq_ignore_macros = { version = "1.0.1", path = "../swc_eq_ignore_macros" }
7678
swc_visit = { version = "2.0.1", path = "../swc_visit" }
7779

78-
cbor4ii = { workspace = true, features = [ "use_std" ] }
79-
8080
[dev-dependencies]
8181
par-iter = { workspace = true }
8282
serde_json = { workspace = true }

0 commit comments

Comments
 (0)