1use std::sync::Arc;
4
5use pedant_types::ResolutionCertainty;
6use serde::Serialize;
7
8use crate::id::{GraphEdgeId, GraphNodeId, GraphReferenceId};
9use crate::reference::GraphReferenceKind;
10
11#[derive(Serialize, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
13#[serde(rename_all = "snake_case")]
14pub enum GraphEdgeKind {
15 Call,
17 Import,
19 Implementation,
21 Reference,
23 DependsOn,
25}
26
27impl GraphEdgeKind {
28 pub(crate) fn of_reference(kind: GraphReferenceKind) -> Self {
34 match kind {
35 GraphReferenceKind::Call => Self::Call,
36 GraphReferenceKind::Import => Self::Import,
37 GraphReferenceKind::Implementation => Self::Implementation,
38 GraphReferenceKind::Reference => Self::Reference,
39 }
40 }
41}
42
43#[derive(Serialize, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
48#[serde(rename_all = "snake_case")]
49pub enum GraphCertainty {
50 Resolved,
52 Possible,
54}
55
56impl GraphCertainty {
57 pub(crate) fn of(certainty: ResolutionCertainty) -> Self {
62 match certainty {
63 ResolutionCertainty::Resolved => Self::Resolved,
64 ResolutionCertainty::Possible => Self::Possible,
65 }
66 }
67}
68
69#[derive(Serialize, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
74#[serde(rename_all = "snake_case")]
75pub enum GraphDependencyKind {
76 Normal,
78 Development,
80 Build,
82}
83
84#[derive(Serialize, Clone, Debug, PartialEq, Eq)]
86pub struct DependencyEvidence {
87 alias: Arc<str>,
88 kind: GraphDependencyKind,
89 predicate: Option<Arc<str>>,
90}
91
92impl DependencyEvidence {
93 pub(crate) fn new(
94 alias: Arc<str>,
95 kind: GraphDependencyKind,
96 predicate: Option<Arc<str>>,
97 ) -> Self {
98 Self {
99 alias,
100 kind,
101 predicate,
102 }
103 }
104
105 pub fn alias(&self) -> &str {
107 &self.alias
108 }
109
110 pub fn kind(&self) -> GraphDependencyKind {
112 self.kind
113 }
114
115 pub fn predicate(&self) -> Option<&str> {
117 self.predicate.as_deref()
118 }
119}
120
121#[derive(Serialize, Clone, Debug, PartialEq, Eq)]
123#[serde(tag = "kind", rename_all = "snake_case")]
124pub enum GraphEdgeOrigin {
125 Reference {
127 reference: GraphReferenceId,
129 },
130 Dependency {
132 evidence: DependencyEvidence,
134 },
135}
136
137#[derive(Serialize, Clone, Debug, PartialEq, Eq)]
139pub struct GraphEdge {
140 id: GraphEdgeId,
141 source: GraphNodeId,
142 target: GraphNodeId,
143 kind: GraphEdgeKind,
144 certainty: GraphCertainty,
145 origin: GraphEdgeOrigin,
146}
147
148pub(crate) struct EdgeDraft {
150 pub(crate) source: GraphNodeId,
152 pub(crate) target: GraphNodeId,
154 pub(crate) kind: GraphEdgeKind,
156 pub(crate) certainty: GraphCertainty,
158 pub(crate) origin: GraphEdgeOrigin,
160}
161
162impl GraphEdge {
163 pub(crate) fn new(id: GraphEdgeId, draft: EdgeDraft) -> Self {
164 Self {
165 id,
166 source: draft.source,
167 target: draft.target,
168 kind: draft.kind,
169 certainty: draft.certainty,
170 origin: draft.origin,
171 }
172 }
173
174 pub fn id(&self) -> GraphEdgeId {
176 self.id
177 }
178
179 pub fn source(&self) -> GraphNodeId {
181 self.source
182 }
183
184 pub fn target(&self) -> GraphNodeId {
186 self.target
187 }
188
189 pub fn kind(&self) -> GraphEdgeKind {
191 self.kind
192 }
193
194 pub fn certainty(&self) -> GraphCertainty {
196 self.certainty
197 }
198
199 pub fn origin(&self) -> &GraphEdgeOrigin {
201 &self.origin
202 }
203}