Skip to main content

model_core/
relation.rs

1use serde::{Deserialize, Serialize};
2
3/// Kind of a relation. Containment is not a relation: it is the entity's
4/// parent.
5#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
6#[serde(rename_all = "snake_case")]
7pub enum RelationKind {
8    Calls,
9    /// Any other use of a symbol (type in a signature, field access...).
10    References,
11    /// Subclassing (`extends`, superclass).
12    Inherits,
13    Implements,
14    Imports,
15    /// Defined by an analysis or a plugin (`http`, `rpc`...).
16    #[serde(untagged)]
17    Other(String),
18}
19
20/// Who asserted a fact: lets analyses weigh syntactic guesses against
21/// compiler-grade SCIP data.
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
23#[serde(rename_all = "snake_case")]
24pub enum Provenance {
25    /// Syntax only (tree-sitter); names may be unresolved.
26    TreeSitter,
27    /// Resolved by a SCIP indexer.
28    Scip,
29    /// A language-specific frontend (hand-written resolution).
30    Frontend,
31    /// Computed from the model by an analysis.
32    Derived,
33}
34
35/// Index of a relation in its [`crate::Model`].
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
37pub struct RelationId(pub(crate) u32);
38
39impl RelationId {
40    pub fn index(self) -> usize {
41        self.0 as usize
42    }
43
44    /// See [`crate::EntityId::from_index`].
45    pub fn from_index(index: u32) -> RelationId {
46        RelationId(index)
47    }
48}
49
50impl RelationKind {
51    /// Serialised name: `calls`, `references`..., or the custom name.
52    pub fn as_str(&self) -> &str {
53        match self {
54            RelationKind::Calls => "calls",
55            RelationKind::References => "references",
56            RelationKind::Inherits => "inherits",
57            RelationKind::Implements => "implements",
58            RelationKind::Imports => "imports",
59            RelationKind::Other(s) => s,
60        }
61    }
62
63    /// Inverse of [`Self::as_str`].
64    pub fn from_name(name: &str) -> RelationKind {
65        match name {
66            "calls" => RelationKind::Calls,
67            "references" => RelationKind::References,
68            "inherits" => RelationKind::Inherits,
69            "implements" => RelationKind::Implements,
70            "imports" => RelationKind::Imports,
71            other => RelationKind::Other(other.to_string()),
72        }
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::RelationKind;
79
80    #[test]
81    fn names_round_trip_as_in_json() {
82        for k in [
83            RelationKind::Calls,
84            RelationKind::References,
85            RelationKind::Inherits,
86            RelationKind::Implements,
87            RelationKind::Imports,
88            RelationKind::Other("http".into()),
89        ] {
90            assert_eq!(RelationKind::from_name(k.as_str()), k);
91            let json = serde_json::to_string(&k).unwrap();
92            assert_eq!(json, format!("\"{}\"", k.as_str()));
93        }
94    }
95}