Skip to main content

vvv_core/symbol/
kind.rs

1use std::fmt;
2use std::str::FromStr;
3
4use serde::{Deserialize, Serialize};
5
6/// What a declaration declares, in vocabulary shared by every language.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
8#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
9#[serde(rename_all = "kebab-case")]
10pub enum SymbolKind {
11    TypeParameter,
12    Parameter,
13    Function,
14    Method,
15    Struct,
16    Class,
17    Enum,
18    Variant,
19    Trait,
20    Interface,
21    TypeAlias,
22    Const,
23    Static,
24    Variable,
25    Field,
26    Module,
27    Macro,
28    /// An `impl` block, named after the type it is for. Not a declaration a
29    /// path can reach; it travels with its type when the type moves.
30    Impl,
31}
32
33impl SymbolKind {
34    pub const ALL: [SymbolKind; 18] = [
35        Self::Function,
36        Self::Method,
37        Self::Struct,
38        Self::Class,
39        Self::Enum,
40        Self::Variant,
41        Self::Trait,
42        Self::Interface,
43        Self::TypeAlias,
44        Self::Const,
45        Self::Static,
46        Self::Variable,
47        Self::Field,
48        Self::Module,
49        Self::Macro,
50        Self::Impl,
51        Self::TypeParameter,
52        Self::Parameter,
53    ];
54
55    /// Comma-separated list of every kind, for help and error messages.
56    pub fn choices() -> String {
57        Self::ALL.map(Self::as_str).join(", ")
58    }
59
60    pub const fn as_str(self) -> &'static str {
61        match self {
62            Self::TypeParameter => "type-parameter",
63            Self::Parameter => "parameter",
64            Self::Function => "function",
65            Self::Method => "method",
66            Self::Struct => "struct",
67            Self::Class => "class",
68            Self::Enum => "enum",
69            Self::Variant => "variant",
70            Self::Trait => "trait",
71            Self::Interface => "interface",
72            Self::TypeAlias => "type-alias",
73            Self::Const => "const",
74            Self::Static => "static",
75            Self::Variable => "variable",
76            Self::Field => "field",
77            Self::Module => "module",
78            Self::Macro => "macro",
79            Self::Impl => "impl",
80        }
81    }
82}
83
84#[derive(Debug, thiserror::Error, PartialEq, Eq)]
85#[error("unknown symbol kind `{0}`; expected one of {choices}", choices = SymbolKind::choices())]
86pub struct UnknownSymbolKind(pub String);
87
88impl FromStr for SymbolKind {
89    type Err = UnknownSymbolKind;
90
91    fn from_str(s: &str) -> Result<Self, Self::Err> {
92        Self::ALL
93            .into_iter()
94            .find(|k| k.as_str() == s)
95            .ok_or_else(|| UnknownSymbolKind(s.to_owned()))
96    }
97}
98
99impl fmt::Display for SymbolKind {
100    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101        f.write_str(self.as_str())
102    }
103}
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108
109    #[test]
110    fn round_trips_through_text_and_serde() {
111        for kind in SymbolKind::ALL {
112            assert_eq!(kind.as_str().parse::<SymbolKind>(), Ok(kind));
113        }
114        assert_eq!(
115            "bogus".parse::<SymbolKind>(),
116            Err(UnknownSymbolKind("bogus".into()))
117        );
118    }
119}