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