architect_derive/lib.rs
1use proc_macro::TokenStream;
2
3mod derive_from_inner;
4mod derive_from_str_json;
5mod derive_from_value;
6mod derive_newtype;
7mod derive_try_into_any_inner;
8mod enum_factory;
9
10// CR alee: move into netidx and add attr netidx_repr, to specify your choice of Bytes
11// (default) or some other Value type
12/// Derives `Into<Value>` and `From<Value>` from `netidx` for the given type.
13///
14/// Specifically, this macro derives `Into<netidx::protocol::value::Value>` and
15/// `From<netidx::protocol::value::Value>` for the given type. The type must implement
16/// `netidx_core::pack::Pack` and the derived implementation will encode/decode the type as a
17/// `netidx::protocol::value::Value::Bytes`.
18#[proc_macro_derive(FromValue)]
19pub fn derive_from_value(input: TokenStream) -> TokenStream {
20 derive_from_value::derive_from_value(input)
21}
22
23/// Given an enum whose variants are all singular tuples, derive `From<InnerType>` for each
24/// variant's inner type.
25///
26/// For example, given the following enum:
27///
28/// ```rust,ignore
29/// #[derive(FromInner)]
30/// enum Foo {
31/// Ayy(A),
32/// Bae(B),
33/// }
34/// ```
35///
36/// The following impls will be generated:
37///
38/// ```rust,ignore
39/// impl From<A> for Foo { /* ... */ }
40/// impl From<B> for Foo { /* ... */ }
41/// ```
42///
43/// such that
44///
45/// ```rust,ignore
46/// let a: A = /* ... */;
47/// let b: B = /* ... */;
48/// assert_eq!(Foo::from(a), Foo::Ayy(a));
49/// assert_eq!(Foo::from(b), Foo::Bae(b));
50/// ```
51///
52/// Each variant's inner type must be distinct, otherwise the macro expansion will produce
53/// conflicting impls, which presents as a compilation error.
54#[proc_macro_derive(FromInner)]
55pub fn derive_from_inner(input: TokenStream) -> TokenStream {
56 derive_from_inner::derive_from_inner(input)
57}
58
59/// Given an enum whose variants are all singular tuples, derive
60/// `Enum: TryInto<MaybeSplit<Enum, Inner>>` for each variant's inner type.
61///
62/// Each variant's inner type must be distinct, otherwise the macro expansion will
63/// produce conflicting impls, which presents as a compilation error.
64#[proc_macro_derive(TryIntoAnyInner, attributes(transitive))]
65pub fn derive_try_into_any_inner(input: TokenStream) -> TokenStream {
66 derive_try_into_any_inner::derive_try_into_any_inner(input)
67}
68
69#[proc_macro_derive(Newtype, attributes(newtype))]
70pub fn derive_newtype(input: TokenStream) -> TokenStream {
71 derive_newtype::derive_newtype(input)
72}
73
74#[proc_macro_attribute]
75pub fn enum_factory(args: TokenStream, input: TokenStream) -> TokenStream {
76 enum_factory::enum_factory(args, input)
77}
78
79#[proc_macro_derive(FromStrJson)]
80pub fn derive_from_str_json(input: TokenStream) -> TokenStream {
81 derive_from_str_json::derive_from_str_json(input)
82}
83
84#[proc_macro_attribute]
85pub fn grpc(_attr: TokenStream, item: TokenStream) -> TokenStream {
86 // NB alee: just an indicator for the tonic build script
87 item
88}