1use std::any::TypeId;
4
5use crate::{
6 AutoConfigurationReport, DependencyDescriptor, Diagnostic, ProviderDescriptor, ProviderKind,
7 ProviderVisibility, SourceLocation,
8};
9
10use super::module::ModuleGraph;
11
12#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
14pub enum ProviderOrigin {
15 Provided,
17 AutoConfiguration,
19 Service,
21 Repository,
23 Provider,
25}
26
27impl From<ProviderKind> for ProviderOrigin {
28 fn from(kind: ProviderKind) -> Self {
29 match kind {
30 ProviderKind::Service => Self::Service,
31 ProviderKind::Repository => Self::Repository,
32 ProviderKind::Provider => Self::Provider,
33 }
34 }
35}
36
37#[derive(Clone, Copy, Debug, Eq, PartialEq)]
39pub enum ProviderState {
40 Provided,
42 Preconstructed,
44 AutoConfigured,
46 Planned,
48}
49
50#[allow(dead_code)]
51#[derive(Clone, Copy)]
52pub(crate) struct SatisfiedProvider {
53 pub(crate) type_id: TypeId,
54 pub(crate) type_name: &'static str,
55 pub(crate) state: ProviderState,
56}
57
58#[allow(dead_code)]
59impl SatisfiedProvider {
60 pub(crate) fn provided<T>() -> Self
61 where
62 T: Send + Sync + 'static,
63 {
64 Self {
65 type_id: TypeId::of::<T>(),
66 type_name: std::any::type_name::<T>(),
67 state: ProviderState::Provided,
68 }
69 }
70
71 pub(crate) fn preconstructed<T>() -> Self
72 where
73 T: Send + Sync + 'static,
74 {
75 Self {
76 type_id: TypeId::of::<T>(),
77 type_name: std::any::type_name::<T>(),
78 state: ProviderState::Preconstructed,
79 }
80 }
81
82 pub(crate) fn auto_configured(type_id: TypeId, type_name: &'static str) -> Self {
83 Self {
84 type_id,
85 type_name,
86 state: ProviderState::AutoConfigured,
87 }
88 }
89}
90
91pub struct ProviderNode {
93 pub(crate) type_id: TypeId,
94 pub(crate) type_name: &'static str,
95 pub(crate) origin: ProviderOrigin,
96 pub(crate) visibility: ProviderVisibility,
97 pub(crate) state: ProviderState,
98 pub(crate) location: Option<SourceLocation>,
99 pub(crate) declared_dependencies: &'static [DependencyDescriptor],
100}
101
102impl ProviderNode {
103 pub const fn type_name(&self) -> &str {
105 self.type_name
106 }
107
108 pub const fn origin(&self) -> ProviderOrigin {
110 self.origin
111 }
112
113 pub const fn visibility(&self) -> ProviderVisibility {
115 self.visibility
116 }
117
118 pub const fn state(&self) -> ProviderState {
120 self.state
121 }
122
123 pub const fn location(&self) -> Option<SourceLocation> {
125 self.location
126 }
127
128 pub const fn declared_dependencies(&self) -> &[DependencyDescriptor] {
130 self.declared_dependencies
131 }
132}
133
134pub struct DependencyEdge {
136 #[allow(dead_code)]
137 pub(crate) provider_type_id: TypeId,
138 pub(crate) provider_type_name: &'static str,
139 #[allow(dead_code)]
140 pub(crate) dependency_type_id: TypeId,
141 pub(crate) dependency_type_name: &'static str,
142}
143
144impl DependencyEdge {
145 pub const fn provider_type_name(&self) -> &str {
147 self.provider_type_name
148 }
149
150 pub const fn dependency_type_name(&self) -> &str {
152 self.dependency_type_name
153 }
154}
155
156pub struct ApplicationGraph {
158 pub(crate) providers: Vec<ProviderNode>,
159 pub(crate) dependencies: Vec<DependencyEdge>,
160}
161
162impl ApplicationGraph {
163 pub fn providers(&self) -> &[ProviderNode] {
165 &self.providers
166 }
167
168 pub fn dependencies(&self) -> &[DependencyEdge] {
170 &self.dependencies
171 }
172
173 pub fn provider<T>(&self) -> Option<&ProviderNode>
175 where
176 T: Send + Sync + 'static,
177 {
178 let type_id = TypeId::of::<T>();
179 self.providers
180 .iter()
181 .find(|provider| provider.type_id == type_id)
182 }
183}
184
185pub struct ConstructionStep {
187 #[allow(dead_code)]
188 pub(crate) type_id: TypeId,
189 pub(crate) type_name: &'static str,
190 pub(crate) origin: ProviderOrigin,
191 pub(crate) location: SourceLocation,
192 #[allow(dead_code)]
193 pub(crate) descriptor: &'static ProviderDescriptor,
194}
195
196impl ConstructionStep {
197 pub(crate) const fn type_id(&self) -> TypeId {
198 self.type_id
199 }
200
201 pub(crate) const fn descriptor(&self) -> &'static ProviderDescriptor {
202 self.descriptor
203 }
204
205 #[doc(hidden)]
207 pub const fn __descriptor(&self) -> &'static ProviderDescriptor {
208 self.descriptor
209 }
210
211 pub const fn type_name(&self) -> &str {
213 self.type_name
214 }
215
216 pub const fn origin(&self) -> ProviderOrigin {
218 self.origin
219 }
220
221 pub const fn location(&self) -> SourceLocation {
223 self.location
224 }
225}
226
227pub struct ConstructionPlan {
229 pub(crate) steps: Vec<ConstructionStep>,
230}
231
232impl ConstructionPlan {
233 pub fn steps(&self) -> &[ConstructionStep] {
235 &self.steps
236 }
237}
238
239pub struct GraphAnalysis {
241 pub(crate) graph: ApplicationGraph,
242 pub(crate) diagnostics: Vec<Diagnostic>,
243 pub(crate) construction_plan: Option<ConstructionPlan>,
244 pub(crate) auto_configurations: Vec<AutoConfigurationReport>,
245 pub(crate) module_graph: Option<ModuleGraph>,
246}
247
248impl GraphAnalysis {
249 pub const fn graph(&self) -> &ApplicationGraph {
251 &self.graph
252 }
253
254 pub fn diagnostics(&self) -> &[Diagnostic] {
256 &self.diagnostics
257 }
258
259 pub fn auto_configurations(&self) -> &[AutoConfigurationReport] {
261 &self.auto_configurations
262 }
263
264 pub const fn module_graph(&self) -> Option<&ModuleGraph> {
266 self.module_graph.as_ref()
267 }
268
269 pub const fn construction_plan(&self) -> Option<&ConstructionPlan> {
271 self.construction_plan.as_ref()
272 }
273
274 pub fn is_valid(&self) -> bool {
276 self.diagnostics.is_empty() && self.construction_plan.is_some()
277 }
278
279 pub(crate) fn prepend_diagnostics(&mut self, mut diagnostics: Vec<Diagnostic>) {
280 if diagnostics.is_empty() {
281 return;
282 }
283 self.construction_plan = None;
284 diagnostics.append(&mut self.diagnostics);
285 self.diagnostics = diagnostics;
286 }
287
288 pub(crate) fn append_diagnostics(&mut self, diagnostics: Vec<Diagnostic>) {
289 if diagnostics.is_empty() {
290 return;
291 }
292 self.construction_plan = None;
293 self.diagnostics.extend(diagnostics);
294 }
295
296 pub(crate) fn set_module_graph(&mut self, module_graph: ModuleGraph) {
297 self.module_graph = Some(module_graph);
298 }
299
300 pub(crate) fn invalid(diagnostics: Vec<Diagnostic>) -> Self {
301 Self {
302 graph: ApplicationGraph {
303 providers: Vec::new(),
304 dependencies: Vec::new(),
305 },
306 diagnostics,
307 construction_plan: None,
308 auto_configurations: Vec::new(),
309 module_graph: None,
310 }
311 }
312
313 pub(crate) fn into_valid_parts(
314 self,
315 ) -> crate::Result<(
316 ApplicationGraph,
317 ConstructionPlan,
318 Vec<AutoConfigurationReport>,
319 Option<ModuleGraph>,
320 )> {
321 match (self.diagnostics.is_empty(), self.construction_plan) {
322 (true, Some(plan)) => Ok((
323 self.graph,
324 plan,
325 self.auto_configurations,
326 self.module_graph,
327 )),
328 (false, _) => {
329 let mut diagnostics = self.diagnostics.into_iter();
330 let primary = diagnostics
331 .next()
332 .expect("invalid analysis has diagnostics");
333 Err(crate::Error::from_diagnostics(primary, diagnostics))
334 }
335 (true, None) => unreachable!("valid analysis always has a plan"),
336 }
337 }
338}
339
340#[cfg(test)]
341mod tests {
342 use std::any::TypeId;
343 use std::sync::Arc;
344
345 use crate::{
346 ConstructionContext, ErasedProvider, ProviderDescriptor, ProviderFuture, ProviderKind,
347 ProviderVisibility, SourceLocation,
348 };
349
350 use super::*;
351
352 struct Database;
353 struct Repository;
354
355 #[test]
356 fn immutable_graph_types_expose_stable_metadata() {
357 let database = ProviderNode {
358 type_id: TypeId::of::<Database>(),
359 type_name: "graph::Database",
360 origin: ProviderOrigin::Provided,
361 visibility: ProviderVisibility::Public,
362 state: ProviderState::Provided,
363 location: None,
364 declared_dependencies: &[],
365 };
366 let graph = ApplicationGraph {
367 providers: vec![database],
368 dependencies: Vec::new(),
369 };
370 let analysis = GraphAnalysis {
371 graph,
372 diagnostics: Vec::new(),
373 construction_plan: Some(ConstructionPlan { steps: Vec::new() }),
374 auto_configurations: Vec::new(),
375 module_graph: None,
376 };
377
378 assert!(analysis.is_valid());
379 assert_eq!(analysis.graph().providers().len(), 1);
380 assert_eq!(
381 analysis.graph().provider::<Database>().unwrap().origin(),
382 ProviderOrigin::Provided,
383 );
384 assert_eq!(
385 analysis
386 .graph()
387 .provider::<Database>()
388 .unwrap()
389 .visibility(),
390 ProviderVisibility::Public,
391 );
392 assert!(analysis.construction_plan().unwrap().steps().is_empty());
393 }
394
395 fn repository_type_id() -> TypeId {
396 TypeId::of::<Repository>()
397 }
398
399 fn repository_constructor<'a>(_: &'a ConstructionContext<'a>) -> ProviderFuture<'a> {
400 Box::pin(async { Ok(Arc::new(Repository) as ErasedProvider) })
401 }
402
403 static REPOSITORY_DESCRIPTOR: ProviderDescriptor = ProviderDescriptor::new(
404 ProviderKind::Repository,
405 "graph::Repository",
406 repository_type_id,
407 &[],
408 ProviderVisibility::Private,
409 SourceLocation::new("repository.rs", 3, 1),
410 repository_constructor,
411 );
412
413 #[test]
414 fn edges_and_steps_expose_stable_metadata() {
415 let edge = DependencyEdge {
416 provider_type_id: TypeId::of::<Repository>(),
417 provider_type_name: "graph::Repository",
418 dependency_type_id: TypeId::of::<Database>(),
419 dependency_type_name: "graph::Database",
420 };
421 let step = ConstructionStep {
422 type_id: TypeId::of::<Repository>(),
423 type_name: "graph::Repository",
424 origin: ProviderOrigin::Repository,
425 location: SourceLocation::new("repository.rs", 3, 1),
426 descriptor: &REPOSITORY_DESCRIPTOR,
427 };
428
429 assert_eq!(edge.provider_type_name(), "graph::Repository");
430 assert_eq!(edge.dependency_type_name(), "graph::Database");
431 assert_eq!(step.type_name(), "graph::Repository");
432 assert_eq!(step.origin(), ProviderOrigin::Repository);
433 assert_eq!(step.location(), SourceLocation::new("repository.rs", 3, 1));
434 }
435}