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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use proc_macro::TokenStream;

mod derive_from_inner;
mod derive_from_str_json;
mod derive_from_value;
mod derive_newtype;
mod derive_try_into_any_inner;
mod enum_factory;

// CR alee: move into netidx and add attr netidx_repr, to specify your choice of Bytes
// (default) or some other Value type
/// Derives `Into<Value>` and `From<Value>` from `netidx` for the given type.
///
/// Specifically, this macro derives `Into<netidx::protocol::value::Value>` and
/// `From<netidx::protocol::value::Value>` for the given type.  The type must implement
/// `netidx_core::pack::Pack` and the derived implementation will encode/decode the type as a
/// `netidx::protocol::value::Value::Bytes`.
#[proc_macro_derive(FromValue)]
pub fn derive_from_value(input: TokenStream) -> TokenStream {
    derive_from_value::derive_from_value(input)
}

/// Given an enum whose variants are all singular tuples, derive `From<InnerType>` for each
/// variant's inner type.
///
/// For example, given the following enum:
///
/// ```rust,ignore
/// #[derive(FromInner)]
/// enum Foo {
///     Ayy(A),
///     Bae(B),
/// }
/// ```
///
/// The following impls will be generated:
///
/// ```rust,ignore
/// impl From<A> for Foo { /* ... */ }
/// impl From<B> for Foo { /* ... */ }
/// ```
///
/// such that
///
/// ```rust,ignore
/// let a: A = /* ... */;
/// let b: B = /* ... */;
/// assert_eq!(Foo::from(a), Foo::Ayy(a));
/// assert_eq!(Foo::from(b), Foo::Bae(b));
/// ```
///
/// Each variant's inner type must be distinct, otherwise the macro expansion will produce
/// conflicting impls, which presents as a compilation error.
#[proc_macro_derive(FromInner)]
pub fn derive_from_inner(input: TokenStream) -> TokenStream {
    derive_from_inner::derive_from_inner(input)
}

/// Given an enum whose variants are all singular tuples, derive
/// `Enum: TryInto<MaybeSplit<Enum, Inner>>` for each variant's inner type.
///
/// Each variant's inner type must be distinct, otherwise the macro expansion will
/// produce conflicting impls, which presents as a compilation error.
#[proc_macro_derive(TryIntoAnyInner, attributes(transitive))]
pub fn derive_try_into_any_inner(input: TokenStream) -> TokenStream {
    derive_try_into_any_inner::derive_try_into_any_inner(input)
}

#[proc_macro_derive(Newtype, attributes(newtype))]
pub fn derive_newtype(input: TokenStream) -> TokenStream {
    derive_newtype::derive_newtype(input)
}

#[proc_macro_attribute]
pub fn enum_factory(args: TokenStream, input: TokenStream) -> TokenStream {
    enum_factory::enum_factory(args, input)
}

#[proc_macro_derive(FromStrJson)]
pub fn derive_from_str_json(input: TokenStream) -> TokenStream {
    derive_from_str_json::derive_from_str_json(input)
}