public_api/
tokens.rs

1//! Contains all token handling logic.
2#[cfg(doc)]
3use crate::public_item::PublicItem;
4
5/// A token in a rendered [`PublicItem`], used to apply syntax coloring in downstream applications.
6#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub enum Token {
8    /// A symbol, like `=` or `::<`
9    Symbol(String),
10    /// A qualifier, like `pub` or `const`
11    Qualifier(String),
12    /// The kind of an item, like `function` or `trait`
13    Kind(String),
14    /// Whitespace, a single space
15    Whitespace,
16    /// An identifier, like variable names or parts of the path of an item
17    Identifier(String),
18    /// An annotation, used e.g. for Rust attributes.
19    Annotation(String),
20    /// The identifier self, the text can be `self` or `Self`
21    Self_(String),
22    /// The identifier for a function, like `fn_arg` in `comprehensive_api::functions::fn_arg`
23    Function(String),
24    /// A lifetime including the apostrophe `'`, like `'a`
25    Lifetime(String),
26    /// A keyword, like `impl`, `where`, or `dyn`
27    Keyword(String),
28    /// A generic parameter, like `T`
29    Generic(String),
30    /// A primitive type, like `usize`
31    Primitive(String),
32    /// A non-primitive type, like the name of a struct or a trait
33    Type(String),
34}
35
36impl Token {
37    /// A symbol, like `=` or `::<`
38    pub(crate) fn symbol(text: impl Into<String>) -> Self {
39        Self::Symbol(text.into())
40    }
41    /// A qualifier, like `pub` or `const`
42    pub(crate) fn qualifier(text: impl Into<String>) -> Self {
43        Self::Qualifier(text.into())
44    }
45    /// The kind of an item, like `function` or `trait`
46    pub(crate) fn kind(text: impl Into<String>) -> Self {
47        Self::Kind(text.into())
48    }
49    /// An identifier, like variable names or parts of the path of an item
50    pub(crate) fn identifier(text: impl Into<String>) -> Self {
51        Self::Identifier(text.into())
52    }
53    /// The identifier self, the text can be `self` or `Self`
54    pub(crate) fn self_(text: impl Into<String>) -> Self {
55        Self::Self_(text.into())
56    }
57    /// The identifier for a function, like `fn_arg` in `comprehensive_api::functions::fn_arg`
58    pub(crate) fn function(text: impl Into<String>) -> Self {
59        Self::Function(text.into())
60    }
61    /// A lifetime including the apostrophe `'`, like `'a`
62    pub(crate) fn lifetime(text: impl Into<String>) -> Self {
63        Self::Lifetime(text.into())
64    }
65    /// A keyword, like `impl`
66    pub(crate) fn keyword(text: impl Into<String>) -> Self {
67        Self::Keyword(text.into())
68    }
69    /// A generic, like `T`
70    pub(crate) fn generic(text: impl Into<String>) -> Self {
71        Self::Generic(text.into())
72    }
73    /// A primitive type, like `usize`
74    pub(crate) fn primitive(text: impl Into<String>) -> Self {
75        Self::Primitive(text.into())
76    }
77    /// A type, like `Iterator`
78    pub(crate) fn type_(text: impl Into<String>) -> Self {
79        Self::Type(text.into())
80    }
81    /// Give the length of the inner text of this token
82    #[allow(clippy::len_without_is_empty)]
83    #[must_use]
84    pub fn len(&self) -> usize {
85        self.text().len()
86    }
87    /// Get the inner text of this token
88    #[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}