dipper 0.5.3

An out-of-the-box modular dependency injection web application framework.
Documentation
use std::{cell::Cell, rc::Rc, sync::Arc};

use salvo::prelude::*;

use super::{Routable, UnroutableScribe};
use crate::dyn_mod::manifest::Manifest;

pub trait Depends {
    fn depends(&self) -> Vec<Manifest>;
}

#[derive(Clone)]
pub struct DmRouter {
    inner: Rc<Cell<Router>>,
    unroutable_scribe: Option<Arc<dyn UnroutableScribe>>,
}

impl DmRouter {
    pub(crate) fn new(hoops: Vec<Arc<dyn Handler>>, unroutable_scribe: Arc<dyn UnroutableScribe>) -> Self {
        let mut router = Router::new();
        router.hoops = hoops;
        Self {
            inner: Rc::new(Cell::new(router)),
            unroutable_scribe: Some(unroutable_scribe),
        }
    }

    /// Take the inner [`salvo::routing::Router`]
    pub(crate) fn into_inner(self) -> Router {
        let mut inner = self.inner.take();
        inner.hoops = Vec::new();
        inner
    }

    pub(crate) fn push(&self, manifest: Arc<Manifest>, mut router: Router) {
        let inner = self.inner.take();
        router = router.hoop(Routable::new(manifest, self.unroutable_scribe.clone()));
        router.hoops.append(&mut inner.hoops.clone());
        self.inner.set(inner.push(router));
    }
}