musli_macros/internals/
tokens.rs

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
83
84
85
use proc_macro2::Span;

pub(crate) struct Tokens {
    pub(crate) as_decoder_t: syn::Path,
    pub(crate) context_t: syn::Path,
    pub(crate) decode_bytes_t: syn::Path,
    pub(crate) decode_packed_t: syn::Path,
    pub(crate) decode_t: syn::Path,
    pub(crate) decoder_t: syn::Path,
    pub(crate) default_function: syn::Path,
    pub(crate) encode_bytes_t: syn::Path,
    pub(crate) encode_packed_t: syn::Path,
    pub(crate) encode_t: syn::Path,
    pub(crate) encoder_t: syn::Path,
    pub(crate) fmt: syn::Path,
    pub(crate) map_decoder_t: syn::Path,
    pub(crate) map_encoder_t: syn::Path,
    pub(crate) map_entry_encoder_t: syn::Path,
    pub(crate) map_hint: syn::Path,
    pub(crate) option_none: syn::Path,
    pub(crate) option_some: syn::Path,
    pub(crate) option: syn::Path,
    pub(crate) pack_decoder_t: syn::Path,
    pub(crate) result_err: syn::Path,
    pub(crate) result_ok: syn::Path,
    pub(crate) result: syn::Path,
    pub(crate) sequence_encoder_t: syn::Path,
    pub(crate) skip_field: syn::Path,
    pub(crate) skip: syn::Path,
    pub(crate) struct_field_decoder_t: syn::Path,
    pub(crate) trace_decode_t: syn::Path,
    pub(crate) trace_encode_t: syn::Path,
    pub(crate) variant_decoder_t: syn::Path,
    pub(crate) variant_encoder_t: syn::Path,
    pub(crate) prefix: syn::Path,
}

impl Tokens {
    pub(crate) fn new(span: Span, prefix: syn::Path) -> Self {
        Self {
            as_decoder_t: path(span, &prefix, ["de", "AsDecoder"]),
            context_t: path(span, &prefix, ["Context"]),
            decode_bytes_t: path(span, &prefix, ["de", "DecodeBytes"]),
            decode_packed_t: path(span, &prefix, ["de", "DecodePacked"]),
            decode_t: path(span, &prefix, ["de", "Decode"]),
            decoder_t: path(span, &prefix, ["de", "Decoder"]),
            default_function: path(span, &prefix, ["__priv", "default"]),
            encode_bytes_t: path(span, &prefix, ["en", "EncodeBytes"]),
            encode_packed_t: path(span, &prefix, ["en", "EncodePacked"]),
            encode_t: path(span, &prefix, ["en", "Encode"]),
            encoder_t: path(span, &prefix, ["en", "Encoder"]),
            fmt: path(span, &prefix, ["__priv", "fmt"]),
            map_decoder_t: path(span, &prefix, ["de", "MapDecoder"]),
            map_encoder_t: path(span, &prefix, ["en", "MapEncoder"]),
            map_entry_encoder_t: path(span, &prefix, ["en", "EntryEncoder"]),
            map_hint: path(span, &prefix, ["hint", "MapHint"]),
            option_none: path(span, &prefix, ["__priv", "None"]),
            option_some: path(span, &prefix, ["__priv", "Some"]),
            option: path(span, &prefix, ["__priv", "Option"]),
            pack_decoder_t: path(span, &prefix, ["de", "SequenceDecoder"]),
            result_err: path(span, &prefix, ["__priv", "Err"]),
            result_ok: path(span, &prefix, ["__priv", "Ok"]),
            result: path(span, &prefix, ["__priv", "Result"]),
            sequence_encoder_t: path(span, &prefix, ["en", "SequenceEncoder"]),
            skip_field: path(span, &prefix, ["__priv", "skip_field"]),
            skip: path(span, &prefix, ["__priv", "skip"]),
            struct_field_decoder_t: path(span, &prefix, ["de", "EntryDecoder"]),
            trace_decode_t: path(span, &prefix, ["de", "DecodeTrace"]),
            trace_encode_t: path(span, &prefix, ["en", "EncodeTrace"]),
            variant_decoder_t: path(span, &prefix, ["de", "VariantDecoder"]),
            variant_encoder_t: path(span, &prefix, ["en", "VariantEncoder"]),
            prefix,
        }
    }
}

fn path<const N: usize>(span: Span, prefix: &syn::Path, segments: [&'static str; N]) -> syn::Path {
    let mut path = prefix.clone();

    for segment in segments {
        path.segments.push(syn::Ident::new(segment, span).into());
    }

    path
}