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
use std::{
    cmp::Ordering,
    fmt::{self, Display, Formatter},
    hash::{Hash, Hasher},
    str::FromStr,
};

use proc_macro2::Span;
use quote::ToTokens;
use syn::{spanned::Spanned, Path, Type};

#[derive(Debug, Clone)]
pub(crate) struct HashType(String, Span);

impl PartialEq for HashType {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.0.eq(&other.0)
    }
}

impl Eq for HashType {}

impl PartialOrd for HashType {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for HashType {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        self.0.cmp(&other.0)
    }
}

impl Hash for HashType {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        Hash::hash(&self.0, state);
    }
}

impl Display for HashType {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        Display::fmt(&self.0.replace("& '", "&'"), f)
    }
}

impl From<Type> for HashType {
    #[inline]
    fn from(value: Type) -> Self {
        Self::from(&value)
    }
}

impl From<&Type> for HashType {
    #[inline]
    fn from(value: &Type) -> Self {
        Self(value.into_token_stream().to_string(), value.span())
    }
}

impl From<Path> for HashType {
    #[inline]
    fn from(value: Path) -> Self {
        Self::from(&value)
    }
}

impl From<&Path> for HashType {
    #[inline]
    fn from(value: &Path) -> Self {
        Self(value.into_token_stream().to_string(), value.span())
    }
}

#[allow(dead_code)]
impl HashType {
    #[inline]
    pub(crate) fn to_type(&self) -> Type {
        syn::parse_str(self.0.as_str()).unwrap()
    }

    #[inline]
    pub(crate) fn span(&self) -> Span {
        self.1
    }
}

impl ToTokens for HashType {
    #[inline]
    fn to_tokens(&self, token_stream: &mut proc_macro2::TokenStream) {
        let ty = proc_macro2::TokenStream::from_str(self.0.as_str()).unwrap();

        token_stream.extend(ty);
    }
}