Skip to content

Commit

Permalink
Testing with --no-default-features didnt seem to work in CI (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
NyxCode authored May 20, 2024
1 parent 7d28c1a commit badbac0
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 33 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ jobs:
toolchain: stable
- uses: Swatinem/rust-cache@v2
- name: Test
working-directory: ts-rs
run: |
cargo test --all-features
shopt -s globstar
tsc ts-rs/bindings/**/*.ts --noEmit --noUnusedLocals --strict
rm -rf ts-rs/bindings
tsc bindings/**/*.ts --noEmit --noUnusedLocals --strict
rm -rf bindings
test-export-env:
name: Test ts-rs with TS_RS_EXPORT_DIR
Expand All @@ -118,11 +119,12 @@ jobs:
toolchain: stable
- uses: Swatinem/rust-cache@v2
- name: Test
working-directory: ts-rs
run: |
TS_RS_EXPORT_DIR=output cargo test --no-default-features
shopt -s globstar
tsc ts-rs/output/**/*.ts --noEmit --noUnusedLocals --strict
rm -rf ts-rs/output
tsc output/**/*.ts --noEmit --noUnusedLocals --strict
rm -rf output
test-no-features:
name: Test ts-rs with --no-default-features
Expand All @@ -136,8 +138,9 @@ jobs:
toolchain: stable
- uses: Swatinem/rust-cache@v2
- name: Test
working-directory: ts-rs
run: |
cargo test --no-default-features
shopt -s globstar
tsc ts-rs/bindings/**/*.ts --noEmit --noUnusedLocals
rm -rf ts-rs/bindings
tsc bindings/**/*.ts --noEmit --noUnusedLocals
rm -rf bindings
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
20 changes: 15 additions & 5 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(crate) fn parse_serde_attrs<'a, A>(attrs: &'a [Attribute]) -> Serde<A>
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 badbac0

Please sign in to comment.