Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

specta-impls crate #185

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ bevy_ecs = ["dep:bevy_ecs"]

[dependencies]
specta-macros = { version = "=2.0.0-rc.6", path = "./macros" }
specta-impls = { version = ">=0.0.0", path = "./crates/impls" }
serde = { version = "1.0.183", optional = true, default-features = false, features = ["derive"] }
serde_json = { version = "1.0.104", optional = true, default-features = false, features = ["std"] }
serde_yaml = { version = "0.9.25", optional = true, default-features = false, features = [] }
Expand Down Expand Up @@ -139,3 +140,7 @@ once_cell = "1.18.0"
doc-comment = "0.3.3"
serde = { version = "1.0.183", features = ["derive"] }
trybuild = "1.0.82"
specta-impls = { version = ">=0.0.0", path = "./crates/impls", features = ["testing"] }

[workspace]
members = ["./crates/impls"]
10 changes: 10 additions & 0 deletions crates/impls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "specta-impls"
version = "0.0.1"
edition = "2021"

[features]
default = []
testing = []

[dependencies]
35 changes: 35 additions & 0 deletions crates/impls/src/internal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//! The magic that makes this crate work. This module does *NOT* follow semver at all.

// This macro must *NEVER* change signature, even in major releases!!!!!
// This is called by `specta` and `specta` depends on *any* version of this crate.
//
// This is a little hack to avoid the orphan rule. The code inside this is expanded inside `specta` so it can do `impl specta::Type for ...`.
#[macro_export]
macro_rules! impls {
// This runs in the context of the `specta` crate so the `cfg`'s won't work as expected.
() => {
use crate::{DataType, Type, TypeMap};

Check warning on line 11 in crates/impls/src/internal.rs

View workflow job for this annotation

GitHub Actions / clippy

`crate` references the macro call's crate

warning: `crate` references the macro call's crate --> crates/impls/src/internal.rs:11:13 | 11 | use crate::{DataType, Type, TypeMap}; | ^^^^^ help: to reference the macro definition's crate, use: `$crate` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#crate_in_macro_def note: the lint level is defined here --> crates/impls/src/lib.rs:6:9 | 6 | #![warn(clippy::all, clippy::unwrap_used, clippy::panic)] // TODO: missing_docs | ^^^^^^^^^^^ = note: `#[warn(clippy::crate_in_macro_def)]` implied by `#[warn(clippy::all)]`

$crate::_feature_testing!();
};
}

// TODO: Make a nicer abstraction for this

#[macro_export]
#[cfg(any(feature = "testing", docsrs))]
macro_rules! _feature_testing {
() => {
impl Type for specta_impls::Testing {
fn inline(_: &mut TypeMap, _: &[DataType]) -> DataType {
DataType::Any
}
}
};
}

#[macro_export]
#[cfg(not(any(feature = "testing", docsrs)))]
macro_rules! _feature_testing {
() => {};
}
18 changes: 18 additions & 0 deletions crates/impls/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! # Specta Implementations
//! A collection of Specta integrations for popular crates.
//!
//! TODO: List of supported features, why this exists, semver rules
#![forbid(unsafe_code)]
#![warn(clippy::all, clippy::unwrap_used, clippy::panic)] // TODO: missing_docs
#![allow(clippy::module_inception)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc(
html_logo_url = "https://github.com/oscartbeaumont/specta/raw/main/.github/logo-128.png",
html_favicon_url = "https://github.com/oscartbeaumont/specta/raw/main/.github/logo-128.png"
)]

#[doc(hidden)]
mod internal;

#[cfg(feature = "testing")]
pub struct Testing();
3 changes: 3 additions & 0 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ fn main() {
ts_str,
r#"export type Something = { a: { [key in MyEnum]: number } }"#.to_string()
);

let ts_str = ts::inline::<specta_impls::Testing>(&ExportConfig::default()).unwrap();
println!("{ts_str}");
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
#[doc(hidden)]
pub mod internal;

pub mod impls {
// TODO: Can we make these `impls`'s show up in the docs???
specta_impls::impls!();
}

/// Types related to working with [`DataType`](crate::DataType). Exposed for advanced users.
pub mod datatype;
/// Provides the global type store and a method to export them to other languages.
Expand Down
Loading