use std::any::TypeId;
use std::future::Future;
use std::pin::Pin;
use crate::{ConstructionContext, ErasedProvider, Result, SourceLocation};
pub trait Module: Send + Sync + 'static {}
pub struct ModuleImportDescriptor {
type_name: &'static str,
type_id: fn() -> TypeId,
}
impl ModuleImportDescriptor {
pub const fn new(type_name: &'static str, type_id: fn() -> TypeId) -> Self {
Self { type_name, type_id }
}
pub const fn type_name(&self) -> &'static str {
self.type_name
}
pub fn type_id(&self) -> TypeId {
(self.type_id)()
}
}
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ProviderKind {
Service,
Repository,
Provider,
}
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ProviderVisibility {
Public,
Private,
}
pub type ProviderFuture<'a> = Pin<Box<dyn Future<Output = Result<ErasedProvider>> + Send + 'a>>;
pub type ProviderConstructor = for<'a> fn(&'a ConstructionContext<'a>) -> ProviderFuture<'a>;
pub struct DependencyDescriptor {
type_name: &'static str,
type_id: fn() -> TypeId,
}
impl DependencyDescriptor {
pub const fn new(type_name: &'static str, type_id: fn() -> TypeId) -> Self {
Self { type_name, type_id }
}
pub const fn type_name(&self) -> &'static str {
self.type_name
}
pub fn type_id(&self) -> TypeId {
(self.type_id)()
}
}
pub struct ProviderDescriptor {
kind: ProviderKind,
type_name: &'static str,
type_id: fn() -> TypeId,
runtime_type_name: Option<fn() -> &'static str>,
namespace: Option<&'static str>,
dependencies: &'static [DependencyDescriptor],
visibility: ProviderVisibility,
location: SourceLocation,
constructor: ProviderConstructor,
}
impl ProviderDescriptor {
pub const fn new(
kind: ProviderKind,
type_name: &'static str,
type_id: fn() -> TypeId,
dependencies: &'static [DependencyDescriptor],
visibility: ProviderVisibility,
location: SourceLocation,
constructor: ProviderConstructor,
) -> Self {
Self {
kind,
type_name,
type_id,
runtime_type_name: None,
namespace: None,
dependencies,
visibility,
location,
constructor,
}
}
#[doc(hidden)]
pub const fn with_runtime_type_name(mut self, runtime_type_name: fn() -> &'static str) -> Self {
self.runtime_type_name = Some(runtime_type_name);
self
}
#[must_use]
pub const fn with_namespace(mut self, namespace: &'static str) -> Self {
self.namespace = Some(namespace);
self
}
pub const fn kind(&self) -> ProviderKind {
self.kind
}
pub const fn type_name(&self) -> &'static str {
self.type_name
}
pub fn type_id(&self) -> TypeId {
(self.type_id)()
}
#[doc(hidden)]
pub fn runtime_type_name(&self) -> Option<&'static str> {
self.runtime_type_name
.map(|runtime_type_name| runtime_type_name())
}
pub const fn namespace(&self) -> Option<&'static str> {
self.namespace
}
pub const fn dependencies(&self) -> &'static [DependencyDescriptor] {
self.dependencies
}
pub const fn visibility(&self) -> ProviderVisibility {
self.visibility
}
pub const fn location(&self) -> SourceLocation {
self.location
}
pub const fn constructor(&self) -> ProviderConstructor {
self.constructor
}
}
pub struct ModuleDescriptor {
type_name: &'static str,
type_id: fn() -> TypeId,
namespace: Option<&'static str>,
imports: &'static [ModuleImportDescriptor],
location: SourceLocation,
}
impl ModuleDescriptor {
pub const fn new(
type_name: &'static str,
type_id: fn() -> TypeId,
location: SourceLocation,
) -> Self {
Self {
type_name,
type_id,
namespace: None,
imports: &[],
location,
}
}
#[must_use]
pub const fn with_namespace(mut self, namespace: &'static str) -> Self {
self.namespace = Some(namespace);
self
}
#[must_use]
pub const fn with_imports(mut self, imports: &'static [ModuleImportDescriptor]) -> Self {
self.imports = imports;
self
}
pub const fn type_name(&self) -> &'static str {
self.type_name
}
pub fn type_id(&self) -> TypeId {
(self.type_id)()
}
pub const fn namespace(&self) -> Option<&'static str> {
self.namespace
}
pub const fn imports(&self) -> &'static [ModuleImportDescriptor] {
self.imports
}
pub const fn location(&self) -> SourceLocation {
self.location
}
}