use std::any::TypeId;
use crate::{
AutoConfigurationReport, DependencyDescriptor, Diagnostic, ProviderDescriptor, ProviderKind,
ProviderVisibility, SourceLocation,
};
use super::module::ModuleGraph;
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ProviderOrigin {
Provided,
AutoConfiguration,
Service,
Repository,
Provider,
}
impl From<ProviderKind> for ProviderOrigin {
fn from(kind: ProviderKind) -> Self {
match kind {
ProviderKind::Service => Self::Service,
ProviderKind::Repository => Self::Repository,
ProviderKind::Provider => Self::Provider,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProviderState {
Provided,
Preconstructed,
AutoConfigured,
Planned,
}
#[allow(dead_code)]
#[derive(Clone, Copy)]
pub(crate) struct SatisfiedProvider {
pub(crate) type_id: TypeId,
pub(crate) type_name: &'static str,
pub(crate) state: ProviderState,
}
#[allow(dead_code)]
impl SatisfiedProvider {
pub(crate) fn provided<T>() -> Self
where
T: Send + Sync + 'static,
{
Self {
type_id: TypeId::of::<T>(),
type_name: std::any::type_name::<T>(),
state: ProviderState::Provided,
}
}
pub(crate) fn preconstructed<T>() -> Self
where
T: Send + Sync + 'static,
{
Self {
type_id: TypeId::of::<T>(),
type_name: std::any::type_name::<T>(),
state: ProviderState::Preconstructed,
}
}
pub(crate) fn auto_configured(type_id: TypeId, type_name: &'static str) -> Self {
Self {
type_id,
type_name,
state: ProviderState::AutoConfigured,
}
}
}
pub struct ProviderNode {
pub(crate) type_id: TypeId,
pub(crate) type_name: &'static str,
pub(crate) origin: ProviderOrigin,
pub(crate) visibility: ProviderVisibility,
pub(crate) state: ProviderState,
pub(crate) location: Option<SourceLocation>,
pub(crate) declared_dependencies: &'static [DependencyDescriptor],
}
impl ProviderNode {
pub const fn type_name(&self) -> &str {
self.type_name
}
pub const fn origin(&self) -> ProviderOrigin {
self.origin
}
pub const fn visibility(&self) -> ProviderVisibility {
self.visibility
}
pub const fn state(&self) -> ProviderState {
self.state
}
pub const fn location(&self) -> Option<SourceLocation> {
self.location
}
pub const fn declared_dependencies(&self) -> &[DependencyDescriptor] {
self.declared_dependencies
}
}
pub struct DependencyEdge {
#[allow(dead_code)]
pub(crate) provider_type_id: TypeId,
pub(crate) provider_type_name: &'static str,
#[allow(dead_code)]
pub(crate) dependency_type_id: TypeId,
pub(crate) dependency_type_name: &'static str,
}
impl DependencyEdge {
pub const fn provider_type_name(&self) -> &str {
self.provider_type_name
}
pub const fn dependency_type_name(&self) -> &str {
self.dependency_type_name
}
}
pub struct ApplicationGraph {
pub(crate) providers: Vec<ProviderNode>,
pub(crate) dependencies: Vec<DependencyEdge>,
}
impl ApplicationGraph {
pub fn providers(&self) -> &[ProviderNode] {
&self.providers
}
pub fn dependencies(&self) -> &[DependencyEdge] {
&self.dependencies
}
pub fn provider<T>(&self) -> Option<&ProviderNode>
where
T: Send + Sync + 'static,
{
let type_id = TypeId::of::<T>();
self.providers
.iter()
.find(|provider| provider.type_id == type_id)
}
}
pub struct ConstructionStep {
#[allow(dead_code)]
pub(crate) type_id: TypeId,
pub(crate) type_name: &'static str,
pub(crate) origin: ProviderOrigin,
pub(crate) location: SourceLocation,
#[allow(dead_code)]
pub(crate) descriptor: &'static ProviderDescriptor,
}
impl ConstructionStep {
pub(crate) const fn type_id(&self) -> TypeId {
self.type_id
}
pub(crate) const fn descriptor(&self) -> &'static ProviderDescriptor {
self.descriptor
}
#[doc(hidden)]
pub const fn __descriptor(&self) -> &'static ProviderDescriptor {
self.descriptor
}
pub const fn type_name(&self) -> &str {
self.type_name
}
pub const fn origin(&self) -> ProviderOrigin {
self.origin
}
pub const fn location(&self) -> SourceLocation {
self.location
}
}
pub struct ConstructionPlan {
pub(crate) steps: Vec<ConstructionStep>,
}
impl ConstructionPlan {
pub fn steps(&self) -> &[ConstructionStep] {
&self.steps
}
}
pub struct GraphAnalysis {
pub(crate) graph: ApplicationGraph,
pub(crate) diagnostics: Vec<Diagnostic>,
pub(crate) construction_plan: Option<ConstructionPlan>,
pub(crate) auto_configurations: Vec<AutoConfigurationReport>,
pub(crate) module_graph: Option<ModuleGraph>,
}
impl GraphAnalysis {
pub const fn graph(&self) -> &ApplicationGraph {
&self.graph
}
pub fn diagnostics(&self) -> &[Diagnostic] {
&self.diagnostics
}
pub fn auto_configurations(&self) -> &[AutoConfigurationReport] {
&self.auto_configurations
}
pub const fn module_graph(&self) -> Option<&ModuleGraph> {
self.module_graph.as_ref()
}
pub const fn construction_plan(&self) -> Option<&ConstructionPlan> {
self.construction_plan.as_ref()
}
pub fn is_valid(&self) -> bool {
self.diagnostics.is_empty() && self.construction_plan.is_some()
}
pub(crate) fn prepend_diagnostics(&mut self, mut diagnostics: Vec<Diagnostic>) {
if diagnostics.is_empty() {
return;
}
self.construction_plan = None;
diagnostics.append(&mut self.diagnostics);
self.diagnostics = diagnostics;
}
pub(crate) fn append_diagnostics(&mut self, diagnostics: Vec<Diagnostic>) {
if diagnostics.is_empty() {
return;
}
self.construction_plan = None;
self.diagnostics.extend(diagnostics);
}
pub(crate) fn set_module_graph(&mut self, module_graph: ModuleGraph) {
self.module_graph = Some(module_graph);
}
pub(crate) fn invalid(diagnostics: Vec<Diagnostic>) -> Self {
Self {
graph: ApplicationGraph {
providers: Vec::new(),
dependencies: Vec::new(),
},
diagnostics,
construction_plan: None,
auto_configurations: Vec::new(),
module_graph: None,
}
}
pub(crate) fn into_valid_parts(
self,
) -> crate::Result<(
ApplicationGraph,
ConstructionPlan,
Vec<AutoConfigurationReport>,
Option<ModuleGraph>,
)> {
match (self.diagnostics.is_empty(), self.construction_plan) {
(true, Some(plan)) => Ok((
self.graph,
plan,
self.auto_configurations,
self.module_graph,
)),
(false, _) => {
let mut diagnostics = self.diagnostics.into_iter();
let primary = diagnostics
.next()
.expect("invalid analysis has diagnostics");
Err(crate::Error::from_diagnostics(primary, diagnostics))
}
(true, None) => unreachable!("valid analysis always has a plan"),
}
}
}
#[cfg(test)]
mod tests {
use std::any::TypeId;
use std::sync::Arc;
use crate::{
ConstructionContext, ErasedProvider, ProviderDescriptor, ProviderFuture, ProviderKind,
ProviderVisibility, SourceLocation,
};
use super::*;
struct Database;
struct Repository;
#[test]
fn immutable_graph_types_expose_stable_metadata() {
let database = ProviderNode {
type_id: TypeId::of::<Database>(),
type_name: "graph::Database",
origin: ProviderOrigin::Provided,
visibility: ProviderVisibility::Public,
state: ProviderState::Provided,
location: None,
declared_dependencies: &[],
};
let graph = ApplicationGraph {
providers: vec![database],
dependencies: Vec::new(),
};
let analysis = GraphAnalysis {
graph,
diagnostics: Vec::new(),
construction_plan: Some(ConstructionPlan { steps: Vec::new() }),
auto_configurations: Vec::new(),
module_graph: None,
};
assert!(analysis.is_valid());
assert_eq!(analysis.graph().providers().len(), 1);
assert_eq!(
analysis.graph().provider::<Database>().unwrap().origin(),
ProviderOrigin::Provided,
);
assert_eq!(
analysis
.graph()
.provider::<Database>()
.unwrap()
.visibility(),
ProviderVisibility::Public,
);
assert!(analysis.construction_plan().unwrap().steps().is_empty());
}
fn repository_type_id() -> TypeId {
TypeId::of::<Repository>()
}
fn repository_constructor<'a>(_: &'a ConstructionContext<'a>) -> ProviderFuture<'a> {
Box::pin(async { Ok(Arc::new(Repository) as ErasedProvider) })
}
static REPOSITORY_DESCRIPTOR: ProviderDescriptor = ProviderDescriptor::new(
ProviderKind::Repository,
"graph::Repository",
repository_type_id,
&[],
ProviderVisibility::Private,
SourceLocation::new("repository.rs", 3, 1),
repository_constructor,
);
#[test]
fn edges_and_steps_expose_stable_metadata() {
let edge = DependencyEdge {
provider_type_id: TypeId::of::<Repository>(),
provider_type_name: "graph::Repository",
dependency_type_id: TypeId::of::<Database>(),
dependency_type_name: "graph::Database",
};
let step = ConstructionStep {
type_id: TypeId::of::<Repository>(),
type_name: "graph::Repository",
origin: ProviderOrigin::Repository,
location: SourceLocation::new("repository.rs", 3, 1),
descriptor: &REPOSITORY_DESCRIPTOR,
};
assert_eq!(edge.provider_type_name(), "graph::Repository");
assert_eq!(edge.dependency_type_name(), "graph::Database");
assert_eq!(step.type_name(), "graph::Repository");
assert_eq!(step.origin(), ProviderOrigin::Repository);
assert_eq!(step.location(), SourceLocation::new("repository.rs", 3, 1));
}
}