rasn_derive/
lib.rs

1use syn::{parse_macro_input, DeriveInput};
2
3/// Helper function print out the derive.
4fn __print_stream(stream: proc_macro2::TokenStream) -> proc_macro::TokenStream {
5    println!("{}", stream);
6    stream.into()
7}
8
9/// An automatic derive of the `Decode` trait.
10///
11/// Will automatically generate a decode implementation using the your
12/// container's definition. See [`AsnType`](`asn_type_derive`) for information
13/// on available attributes.
14#[proc_macro_derive(Decode, attributes(rasn))]
15pub fn decode_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
16    let derive_input = parse_macro_input!(input as DeriveInput);
17
18    rasn_derive_impl::decode_derive_inner(derive_input)
19        .unwrap_or_else(syn::Error::into_compile_error)
20        .into()
21}
22
23/// An automatic derive of the `Encode` trait.
24///
25/// Will automatically generate a encode implementation using the your
26/// container's definition. See [`AsnType`](`asn_type_derive`) for information
27/// on available attributes.
28#[proc_macro_derive(Encode, attributes(rasn))]
29pub fn encode_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
30    let derive_input = parse_macro_input!(input as DeriveInput);
31
32    rasn_derive_impl::encode_derive_inner(derive_input)
33        .unwrap_or_else(syn::Error::into_compile_error)
34        .into()
35}
36
37/// An automatic derive of the `AsnType` trait.
38///
39/// This macro will automatically generate an implementation of `AsnType`,
40/// and generate a *compile-time* check that all of your fields (if struct) or
41/// variants (if a choice style enum) have distinct tags.
42///
43/// ##### Shared Attributes
44/// These attributes are available on containers, variants, and fields.
45/// - *`tag([class], number)`* — override the default tag with the one
46///   specified with this attribute. E.g. `#[rasn(tag(context, 0))]`, you can also
47///   wrapp `[class], number` in `explicit` to mark it as a explicit tag
48///   (e.g.  `#[rasn(tag(explicit(0)))]`.)
49///
50/// ##### Container Attributes
51/// - `crate_root` The path to the `rasn` library to use in the macro.
52/// - `enumerated/choice` Use either `#[rasn(choice)]` or `#[rasn(enumerated)]`
53/// - `delegate` Only available for newtype wrappers (e.g. `struct Delegate(T)`);
54///   uses the inner `T` type for implementing the trait.
55#[proc_macro_derive(AsnType, attributes(rasn))]
56pub fn asn_type_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
57    let derive_input = parse_macro_input!(input as DeriveInput);
58
59    rasn_derive_impl::asn_type_derive_inner(derive_input)
60        .unwrap_or_else(syn::Error::into_compile_error)
61        .into()
62}