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

Restructure #247

Merged
merged 12 commits into from
Jun 28, 2024
1 change: 1 addition & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# https://github.com/oscartbeaumont/specta/pull/222
148 changes: 3 additions & 145 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,145 +1,3 @@
[package]
name = "specta"
description = "Easily export your Rust types to other languages"
version = "2.0.0-rc.13"
authors = ["Oscar Beaumont <[email protected]>"]
edition = "2021"
license = "MIT"
include = ["/src", "/examples", "/LICENCE", "/README.md"]
repository = "https://github.com/oscartbeaumont/specta"
documentation = "https://docs.rs/specta/latest/specta"
keywords = ["async", "specta", "rspc", "typescript", "typesafe"]
categories = ["web-programming", "asynchronous"]
autotests = false

# /bin/sh RUSTDOCFLAGS="--cfg docsrs2" cargo +nightly doc --all-features
[package.metadata."docs.rs"]
all-features = true
rustdoc-args = ["--cfg", "docsrs2"] # TODO: Rename to `docsrs2` once Tokio fixes https://github.com/tokio-rs/tokio/pull/6360

[[test]]
name = "integration_tests"
path = "tests/lib.rs"
harness = true

[[example]]
name = "export"
required-features = ["export", "typescript"]

# Run `node ./.github/features.js` to update the docs
[features]
default = []

#! Features
## Support for exporting the types of Rust functions.
function = ["serde", "specta-macros/function"]
## Support for collecting up a global type map
export = ["dep:ctor", "specta-macros/export"]

#! Languages
## Support for [TypeScript](https://www.typescriptlang.org) language exporting
typescript = ["serde"] # TODO: Probally don't keep `serde` here for v2???
## Support for [JSDoc](https://jsdoc.app) exporting helpers.
## Also requires `typescript` feature to be enabled.
js_doc = []
# ## Support for [Rust](https://www.rust-lang.org) language exporting
# rust = []
# ## Support for [Swift](https://www.swift.org/) language exporting
# swift = []
# ## Support for [Kotlin](https://kotlinlang.org/) language exporting
# kotlin = []
# ## Support for [Go Lang](https://go.dev/) language exporting
# go = []
# ## Support for [OpenAPI](https://www.openapis.org) language exporting
# openapi = ["dep:openapiv3"]

#! Compatability
## Support for [serde](https://serde.rs)
serde = ["dep:serde", "specta-macros/serde"]
## Support for [serde-json](https://github.com/serde-rs/json)
serde_json = ["dep:serde_json"]
## Support for [serde_yaml](https://github.com/dtolnay/serde-yaml)
serde_yaml = ["dep:serde_yaml"]
## Support for [toml](https://github.com/toml-rs/toml)
toml = ["dep:toml"]

#! External types
## [uuid](https://docs.rs/uuid) crate
uuid = ["dep:uuid"]
## [uuid](https://docs.rs/ulid) crate
ulid = ["dep:ulid"]
## [chrono](https://docs.rs/chrono) crate
chrono = ["dep:chrono"]
## [time](https://docs.rs/time) crate
time = ["dep:time"]
## [bigdecimal](https://docs.rs/bigdecimal) crate
bigdecimal = ["dep:bigdecimal"]
## [rust_decimal](https://docs.rs/rust_decimal) crate
rust_decimal = ["dep:rust_decimal"]
## [indexmap](https://docs.rs/indexmap) crate
indexmap = ["dep:indexmap"]
## [ipnetwork](https://docs.rs/ipnetwork) crate
ipnetwork = ["dep:ipnetwork"]
## [mac_address](https://docs.rs/mac_address) crate
mac_address = ["dep:mac_address"]
## [bit-vec](https://docs.rs/bit-vec) crate
bit-vec = ["dep:bit-vec"]
## [bson](https://docs.rs/bson) crate
bson = ["dep:bson"]
## [uhlc](https://docs.rs/uhlc) crate
uhlc = ["dep:uhlc"]
## [bytesize](https://docs.rs/bytesize) crate
bytesize = ["dep:bytesize"]
## [glam](https://docs.rs/glam) crate
glam = ["dep:glam"]
## [tokio](https://docs.rs/tokio) crate
tokio = ["dep:tokio"]
## [url](https://docs.rs/url) crate
url = ["dep:url"]
## [either](https://docs.rs/either) crate
either = ["dep:either"]
# [bevy_ecs](https://docs.rs/bevy_ecs) crate
bevy_ecs = ["dep:bevy_ecs"]
# [bevy_input](https://docs.rs/bevy_input) crate
bevy_input = ["dep:bevy_input", "dep:bevy_ecs", "dep:glam"]

# Internal features (unstable)
interop = ["dep:specta1"]

