1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#![feature(drain_filter)]

use proc_macro::TokenStream;
mod errorx;
mod pricing;
mod serde;

use errorx::emit::{emit, emit_all};
use syn::parse_macro_input;

#[proc_macro]
pub fn define_scope_internal(input: TokenStream) -> TokenStream {
	let ast: errorx::ast::DefineScope = syn::parse(input).unwrap();
	emit::<true>(&ast).into()
}

#[proc_macro]
pub fn define_scope(input: TokenStream) -> TokenStream {
	let ast: errorx::ast::DefineScopes = syn::parse(input).unwrap();
	emit_all(&ast.0).into()
}

/// Marks the type with a generated unique type id that distinct among generics and package versions.
///
/// Use `#[response(<ResponseType>)]` to apply a response type so that it could be called on rpc calls.
///
/// If the type name ends with `Request`, then a response attribute is automatically added with the suffix of `Response` by convention.
///
/// # Examples
///
/// ```
/// use tea_codec_macros::TypeId;
///
/// #[derive(TypeId)]
/// // a `#[response(GetSystemTimeResponse)]` is automatically added by convention.
/// pub struct GetSystemTimeRequest;
///
/// #[derive(TypeId)]
/// pub struct GetSystemTimeResponse(pub u128);
/// ```
#[proc_macro_derive(TypeId, attributes(response))]
pub fn derive_type_id(input: TokenStream) -> TokenStream {
	let input: serde::ast::Input = parse_macro_input!(input);
	serde::emit::emit(input).into()
}

#[proc_macro_derive(Priced, attributes(price))]
pub fn derive_priced(input: TokenStream) -> TokenStream {
	let input: pricing::ast::Input = parse_macro_input!(input);
	pricing::emit::emit(input).into()
}