specta_macros/
lib.rs

1//! Easily export your Rust types to other languages
2//!
3//! This crate contains the macro which are reexported by the `specta` crate.
4//! You shouldn't need to use this crate directly.
5//! Checkout [Specta](https://docs.rs/specta).
6//!
7#[macro_use]
8mod utils;
9mod data_type_from;
10#[cfg(feature = "functions")]
11mod fn_datatype;
12#[cfg(feature = "functions")]
13mod specta;
14mod r#type;
15
16#[proc_macro_derive(Type, attributes(specta, serde, doc))]
17pub fn derive_type(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
18    r#type::derive(input, "specta".into()).unwrap_or_else(|err| err.into_compile_error().into())
19}
20
21/// This macro is exposed from rspc as a wrapper around [Type] with a correct import path.
22/// This is exposed from here so rspc doesn't need a macro package for 4 lines of code.
23#[doc(hidden)]
24#[proc_macro_derive(RSPCType, attributes(specta, serde, doc))]
25pub fn derive_rspc_type(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
26    r#type::derive(input, "rspc::internal::specta".into())
27        .unwrap_or_else(|err| err.into_compile_error().into())
28}
29
30#[proc_macro_derive(DataTypeFrom, attributes(specta))]
31pub fn derive_data_type_from(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
32    data_type_from::derive(input).unwrap_or_else(|err| err.into_compile_error().into())
33}
34
35#[proc_macro_attribute]
36#[cfg(feature = "functions")]
37pub fn specta(
38    _: proc_macro::TokenStream,
39    item: proc_macro::TokenStream,
40) -> proc_macro::TokenStream {
41    specta::attribute(item).unwrap_or_else(|err| err.into_compile_error().into())
42}
43
44#[proc_macro]
45#[cfg(feature = "functions")]
46pub fn fn_datatype(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
47    use syn::parse_macro_input;
48
49    fn_datatype::proc_macro(parse_macro_input!(input as fn_datatype::FnDatatypeInput))
50        .unwrap_or_else(|err| err.into_compile_error())
51        .into()
52}