[dependencies]
specta-macros = { version = "=2.0.0-rc.13", path = "./macros" }
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 = [] }
toml = { version = "0.7.6", optional = true, default-features = false }
ulid = { version = "1.1.0", optional = true, default-features = false, features = [] }
uuid = { version = "1.4.1", optional = true, default-features = false, features = [] }
chrono = { version = "0.4.26", optional = true, default-features = false, features = ["clock"] }
time = { version = "0.3.25", optional = true, default-features = false, features = [] }
bigdecimal = { version = "0.4.1", optional = true, default-features = false, features = [] }
rust_decimal = { version = "1.31.0", optional = true, default-features = false, features = [] }
indexmap = { version = "2.0.0", optional = true, default-features = false, features = ["std"] } # TODO: Don't require `std`
ipnetwork = { version = "0.20.0", optional = true, default-features = false, features = [] }
mac_address = { version = "1.1.5", optional = true, default-features = false, features = [] }
bit-vec = { version = "0.6.3", optional = true, default-features = false, features = [] }
bson = { version = "2.6.1", optional = true, default-features = false, features = [] }
openapiv3 = { version = "1.0.2", optional = true, default-features = false, features = [] }
uhlc = { version = "0.6.0", optional = true, default-features = false, features = [] }
bytesize = { version = "1.2.0", optional = true, default-features = false, features = [] }
glam = { version = "0.25", optional = true, default-features = false, features = ["std"] }
tokio = { version = "1.30", optional = true, default-features = false, features = ["sync"] }
url = { version = "2.4.0", optional = true, default-features = false }
either = { version = "1.9.0", optional = true, default-features = false }
bevy_ecs = { version = "0.13.2", optional = true, default-features = false }
bevy_input = { version = "0.13.2", optional = true, default-features = false }
thiserror = "1.0.44"
paste = "1.0.14"
ctor = { version = "0.2.4", optional = true }
once_cell = "1.18.0"
specta1 = { package = "specta", version = "1", optional = true, default-features = false }

[dev-dependencies]
doc-comment = "0.3.3"
serde = { version = "1.0.183", features = ["derive"] }
trybuild = "1.0.82"
wasm-bindgen = "0.2.89"
[workspace]
members = ["specta", "specta-*", "tests"]
resolver = "2"
4 changes: 3 additions & 1 deletion examples/export.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use specta::{
use specta_util::{
export,
ts::{BigIntExportBehavior, ExportConfig},
Type,
Expand Down Expand Up @@ -28,3 +28,5 @@ fn main() {
)
.unwrap();
}

fn main() {}
20 changes: 20 additions & 0 deletions specta-datatype-from/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "specta-datatype-from"
description = "Dynamically construct types with Specta"
version = "0.0.1"
authors = ["Oscar Beaumont <[email protected]>"]
edition = "2021"
license = "MIT"
repository = "https://github.com/oscartbeaumont/specta"
documentation = "https://docs.rs/specta/latest/specta"
keywords = ["async", "specta", "typesafe"]
categories = ["web-programming", "asynchronous"]

[lib]
proc-macro = true

[dependencies]
proc-macro2 = "1.0.49"
quote = "1.0.23"
syn = { version = "1.0.107", features = ["full"] }
Inflector = { version = "0.11.4", default-features = false }
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::Result;

use crate::utils::Attribute;
use crate::utils::{impl_parse, Attribute};

#[derive(Default)]
pub struct ContainerAttr {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::Result;

use crate::utils::Attribute;
use crate::utils::{impl_parse, Attribute};

#[derive(Default)]
pub struct FieldAttr {
Expand Down
File renamed without changes.
72 changes: 72 additions & 0 deletions specta-datatype-from/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//! Dynamically construct types with Specta
//!
//! Generates an implementation to help converting a type into into [`DataType`].
//!
//! **This is an advanced feature and should probably be limited to usage in libraries built on top of Specta.**
//!
//! This differs from [`Type`] in that you can use other [`DataType`] values
//! at runtime inside the targeted type, providing an easy way to construct types at
//! runtime from other types which are known statically via [`Type`].
//!
//! Along with inner data types such as [`StructType`] and [`EnumType`], some builtin types
//! can easily be convert to a [`DataType`]:
//! - [`Vec`] will become [`DataType::Enum`]
//! - [`Option`] will become the value it contains or [`LiteralType::None`] if it is [`None`]
//! - [`String`] and [`&str`] will become [`LiteralType::String`]
//!
//! # Example
//! ```rust
//! use specta::{datatype::LiteralType, ts, DataType, DataTypeFrom, StructType, TupleType};
//!
//! #[derive(Clone, DataTypeFrom)]
//! pub struct MyEnum(pub Vec<DataType>);
//!
//! #[derive(Clone, DataTypeFrom)]
//! pub struct MyObject {
//! a: Vec<DataType>,
//! }
//!
//! //
//! // Enum
//! //
//!
//! let val: DataType = MyEnum(vec![
//! LiteralType::String("A".to_string()).into(),
//! LiteralType::String("B".to_string()).into(),
//! ]).into();
//!
//! let anon = ts::datatype(&Default::default(), &val, &Default::default()).unwrap();
//! assert_eq!(anon, "\"A\" | \"B\"");
//!
//! let named = val.to_named("MyEnum");
//! let named_export = ts::export_named_datatype(&Default::default(), &named, &Default::default()).unwrap();
//! assert_eq!(named_export, "export type MyEnum = \"A\" | \"B\"");
//!
//! //
//! // Object
//! //
//!
//! let val: StructType = MyObject {
//! a: vec![
//! LiteralType::String("A".to_string()).into(),
//! LiteralType::String("B".to_string()).into(),
//! ],
//! }
//! .into();
//!
//! let anon = val.clone().to_anonymous();
//! let anon = ts::datatype(&Default::default(), &anon, &Default::default()).unwrap();
//! assert_eq!(anon, "{ a: \"A\" | \"B\" }");
//!
//! let named = val.to_named("MyObject");
//! let named_export = ts::export_named_datatype(&Default::default(), &named, &Default::default()).unwrap();
//! assert_eq!(named_export, "export type MyObject = { a: \"A\" | \"B\" }");
//! ```

mod data_type_from;
mod utils;

#[proc_macro_derive(DataTypeFrom, attributes(specta))]
pub fn derive_data_type_from(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
data_type_from::derive(input).unwrap_or_else(|err| err.into_compile_error().into())
}
Loading
Loading