1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
6#[serde(rename_all = "snake_case")]
7pub enum RelationKind {
8 Calls,
9 References,
11 Inherits,
13 Implements,
14 Imports,
15 #[serde(untagged)]
17 Other(String),
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
23#[serde(rename_all = "snake_case")]
24pub enum Provenance {
25 TreeSitter,
27 Scip,
29 Frontend,
31 Derived,
33}
34
35#[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 pub fn from_index(index: u32) -> RelationId {
46 RelationId(index)
47 }
48}
49
50impl RelationKind {
51 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 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}