summaryrefslogtreecommitdiff
path: root/src/chomp/name.rs
blob: 450b6e973614727a3b6a4586b3141d1c550e5d54 (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use std::{
    array::IntoIter,
    cmp::{self, Ordering},
    fmt, hash,
};

use heck::{CamelCase, SnakeCase};
use proc_macro2::{Ident, Span};
use syn::{ext::IdentExt, spanned::Spanned};

#[derive(Clone, Debug)]
pub enum Content {
    Spanned(Ident),
    Spanless(String),
}

impl Spanned for Content {
    fn span(&self) -> Span {
        match self {
            Content::Spanned(i) => i.span(),
            Content::Spanless(_) => Span::call_site(),
        }
    }
}

impl From<Content> for Ident {
    fn from(content: Content) -> Self {
        match content {
            Content::Spanned(i) => i,
            Content::Spanless(s) => Ident::new(&s, Span::call_site()),
        }
    }
}

impl CamelCase for Content {
    fn to_camel_case(&self) -> Self::Owned {
        match self {
            Self::Spanned(ident) => {
                let span = ident.span();
                let name = ident.unraw().to_string();
                Ident::new(&name.to_camel_case(), span).into()
            }
            Self::Spanless(name) => name.to_camel_case().into(),
        }
    }
}

impl SnakeCase for Content {
    fn to_snake_case(&self) -> Self::Owned {
        match self {
            Self::Spanned(ident) => {
                let span = ident.span();
                let name = ident.unraw().to_string();
                Ident::new(&name.to_snake_case(), span).into()
            }
            Self::Spanless(name) => name.to_snake_case().into(),
        }
    }
}

impl PartialEq for Content {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Spanned(me), Self::Spanned(them)) => me == them,
            (Self::Spanned(me), Self::Spanless(them)) => me.unraw() == them,
            (Self::Spanless(me), Self::Spanned(them)) => them.unraw() == me,
            (Self::Spanless(me), Self::Spanless(them)) => me == them,
        }
    }
}

impl<T: AsRef<str>> PartialEq<T> for Content {
    fn eq(&self, other: &T) -> bool {
        match self {
            Self::Spanned(me) => me.unraw() == other,
            Self::Spanless(me) => me == other.as_ref(),
        }
    }
}

impl Eq for Content {}

impl hash::Hash for Content {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        match self {
            Self::Spanned(i) => i.unraw().to_string().hash(state),
            Self::Spanless(s) => s.hash(state),
        }
    }
}

impl fmt::Display for Content {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Spanned(i) => i.fmt(f),
            Self::Spanless(s) => s.fmt(f),
        }
    }
}

impl From<Ident> for Content {
    fn from(ident: Ident) -> Self {
        Self::Spanned(ident)
    }
}

impl From<String> for Content {
    fn from(string: String) -> Self {
        Self::Spanless(string)
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Kind {
    Label,
    Let,
    Variable,
}

impl PartialOrd for Kind {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(match (self, other) {
            (Kind::Label, Kind::Label)
            | (Kind::Let, Kind::Let)
            | (Kind::Variable, Kind::Variable) => Ordering::Equal,
            (Kind::Label, Kind::Let)
            | (Kind::Label, Kind::Variable)
            | (Kind::Let, Kind::Variable) => Ordering::Greater,
            (Kind::Let, Kind::Label)
            | (Kind::Variable, Kind::Label)
            | (Kind::Variable, Kind::Let) => Ordering::Less,
        })
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Name {
    pub content: Content,
    kind: Kind,
}

impl Name {
    pub fn new_variable<T: Into<Content>>(content: T) -> Self {
        Self {
            content: content.into(),
            kind: Kind::Variable,
        }
    }

    pub fn new_let<T: Into<Content>>(content: T) -> Self {
        Self {
            content: content.into(),
            kind: Kind::Let,
        }
    }

    pub fn new_label<T: Into<Content>>(content: T) -> Self {
        Self {
            content: content.into(),
            kind: Kind::Label,
        }
    }

    pub fn merge(this: Option<Self>, that: Option<Self>) -> Option<Self> {
        match (this, that) {
            (None, that) => that,
            (Some(this), None) => Some(this),
            (Some(this), Some(that)) => {
                if this.kind >= that.kind {
                    Some(this)
                } else {
                    Some(that)
                }
            }
        }
    }

    pub fn merge_all<I: IntoIterator<Item = Option<Self>>>(iter: I) -> Option<Self> {
        iter.into_iter().fold(None, Self::merge)
    }
}

impl Spanned for Name {
    fn span(&self) -> Span {
        self.content.span()
    }
}

impl From<Name> for Ident {
    fn from(name: Name) -> Self {
        name.content.into()
    }
}

impl CamelCase for Name {
    fn to_camel_case(&self) -> Self::Owned {
        Self {
            content: self.content.to_camel_case(),
            kind: self.kind,
        }
    }
}

impl SnakeCase for Name {
    fn to_snake_case(&self) -> Self::Owned {
        Self {
            content: self.content.to_snake_case(),
            kind: self.kind,
        }
    }
}

impl fmt::Display for Name {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.content.fmt(f)
    }
}