musli_macros/internals/
ctxt.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt::{self, Write};
use std::rc::Rc;

use proc_macro2::Span;

use super::attr::{ModeIdent, ModeKind};
use super::build::Field;
use super::ATTR;

struct Inner {
    b1: String,
    modes: HashMap<ModeKind, ModeIdent>,
    errors: Vec<syn::Error>,
    #[cfg(not(feature = "verbose"))]
    names: HashMap<String, usize>,
    #[cfg(not(feature = "verbose"))]
    types: usize,
}

pub(crate) struct Ctxt {
    inner: RefCell<Inner>,
}

impl Ctxt {
    /// Construct a new handling context.
    pub(crate) fn new() -> Self {
        Self {
            inner: RefCell::new(Inner {
                b1: String::new(),
                modes: HashMap::new(),
                errors: Vec::new(),
                #[cfg(not(feature = "verbose"))]
                names: HashMap::new(),
                #[cfg(not(feature = "verbose"))]
                types: 0,
            }),
        }
    }

    /// Emit diagnostics for a transparent encode / decode that failed because
    /// the wrong number of fields existed.
    pub(crate) fn transparent_diagnostics(&self, span: Span, fields: &[Rc<Field>]) {
        if fields.is_empty() {
            self.error_span(
                span,
                format_args!("#[{ATTR}(transparent)] types must have a single unskipped field"),
            );
        } else {
            self.error_span(
                span,
                format_args!(
                    "#[{ATTR}(transparent)] can only be used on types which have a single field",
                ),
            );
        }
    }

    /// Register a new mode.
    pub(crate) fn register_mode(&self, mode: ModeIdent) {
        self.inner
            .borrow_mut()
            .modes
            .insert(mode.kind.clone(), mode);
    }

    /// Test if context contains errors.
    pub(crate) fn has_errors(&self) -> bool {
        !self.inner.borrow().errors.is_empty()
    }

    /// Report an error with a span.
    pub(crate) fn error_span<T>(&self, span: Span, message: T)
    where
        T: fmt::Display,
    {
        self.inner
            .borrow_mut()
            .errors
            .push(syn::Error::new(span, message));
    }

    /// Error reported directly by syn.
    pub(crate) fn syn_error(&self, error: syn::Error) {
        self.inner.borrow_mut().errors.push(error);
    }

    /// Access interior errors.
    pub(crate) fn into_errors(self) -> Vec<syn::Error> {
        std::mem::take(&mut self.inner.borrow_mut().errors)
    }

    /// Get all extra modes specified.
    pub(crate) fn modes(&self) -> Vec<ModeIdent> {
        self.inner.borrow().modes.values().cloned().collect()
    }

    pub(crate) fn reset(&self) {
        #[cfg(not(feature = "verbose"))]
        {
            let mut inner = self.inner.borrow_mut();
            inner.names.clear();
            inner.types = 0;
        }
    }

    /// Build a lifetime.
    #[allow(unused)]
    pub(crate) fn lifetime(&self, name: &str) -> syn::Lifetime {
        self.with_string("'", name, "", |s| syn::Lifetime::new(s, Span::call_site()))
    }

    /// Build an identifier with the given name, escaped so it's harder to conflict with.
    pub(crate) fn ident(&self, name: &str) -> syn::Ident {
        self.ident_with_span(name, Span::call_site(), "")
    }

    /// Build an identifier with the given name, escaped so it's harder to conflict with.
    pub(crate) fn ident_with_span(&self, name: &str, span: Span, extra: &str) -> syn::Ident {
        self.with_string("", name, extra, |s| syn::Ident::new(s, span))
    }

    fn with_string<F, O>(&self, prefix: &str, name: &str, suffix: &str, f: F) -> O
    where
        F: FnOnce(&str) -> O,
    {
        let mut inner = self.inner.borrow_mut();

        #[cfg(not(feature = "verbose"))]
        {
            let index = if let Some(index) = inner.names.get(name) {
                *index
            } else {
                let index = inner.names.len();
                inner.names.insert(name.to_owned(), index);
                index
            };

            _ = write!(inner.b1, "{prefix}_{index}{suffix}");
        }

        #[cfg(feature = "verbose")]
        {
            let name = name.strip_prefix('_').unwrap_or(name);
            _ = write!(inner.b1, "{prefix}_{name}{suffix}");
        }

        let ident = f(&inner.b1);
        inner.b1.clear();
        ident
    }

    /// Build a type identifier with a span.
    pub(crate) fn type_with_span<N>(
        &self,
        #[cfg_attr(not(feature = "verbose"), allow(unused))] name: N,
        span: Span,
    ) -> syn::Ident
    where
        N: fmt::Display,
    {
        let mut inner = self.inner.borrow_mut();

        #[cfg(not(feature = "verbose"))]
        {
            let index = inner.types;
            inner.types += 1;
            _ = write!(inner.b1, "T{index}");
        }

        #[cfg(feature = "verbose")]
        {
            _ = write!(inner.b1, "{name}");
        }

        let ident = syn::Ident::new(&inner.b1, span);
        inner.b1.clear();
        ident
    }
}