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
#[cfg(feature = "uuid_ident_gen")]
mod uuid;

#[cfg(feature = "uuid_ident_gen")]
pub use self::uuid::UUIDIdentGenerator;

#[cfg(any(test, feature = "mock_ident_gen"))]
mod mock;

#[cfg(any(test, feature = "mock_ident_gen"))]
#[allow(clippy::module_name_repetitions)]
pub use mock::MockIdentGenerator;

mod counting;
#[allow(clippy::module_name_repetitions)]
pub use counting::CountingIdentGenerator;

use proc_macro2::Span;
use syn::Ident;

/// Some type that can generate unique idents, used to avoid collisions between multiple
/// types that are generated.
pub trait IdentGenerator {
    /// Convenience method to create some prefixed ident.
    #[must_use]
    fn prefixed(&mut self, prefix: &'static str) -> Ident {
        self.generate(Some(prefix), Span::call_site())
    }

    /// Convenience method to generate just any ident.
    #[must_use]
    fn ident(&mut self) -> Ident {
        self.generate(None, Span::call_site())
    }

    /// Convenience method to generate just any ident with a specific [`Span`].
    #[must_use]
    fn spanned(&mut self, span: Span) -> Ident {
        self.generate(None, span)
    }

    /// Main method which actually implements the generation.
    #[must_use]
    fn generate(&mut self, prefix: Option<&'static str>, span: Span) -> Ident;
}