laravel-rust 0.1.0

Laravel for Rust: a full-stack Rust web framework, inspired by PHP Laravel
Documentation
use super::__Registration;
use super::store::Store;
use crate::console::{Console, clap};
use crate::container::{Container, Inject, Registrar};
use crate::error::Result;
use crate::foundation::ServiceProvider;

pub(crate) struct FeatureServiceProvider;

impl ServiceProvider for FeatureServiceProvider {
    fn register(&self, app: &mut Registrar) {
        app.commands().add::<Purge>();
        app.part(crate::feature::Fakes::default());
        #[cfg(feature = "minijinja")]
        crate::feature::add_template_function(app.views());
    }

    async fn boot(&self, app: &Container) -> Result<()> {
        crate::queue::duplicate_names(
            "features",
            "#[flag(name = \"..\")]",
            inventory::iter::<__Registration>
                .into_iter()
                .map(|feature| (feature.name, (feature.type_name)())),
        )
        .map_err(crate::Error::msg)?;
        app.resolve::<Store>().await?;

        Ok(())
    }
}

/// Delete stored feature values.
#[derive(clap::Parser)]
#[command(name = "feature:purge")]
struct Purge {
    /// The stored names of the features to purge. Without names, the command purges every stored
    /// feature.
    #[arg(value_name = "FEATURES")]
    features: Vec<String>,
    /// Keep this feature. Repeat it to keep several.
    #[arg(long, value_name = "FEATURE")]
    except: Vec<String>,
    /// Keep every feature the application defines.
    #[arg(long)]
    except_defined: bool,
}

#[crate::console::command]
impl Purge {
    async fn handle(mut self, store: Inject<Store>, console: &mut Console) -> Result<()> {
        let mut stored = store.stored_names().await?;
        stored.sort_unstable();
        stored.dedup();
        let mut features = if self.features.is_empty() {
            stored
        } else {
            self.features.sort_unstable();
            self.features.dedup();
            let (known, unknown): (Vec<_>, Vec<_>) = self
                .features
                .into_iter()
                .partition(|feature| stored.contains(feature) || super::find(feature).is_some());
            for feature in unknown {
                match super::any::similar(&feature) {
                    Some(similar) => console.warn(format!(
                        "Feature `{feature}` isn't defined or stored. Did you mean `{similar}`?"
                    )),
                    None => console.warn(format!("Feature `{feature}` isn't defined or stored.")),
                }
            }
            known
        };
        features.retain(|feature| {
            !self.except.contains(feature) && !(self.except_defined && super::find(feature).is_some())
        });
        if features.is_empty() {
            console.info("No features to purge.");
            return Ok(());
        }

        store
            .purge(&features.iter().map(String::as_str).collect::<Vec<_>>())
            .await?;
        match features.as_slice() {
            [feature] => console.success(format!("Feature `{feature}` purged.")),
            _ => console.success(format!("{} features purged.", features.len())),
        }

        Ok(())
    }
}