use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SymbolKind {
Struct,
Enum,
Function,
Method,
Trait,
Impl,
Mod,
Const,
Static,
TypeAlias,
Union,
Macro,
Variable,
Parameter,
Field,
TupleField,
Variant,
Use,
Any,
Other,
}
impl SymbolKind {
pub fn is_in_symbol(&self) -> bool {
matches!(
self,
SymbolKind::Variable
| SymbolKind::Parameter
| SymbolKind::Field
| SymbolKind::TupleField
)
}
pub fn is_type(&self) -> bool {
matches!(
self,
SymbolKind::Struct
| SymbolKind::Enum
| SymbolKind::Trait
| SymbolKind::TypeAlias
| SymbolKind::Union
)
}
pub fn is_callable(&self) -> bool {
matches!(self, SymbolKind::Function | SymbolKind::Method)
}
pub fn is_value(&self) -> bool {
matches!(
self,
SymbolKind::Const
| SymbolKind::Static
| SymbolKind::Field
| SymbolKind::TupleField
| SymbolKind::Variable
| SymbolKind::Parameter
)
}
pub fn display_name(&self) -> &'static str {
match self {
SymbolKind::Struct => "struct",
SymbolKind::Enum => "enum",
SymbolKind::Function => "function",
SymbolKind::Method => "method",
SymbolKind::Trait => "trait",
SymbolKind::Impl => "impl",
SymbolKind::Mod => "module",
SymbolKind::Const => "const",
SymbolKind::Static => "static",
SymbolKind::TypeAlias => "type alias",
SymbolKind::Union => "union",
SymbolKind::Macro => "macro",
SymbolKind::Variable => "variable",
SymbolKind::Parameter => "parameter",
SymbolKind::Field => "field",
SymbolKind::TupleField => "tuple field",
SymbolKind::Variant => "variant",
SymbolKind::Use => "use",
SymbolKind::Any => "*",
SymbolKind::Other => "other",
}
}
pub fn is_any(&self) -> bool {
matches!(self, SymbolKind::Any)
}
pub fn matches(&self, other: &SymbolKind) -> bool {
*self == SymbolKind::Any || *other == SymbolKind::Any || self == other
}
}
impl std::fmt::Display for SymbolKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.display_name())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_in_symbol() {
assert!(SymbolKind::Variable.is_in_symbol());
assert!(SymbolKind::Parameter.is_in_symbol());
assert!(SymbolKind::Field.is_in_symbol());
assert!(!SymbolKind::Function.is_in_symbol());
assert!(!SymbolKind::Struct.is_in_symbol());
}
#[test]
fn test_is_type() {
assert!(SymbolKind::Struct.is_type());
assert!(SymbolKind::Enum.is_type());
assert!(SymbolKind::Trait.is_type());
assert!(!SymbolKind::Function.is_type());
assert!(!SymbolKind::Variable.is_type());
}
#[test]
fn test_is_callable() {
assert!(SymbolKind::Function.is_callable());
assert!(SymbolKind::Method.is_callable());
assert!(!SymbolKind::Struct.is_callable());
assert!(!SymbolKind::Const.is_callable());
}
#[test]
fn test_display_name() {
assert_eq!(SymbolKind::Struct.display_name(), "struct");
assert_eq!(SymbolKind::Function.display_name(), "function");
assert_eq!(SymbolKind::Variable.display_name(), "variable");
}
}