Skip to main content

model_core/
entity.rs

1use serde::{Deserialize, Serialize};
2
3/// Index of an entity in its [`crate::Model`]; only meaningful there.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
5#[serde(transparent)]
6pub struct EntityId(pub(crate) u32);
7
8impl EntityId {
9    pub fn index(self) -> usize {
10        self.0 as usize
11    }
12
13    /// Id of the entity at `index` (e.g. read back from a client); lookups
14    /// return `None` when it is out of the model's range.
15    pub fn from_index(index: u32) -> EntityId {
16        EntityId(index)
17    }
18}
19
20/// Open language identifier (`java`, `typescript`, `python`, `pharo`...):
21/// supporting a new language must not require changing this crate.
22#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
23#[serde(transparent)]
24pub struct LanguageId(String);
25
26impl LanguageId {
27    pub fn new(id: impl Into<String>) -> Self {
28        LanguageId(id.into())
29    }
30
31    pub fn as_str(&self) -> &str {
32        &self.0
33    }
34}
35
36impl std::fmt::Display for LanguageId {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        f.write_str(&self.0)
39    }
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
43#[serde(rename_all = "snake_case")]
44pub enum EntityKind {
45    /// Package, namespace, module.
46    Namespace,
47    File,
48    Class,
49    Interface,
50    Trait,
51    Enum,
52    Struct,
53    TypeAlias,
54    Method,
55    Constructor,
56    Function,
57    Field,
58    Property,
59    Constant,
60    Variable,
61}
62
63impl EntityKind {
64    /// Every archetype, in discriminant order (= [`crate::KindId::of`]).
65    pub const ALL: [EntityKind; 15] = [
66        EntityKind::Namespace,
67        EntityKind::File,
68        EntityKind::Class,
69        EntityKind::Interface,
70        EntityKind::Trait,
71        EntityKind::Enum,
72        EntityKind::Struct,
73        EntityKind::TypeAlias,
74        EntityKind::Method,
75        EntityKind::Constructor,
76        EntityKind::Function,
77        EntityKind::Field,
78        EntityKind::Property,
79        EntityKind::Constant,
80        EntityKind::Variable,
81    ];
82
83    /// The `snake_case` name used in JSON.
84    pub fn as_str(self) -> &'static str {
85        match self {
86            EntityKind::Namespace => "namespace",
87            EntityKind::File => "file",
88            EntityKind::Class => "class",
89            EntityKind::Interface => "interface",
90            EntityKind::Trait => "trait",
91            EntityKind::Enum => "enum",
92            EntityKind::Struct => "struct",
93            EntityKind::TypeAlias => "type_alias",
94            EntityKind::Method => "method",
95            EntityKind::Constructor => "constructor",
96            EntityKind::Function => "function",
97            EntityKind::Field => "field",
98            EntityKind::Property => "property",
99            EntityKind::Constant => "constant",
100            EntityKind::Variable => "variable",
101        }
102    }
103
104    pub fn is_type(self) -> bool {
105        matches!(
106            self,
107            EntityKind::Class
108                | EntityKind::Interface
109                | EntityKind::Trait
110                | EntityKind::Enum
111                | EntityKind::Struct
112                | EntityKind::TypeAlias
113        )
114    }
115
116    pub fn is_callable(self) -> bool {
117        matches!(
118            self,
119            EntityKind::Method | EntityKind::Constructor | EntityKind::Function
120        )
121    }
122
123    pub fn is_variable(self) -> bool {
124        matches!(
125            self,
126            EntityKind::Field | EntityKind::Property | EntityKind::Constant | EntityKind::Variable
127        )
128    }
129}