use std::fmt::Display;
use super::{dependencies::Dependencies, handle::Handle, App};
pub trait Module: 'static + Sized {
type Config: 'static + Sized + Clone + PartialEq + std::fmt::Debug = ();
type Dependencies: Dependencies = ();
fn new(config: Self::Config, deps: Self::Dependencies) -> anyhow::Result<Self>;
fn intialize(_handle: Handle<Self>) -> anyhow::Result<()> {
Ok(())
}
}
pub trait MainModule: Module {
fn main(&mut self, app: &App) -> anyhow::Result<()>;
}
#[derive(Debug, Clone, Copy)]
pub struct ModuleId {
type_id: std::any::TypeId,
type_name: &'static str,
}
impl Display for ModuleId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let ty_name = self.type_name.split("::").last().unwrap();
f.write_str(ty_name)
}
}
impl PartialEq for ModuleId {
fn eq(&self, other: &Self) -> bool {
self.type_id == other.type_id
}
}
impl Eq for ModuleId {}
impl std::hash::Hash for ModuleId {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.type_id.hash(state);
}
}
impl ModuleId {
pub fn of<T: Module>() -> Self {
ModuleId {
type_id: std::any::TypeId::of::<T>(),
type_name: std::any::type_name::<T>(),
}
}
}