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    pub fn is_type(self) -> bool {
65        matches!(
66            self,
67            EntityKind::Class
68                | EntityKind::Interface
69                | EntityKind::Trait
70                | EntityKind::Enum
71                | EntityKind::Struct
72                | EntityKind::TypeAlias
73        )
74    }
75
76    pub fn is_callable(self) -> bool {
77        matches!(
78            self,
79            EntityKind::Method | EntityKind::Constructor | EntityKind::Function
80        )
81    }
82
83    pub fn is_variable(self) -> bool {
84        matches!(
85            self,
86            EntityKind::Field | EntityKind::Property | EntityKind::Constant | EntityKind::Variable
87        )
88    }
89}