use std::{any::Any, fmt, path::Path, sync::Arc};
use serde::de::DeserializeOwned;
use super::{ContextEntry, Error, IndexedFiles, Metadata, MetadataContext};
use crate::RenderCtx;
type Erased = dyn Any + Send + Sync;
pub(crate) trait Domain: fmt::Debug + Send + Sync + 'static {
type Config: DeserializeOwned + fmt::Debug + Send + Sync + 'static;
type State: Default + fmt::Debug + Send + Sync + 'static;
const NAME: &'static str;
fn initialize(
_config: &Self::Config,
_root: &Path,
_files: &IndexedFiles,
) -> Result<Self::State, Error> {
Ok(Self::State::default())
}
fn manifests(
_config: &Self::Config,
_state: &Self::State,
_root: &Path,
_files: &IndexedFiles,
) -> Result<Vec<std::path::PathBuf>, Error> {
Ok(Vec::new())
}
fn is_source_input(
_config: &Self::Config,
_state: &Self::State,
_root: &Path,
_path: &Path,
) -> bool {
false
}
fn metadata(
_config: &Self::Config,
_state: &Self::State,
_context: &MetadataContext<'_>,
) -> Result<Option<Metadata>, Error> {
Ok(None)
}
fn context(
_config: &Self::Config,
_state: &Self::State,
_context: &RenderCtx<'_>,
) -> Result<Vec<ContextEntry>, Error> {
Ok(Vec::new())
}
fn source_description(
_config: &Self::Config,
_state: &Self::State,
_path: &Path,
_source: &str,
) -> Option<String> {
None
}
fn namespaces(_config: &Self::Config) -> Vec<Box<str>> {
Vec::new()
}
}
#[derive(Clone)]
pub(crate) struct ConfiguredDomain {
registration: &'static Registration,
config: Arc<Erased>,
}
impl ConfiguredDomain {
pub(crate) fn parse(
registration: &'static Registration,
value: toml::Value,
) -> Result<Self, Error> {
Ok(Self {
registration,
config: Arc::from((registration.parse)(value)?),
})
}
#[must_use]
pub(crate) const fn name(&self) -> &'static str {
self.registration.name
}
pub(crate) fn namespaces(&self) -> Vec<Box<str>> {
(self.registration.namespaces)(&*self.config)
}
}
impl fmt::Debug for ConfiguredDomain {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("ConfiguredDomain")
.field("name", &self.name())
.finish_non_exhaustive()
}
}
pub(crate) struct Runtime {
registration: &'static Registration,
config: Arc<Erased>,
state: Box<Erased>,
}
impl Runtime {
pub(crate) fn initialize(
configured: &ConfiguredDomain,
root: &Path,
files: &IndexedFiles,
) -> Result<Self, Error> {
let state = (configured.registration.initialize)(&*configured.config, root, files)?;
Ok(Self {
registration: configured.registration,
config: Arc::clone(&configured.config),
state,
})
}
#[must_use]
pub(crate) const fn name(&self) -> &'static str {
self.registration.name
}
pub(crate) fn manifests(
&self,
root: &Path,
files: &IndexedFiles,
) -> Result<Vec<std::path::PathBuf>, Error> {
(self.registration.manifests)(&*self.config, &*self.state, root, files)
}
pub(crate) fn is_source_input(&self, root: &Path, path: &Path) -> bool {
(self.registration.is_source_input)(&*self.config, &*self.state, root, path)
}
pub(crate) fn metadata(
&self,
context: &MetadataContext<'_>,
) -> Result<Option<Metadata>, Error> {
(self.registration.metadata)(&*self.config, &*self.state, context)
}
pub(crate) fn context(&self, context: &RenderCtx<'_>) -> Result<Vec<ContextEntry>, Error> {
(self.registration.context)(&*self.config, &*self.state, context)
}
pub(crate) fn source_description(&self, path: &Path, source: &str) -> Option<String> {
(self.registration.source_description)(&*self.config, &*self.state, path, source)
}
}
impl fmt::Debug for Runtime {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Runtime")
.field("name", &self.name())
.finish_non_exhaustive()
}
}
pub(crate) struct Registration {
pub(crate) name: &'static str,
parse: fn(toml::Value) -> Result<Box<Erased>, Error>,
initialize: fn(&Erased, &Path, &IndexedFiles) -> Result<Box<Erased>, Error>,
manifests: fn(&Erased, &Erased, &Path, &IndexedFiles) -> Result<Vec<std::path::PathBuf>, Error>,
is_source_input: fn(&Erased, &Erased, &Path, &Path) -> bool,
metadata: fn(&Erased, &Erased, &MetadataContext<'_>) -> Result<Option<Metadata>, Error>,
context: fn(&Erased, &Erased, &RenderCtx<'_>) -> Result<Vec<ContextEntry>, Error>,
source_description: fn(&Erased, &Erased, &Path, &str) -> Option<String>,
namespaces: fn(&Erased) -> Vec<Box<str>>,
}
impl Registration {
pub(crate) const fn new<D>() -> Self
where
D: Domain,
{
Self {
name: D::NAME,
parse: parse::<D>,
initialize: initialize::<D>,
manifests: manifests::<D>,
is_source_input: is_source_input::<D>,
metadata: metadata::<D>,
context: context::<D>,
source_description: source_description::<D>,
namespaces: namespaces::<D>,
}
}
}
inventory::collect!(Registration);
pub(crate) fn registrations() -> impl Iterator<Item = &'static Registration> {
inventory::iter::<Registration>.into_iter()
}
fn parse<D>(value: toml::Value) -> Result<Box<Erased>, Error>
where
D: Domain,
{
value
.try_into::<D::Config>()
.map(|config| Box::new(config) as Box<Erased>)
.map_err(|source| Error::parse(crate::layout::CONFIG, D::NAME, source))
}
fn initialize<D>(config: &Erased, root: &Path, files: &IndexedFiles) -> Result<Box<Erased>, Error>
where
D: Domain,
{
D::initialize(config_of::<D>(config), root, files).map(|state| Box::new(state) as Box<Erased>)
}
fn manifests<D>(
config: &Erased,
state: &Erased,
root: &Path,
files: &IndexedFiles,
) -> Result<Vec<std::path::PathBuf>, Error>
where
D: Domain,
{
D::manifests(config_of::<D>(config), state_of::<D>(state), root, files)
}
fn is_source_input<D>(config: &Erased, state: &Erased, root: &Path, path: &Path) -> bool
where
D: Domain,
{
D::is_source_input(config_of::<D>(config), state_of::<D>(state), root, path)
}
fn metadata<D>(
config: &Erased,
state: &Erased,
context: &MetadataContext<'_>,
) -> Result<Option<Metadata>, Error>
where
D: Domain,
{
D::metadata(config_of::<D>(config), state_of::<D>(state), context)
}
fn context<D>(
config: &Erased,
state: &Erased,
context: &RenderCtx<'_>,
) -> Result<Vec<ContextEntry>, Error>
where
D: Domain,
{
D::context(config_of::<D>(config), state_of::<D>(state), context)
}
fn source_description<D>(
config: &Erased,
state: &Erased,
path: &Path,
source: &str,
) -> Option<String>
where
D: Domain,
{
D::source_description(config_of::<D>(config), state_of::<D>(state), path, source)
}
fn namespaces<D>(config: &Erased) -> Vec<Box<str>>
where
D: Domain,
{
D::namespaces(config_of::<D>(config))
}
fn config_of<D>(config: &Erased) -> &D::Config
where
D: Domain,
{
config
.downcast_ref()
.expect("domain registration must retain its config type")
}
fn state_of<D>(state: &Erased) -> &D::State
where
D: Domain,
{
state
.downcast_ref()
.expect("domain registration must retain its state type")
}
macro_rules! register_domain {
($domain:ty) => {
inventory::submit! {
crate::domain::registry::Registration::new::<$domain>()
}
};
}
pub(crate) use register_domain;