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
#![warn(missing_docs)]
//! Crate for Identifiers used in the WAGon ecosystem.
//!
//! Identifiers are used throughout various parts of WAGon for various reasons. This crate acts as a central location to retrieve them from.
use std::{fmt::Display, write};

use quote::{quote, ToTokens};

#[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord)]
/// A valid WAGon identifier.
pub enum Ident {
    /// An inherited attribute
    Inherit(String),
    /// A synthesized attribute
    Synth(String),
    /// A local attribute
    Local(String),
    /// Either an attribute that we do not yet know the scope of, or an identifier used for another purpose.
    Unknown(String)
}

impl ToTokens for Ident {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let new_tokens = match self {
            Self::Inherit(s) => quote!(wagon_ident::Ident::Inherit(#s.to_string())),
            Self::Synth(s) => quote!(wagon_ident::Ident::Synth(#s.to_string())),
            Self::Local(s) => quote!(wagon_ident::Ident::Local(#s.to_string())),
            Self::Unknown(s) => quote!(wagon_ident::Ident::Unknown(#s.to_string())),
        };
        tokens.extend(new_tokens);
    }
}

impl Default for Ident {
    fn default() -> Self {
        Self::Unknown(String::new())
    }
}

impl Display for Ident {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Inherit(i) => write!(f, "*{i}"),
            Self::Synth(i) => write!(f, "&{i}"),
            Self::Local(i) => write!(f, "${i}"),
            Self::Unknown(i) => write!(f, "{i}"),
        }
    }
}

impl Ident {
    /// Get the original string from the identifier.
    #[must_use]
    pub fn extract_string(&self) -> &str {
        match self {
            Self::Inherit(s) | Self::Synth(s) | Self::Local(s) | Self::Unknown(s) => s,
        }
    }

    /// Convert into a standardized [`proc_macro2::Ident`] for code generation purposes.
    ///
    /// # Conversions
    /// | `Ident` | `proc_macro2::Ident` |
    /// |:-------:|:--------------------:|
    /// | `*foo`  | `i_foo`              |
    /// | `&foo`  | `s_foo`              |
    /// | `$foo`  | `l_foo`              |
    /// | `foo`   | `u_foo`              |
    #[must_use]
    pub fn to_ident(&self) -> proc_macro2::Ident {
        let text = match self {
            Self::Inherit(s) => format!("i_{s}"),
            Self::Synth(s) => format!("s_{s}"),
            Self::Local(s) => format!("l_{s}"),
            Self::Unknown(s) => format!("u_{s}"),
        };
        proc_macro2::Ident::new(&text, proc_macro2::Span::call_site())
    }

    /// Converts the Ident (whatever it is) into a synthesized ident with the same name.
    #[must_use] 
    pub fn to_synth(&self) -> Self {
        Self::Synth(self.extract_string().to_string())
    }
}

impl From<&str> for Ident {
    fn from(value: &str) -> Self {
        Self::Unknown(value.to_string())
    }
}