sql_parse/
identifier.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13use core::borrow::Borrow;
14
15use crate::{Span, Spanned};
16
17/// Simple identifier in code
18/// it derefs to its string value
19#[derive(Clone, Debug)]
20pub struct Identifier<'a> {
21    /// Identifier string
22    pub value: &'a str,
23    /// Span of the value
24    pub span: Span,
25}
26
27impl<'a> PartialEq for Identifier<'a> {
28    fn eq(&self, other: &Self) -> bool {
29        self.value == other.value
30    }
31}
32impl<'a> Eq for Identifier<'a> {}
33
34impl<'a> PartialOrd for Identifier<'a> {
35    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
36        Some(self.cmp(other))
37    }
38}
39
40impl<'a> Ord for Identifier<'a> {
41    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
42        self.value.cmp(other.value)
43    }
44}
45
46impl<'a> alloc::fmt::Display for Identifier<'a> {
47    fn fmt(&self, f: &mut alloc::fmt::Formatter<'_>) -> alloc::fmt::Result {
48        self.value.fmt(f)
49    }
50}
51
52impl<'a> Borrow<str> for Identifier<'a> {
53    fn borrow(&self) -> &str {
54        self.value
55    }
56}
57
58impl<'a> Identifier<'a> {
59    /// Produce new identifier given value and span
60    pub fn new(value: &'a str, span: Span) -> Self {
61        Identifier { value, span }
62    }
63
64    /// Get the string representation of the identifier
65    pub fn as_str(&self) -> &'a str {
66        self.value
67    }
68}
69
70impl<'a> core::ops::Deref for Identifier<'a> {
71    type Target = str;
72
73    fn deref(&self) -> &'a Self::Target {
74        self.value
75    }
76}
77
78impl<'a> Spanned for Identifier<'a> {
79    fn span(&self) -> Span {
80        self.span.span()
81    }
82}