Skip to main content

sz_rust_orm_facade/data_scope/modes/
custom.rs

1//! CUSTOM 模式 — 自定义条件(通过 CustomConditionGenerator 生成)
2
3use super::ModeEvaluator;
4use crate::data_scope::context::DataScopeContext;
5use crate::data_scope::custom::CustomGeneratorRegistry;
6use crate::data_scope::error::DataScopeError;
7use crate::data_scope::rule::DataScopeRule;
8use crate::repository::WhereCondition;
9use async_trait::async_trait;
10use std::sync::Arc;
11
12/// CUSTOM 模式评估器
13pub struct CustomMode {
14    registry: Arc<CustomGeneratorRegistry>,
15}
16
17impl CustomMode {
18    pub fn new(registry: Arc<CustomGeneratorRegistry>) -> Self {
19        Self { registry }
20    }
21}
22
23#[async_trait]
24impl ModeEvaluator for CustomMode {
25    async fn evaluate(
26        &self,
27        ctx: &DataScopeContext,
28        rule: &DataScopeRule,
29    ) -> Result<Vec<WhereCondition>, DataScopeError> {
30        let generator_name = rule.custom_generator.as_deref().ok_or_else(|| {
31            DataScopeError::InvalidRule("CUSTOM mode requires custom_generator".into())
32        })?;
33        let generator = self
34            .registry
35            .get(generator_name)
36            .ok_or_else(|| DataScopeError::GeneratorNotFound(generator_name.to_string()))?;
37        let conditions = generator.generate(ctx).await?;
38        if conditions.is_empty() {
39            return Err(DataScopeError::UnsafeCustomCondition(
40                "custom generator returned empty conditions".into(),
41            ));
42        }
43        Ok(conditions)
44    }
45}