1use std::sync::Arc;
4
5use serde::Serialize;
6
7use crate::id::{GraphEdgeId, GraphNodeId, GraphReferenceId};
8
9#[derive(Serialize, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
11#[serde(rename_all = "snake_case")]
12pub enum GraphEdgeKind {
13 Call,
15 Import,
17 Implementation,
19 Reference,
21 DependsOn,
23}
24
25#[derive(Serialize, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
30#[serde(rename_all = "snake_case")]
31pub enum GraphCertainty {
32 Resolved,
34 Possible,
36}
37
38#[derive(Serialize, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
43#[serde(rename_all = "snake_case")]
44pub enum GraphDependencyKind {
45 Normal,
47 Development,
49 Build,
51}
52
53#[derive(Serialize, Clone, Debug, PartialEq, Eq)]
55pub struct DependencyEvidence {
56 alias: Arc<str>,
57 kind: GraphDependencyKind,
58 predicate: Option<Arc<str>>,
59}
60
61impl DependencyEvidence {
62 pub(crate) fn new(
63 alias: Arc<str>,
64 kind: GraphDependencyKind,
65 predicate: Option<Arc<str>>,
66 ) -> Self {
67 Self {
68 alias,
69 kind,
70 predicate,
71 }
72 }
73
74 pub fn alias(&self) -> &str {
76 &self.alias
77 }
78
79 pub fn kind(&self) -> GraphDependencyKind {
81 self.kind
82 }
83
84 pub fn predicate(&self) -> Option<&str> {
86 self.predicate.as_deref()
87 }
88}
89
90#[derive(Serialize, Clone, Debug, PartialEq, Eq)]
92#[serde(tag = "kind", rename_all = "snake_case")]
93pub enum GraphEdgeOrigin {
94 Reference {
96 reference: GraphReferenceId,
98 },
99 Dependency {
101 evidence: DependencyEvidence,
103 },
104}
105
106#[derive(Serialize, Clone, Debug, PartialEq, Eq)]
108pub struct GraphEdge {
109 id: GraphEdgeId,
110 source: GraphNodeId,
111 target: GraphNodeId,
112 kind: GraphEdgeKind,
113 certainty: GraphCertainty,
114 origin: GraphEdgeOrigin,
115}
116
117pub(crate) struct EdgeDraft {
119 pub(crate) source: GraphNodeId,
121 pub(crate) target: GraphNodeId,
123 pub(crate) kind: GraphEdgeKind,
125 pub(crate) certainty: GraphCertainty,
127 pub(crate) origin: GraphEdgeOrigin,
129}
130
131impl GraphEdge {
132 pub(crate) fn new(id: GraphEdgeId, draft: EdgeDraft) -> Self {
133 Self {
134 id,
135 source: draft.source,
136 target: draft.target,
137 kind: draft.kind,
138 certainty: draft.certainty,
139 origin: draft.origin,
140 }
141 }
142
143 pub fn id(&self) -> GraphEdgeId {
145 self.id
146 }
147
148 pub fn source(&self) -> GraphNodeId {
150 self.source
151 }
152
153 pub fn target(&self) -> GraphNodeId {
155 self.target
156 }
157
158 pub fn kind(&self) -> GraphEdgeKind {
160 self.kind
161 }
162
163 pub fn certainty(&self) -> GraphCertainty {
165 self.certainty
166 }
167
168 pub fn origin(&self) -> &GraphEdgeOrigin {
170 &self.origin
171 }
172}