rustorm-core 0.1.2

Core traits, types and utilities for RustORM
Documentation
use crate::query::QueryBuilder;

/// Именованный переиспользуемый фрагмент запроса.
///
/// ```rust
/// let active = Scope::new(|q| q.filter(|u| u.is_active.is_true()));
/// User::query().apply(active).fetch_all(&pool).await?;
/// ```
pub struct Scope<T> {
    pub(crate) apply_fn: Box<dyn FnOnce(QueryBuilder<T>) -> QueryBuilder<T> + Send>,
}

impl<T: Send + Sync + Unpin + 'static> Scope<T> {
    pub fn new<F>(f: F) -> Self
    where
        F: FnOnce(QueryBuilder<T>) -> QueryBuilder<T> + Send + 'static,
    {
        Self {
            apply_fn: Box::new(f),
        }
    }

    /// Создаёт пустой scope (no-op).
    pub fn none() -> Self {
        Self::new(|q| q)
    }
}

/// Трейт для определения scopes на модели.
/// Пользователь реализует его в `impl UserScopes for User { ... }`.
pub trait ModelScopes: Sized + Send + Sync + Unpin + 'static {
    fn scope(s: Scope<Self>) -> crate::query::QueryBuilder<Self>;
}