1#[cfg(doc)]
3use crate::public_item::PublicItem;
4
5#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub enum Token {
8 Symbol(String),
10 Qualifier(String),
12 Kind(String),
14 Whitespace,
16 Identifier(String),
18 Annotation(String),
20 Self_(String),
22 Function(String),
24 Lifetime(String),
26 Keyword(String),
28 Generic(String),
30 Primitive(String),
32 Type(String),
34}
35
36impl Token {
37 pub(crate) fn symbol(text: impl Into<String>) -> Self {
39 Self::Symbol(text.into())
40 }
41 pub(crate) fn qualifier(text: impl Into<String>) -> Self {
43 Self::Qualifier(text.into())
44 }
45 pub(crate) fn kind(text: impl Into<String>) -> Self {
47 Self::Kind(text.into())
48 }
49 pub(crate) fn identifier(text: impl Into<String>) -> Self {
51 Self::Identifier(text.into())
52 }
53 pub(crate) fn self_(text: impl Into<String>) -> Self {
55 Self::Self_(text.into())
56 }
57 pub(crate) fn function(text: impl Into<String>) -> Self {
59 Self::Function(text.into())
60 }
61 pub(crate) fn lifetime(text: impl Into<String>) -> Self {
63 Self::Lifetime(text.into())
64 }
65 pub(crate) fn keyword(text: impl Into<String>) -> Self {
67 Self::Keyword(text.into())
68 }
69 pub(crate) fn generic(text: impl Into<String>) -> Self {
71 Self::Generic(text.into())
72 }
73 pub(crate) fn primitive(text: impl Into<String>) -> Self {
75 Self::Primitive(text.into())
76 }
77 pub(crate) fn type_(text: impl Into<String>) -> Self {
79 Self::Type(text.into())
80 }
81 #[allow(clippy::len_without_is_empty)]
83 #[must_use]
84 pub fn len(&self) -> usize {
85 self.text().len()
86 }
87 #[must_use]
89 pub fn text(&self) -> &str {
90 match self {
91 Self::Symbol(l)
92 | Self::Qualifier(l)
93 | Self::Kind(l)
94 | Self::Identifier(l)
95 | Self::Annotation(l)
96 | Self::Self_(l)
97 | Self::Function(l)
98 | Self::Lifetime(l)
99 | Self::Keyword(l)
100 | Self::Generic(l)
101 | Self::Primitive(l)
102 | Self::Type(l) => l,
103 Self::Whitespace => " ",
104 }
105 }
106}
107
108pub(crate) fn tokens_to_string(tokens: &[Token]) -> String {
109 tokens.iter().map(Token::text).collect()
110}