Skip to content

Commit

Permalink
Adjust feature gates
Browse files Browse the repository at this point in the history
  • Loading branch information
NyxCode committed May 17, 2024
1 parent 5c1a144 commit 4615aed
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 26 deletions.
4 changes: 1 addition & 3 deletions macros/src/attr/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ impl EnumAttr {
pub fn from_attrs(attrs: &[Attribute]) -> Result<Self> {
let mut result = parse_attrs::<Self>(attrs)?;

#[cfg(feature = "serde-compat")]
{
if cfg!(feature = "serde-compat") {
let serde_attr = crate::utils::parse_serde_attrs::<EnumAttr>(attrs);
result = result.merge(serde_attr.0);
}
Expand Down Expand Up @@ -223,7 +222,6 @@ impl_parse! {
}
}

#[cfg(feature = "serde-compat")]
impl_parse! {
Serde<EnumAttr>(input, out) {
"rename" => out.0.rename = Some(parse_assign_str(input)?),
Expand Down
13 changes: 6 additions & 7 deletions macros/src/attr/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub struct FieldAttr {
pub flatten: bool,
pub docs: String,

#[cfg(feature = "serde-compat")]
pub using_serde_with: bool,
}

Expand All @@ -35,8 +34,7 @@ impl FieldAttr {
pub fn from_attrs(attrs: &[Attribute]) -> Result<Self> {
let mut result = parse_attrs::<Self>(attrs)?;

#[cfg(feature = "serde-compat")]
if !result.skip {
if cfg!(feature = "serde-compat") && !result.skip {
let serde_attr = crate::utils::parse_serde_attrs::<FieldAttr>(attrs);
result = result.merge(serde_attr.0);
}
Expand Down Expand Up @@ -71,7 +69,7 @@ impl Attr for FieldAttr {
nullable: self.optional.nullable || other.optional.nullable,
},
flatten: self.flatten || other.flatten,
#[cfg(feature = "serde-compat")]

using_serde_with: self.using_serde_with || other.using_serde_with,

// We can't emit TSDoc for a flattened field
Expand All @@ -86,8 +84,10 @@ impl Attr for FieldAttr {
}

fn assert_validity(&self, field: &Self::Item) -> Result<()> {
#[cfg(feature = "serde-compat")]
if self.using_serde_with && !(self.type_as.is_some() || self.type_override.is_some()) {
if cfg!(feature = "serde-compat")
&& self.using_serde_with
&& !(self.type_as.is_some() || self.type_override.is_some())
{
syn_err_spanned!(
field;
r#"using `#[serde(with = "...")]` requires the use of `#[ts(as = "...")]` or `#[ts(type = "...")]`"#
Expand Down Expand Up @@ -196,7 +196,6 @@ impl_parse! {
}
}

#[cfg(feature = "serde-compat")]
impl_parse! {
Serde<FieldAttr>(input, out) {
"rename" => out.0.rename = Some(parse_assign_str(input)?),
Expand Down
2 changes: 0 additions & 2 deletions macros/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ pub(super) trait ContainerAttr: Attr {
fn crate_rename(&self) -> Path;
}

#[cfg(feature = "serde-compat")]
#[derive(Default)]
pub(super) struct Serde<T>(pub T)
where
T: Attr;

#[cfg(feature = "serde-compat")]
impl<T> Serde<T>
where
T: Attr,
Expand Down
4 changes: 1 addition & 3 deletions macros/src/attr/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ impl StructAttr {
pub fn from_attrs(attrs: &[Attribute]) -> Result<Self> {
let mut result = parse_attrs::<Self>(attrs)?;

#[cfg(feature = "serde-compat")]
{
if cfg!(feature = "serde-compat") {
let serde_attr = crate::utils::parse_serde_attrs::<StructAttr>(attrs);
result = result.merge(serde_attr.0);
}
Expand Down Expand Up @@ -145,7 +144,6 @@ impl_parse! {
}
}

#[cfg(feature = "serde-compat")]
impl_parse! {
Serde<StructAttr>(input, out) {
"rename" => out.0.rename = Some(parse_assign_str(input)?),
Expand Down
4 changes: 1 addition & 3 deletions macros/src/attr/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ pub struct VariantAttr {
impl VariantAttr {
pub fn from_attrs(attrs: &[Attribute]) -> Result<Self> {
let mut result = parse_attrs::<Self>(attrs)?;
#[cfg(feature = "serde-compat")]
if !result.skip {
if cfg!(feature = "serde-compat") && !result.skip {
let serde_attr = crate::utils::parse_serde_attrs::<VariantAttr>(attrs);
result = result.merge(serde_attr.0);
}
Expand Down Expand Up @@ -62,7 +61,6 @@ impl_parse! {
}
}

#[cfg(feature = "serde-compat")]
impl_parse! {
Serde<VariantAttr>(input, out) {
"rename" => out.0.rename = Some(parse_assign_str(input)?),
Expand Down
18 changes: 14 additions & 4 deletions macros/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,11 @@ where
}

/// Parse all `#[serde(..)]` attributes from the given slice.
#[cfg(feature = "serde-compat")]
#[allow(unused)]
pub fn parse_serde_attrs<'a, A>(attrs: &'a [Attribute]) -> Serde<A>
where
A: Attr,
Serde<A>: TryFrom<&'a Attribute, Error = Error>,
{
use crate::attr::Serde;

attrs
.iter()
.filter(|a| a.path().is_ident("serde"))
Expand Down Expand Up @@ -223,6 +219,20 @@ mod warning {
writer.print(&buffer)
}
}
#[cfg(not(feature = "serde-compat"))]
mod warning {
use std::fmt::Display;

// Just a stub!
#[allow(unused)]
pub fn print_warning(
title: impl Display,
content: impl Display,
note: impl Display,
) -> std::io::Result<()> {
Ok(())
}
}

/// formats the generic arguments (like A, B in struct X<A, B>{..}) as "<X>" where x is a comma
/// seperated list of generic arguments, or an empty string if there are no type generics (lifetime/const generics are ignored).
Expand Down
1 change: 1 addition & 0 deletions ts-rs/tests/integration/serde_skip_with_default.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "serde-compat")]
#![allow(dead_code)]

// from issue #107. This does now no longer generate a warning.
Expand Down
4 changes: 0 additions & 4 deletions ts-rs/tests/integration/struct_rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ struct RenameAllScreamingKebab {
#[test]
fn rename_all_screaming_kebab_case() {
let rename_all = RenameAllScreamingKebab::default();
assert_eq!(
serde_json::to_string(&rename_all).unwrap(),
r#"{"CRC32C-HASH":0,"SOME-FIELD":0,"SOME-OTHER-FIELD":0}"#
);
assert_eq!(
RenameAllScreamingKebab::inline(),
r#"{ "CRC32C-HASH": number, "SOME-FIELD": number, "SOME-OTHER-FIELD": number, }"#
Expand Down

0 comments on commit 4615aed

Please sign in to comment.