mf_expression/functions/
mod.rs

1//! 函数模块
2//!
3//! 提供表达式中可用的各种函数类型,包括内置函数、自定义函数、方法和已废弃函数
4
5pub use crate::functions::date_method::DateMethod;
6pub use crate::functions::defs::FunctionTypecheck;
7pub use crate::functions::deprecated::DeprecatedFunction;
8pub use crate::functions::internal::InternalFunction;
9pub use crate::functions::method::{MethodKind, MethodRegistry};
10pub use crate::functions::registry::FunctionRegistry;
11pub use crate::functions::mf_function::MfFunction;
12pub use crate::functions::state_guard::{StateGuard, with_state_async};
13
14use std::fmt::Display;
15use strum_macros::{Display, EnumIter, EnumString, IntoStaticStr};
16
17pub mod arguments; // 函数参数处理
18mod date_method; // 日期方法
19pub mod defs; // 函数定义接口
20mod deprecated; // 已废弃函数
21pub mod internal; // 内置函数
22mod method; // 方法注册表
23pub mod mf_function;
24pub(crate) mod registry; // 函数注册表
25pub mod state_guard; // State 守卫模块
26
27/// 函数类型枚举
28///
29/// 定义了表达式系统中所有可用的函数类型
30#[derive(Debug, PartialEq, Eq, Clone)]
31pub enum FunctionKind {
32    /// 内置函数:系统预定义的标准函数
33    Internal(InternalFunction),
34    /// 已废弃函数:为向后兼容保留的旧函数
35    Deprecated(DeprecatedFunction),
36    /// 闭包函数:用于数组操作的特殊函数
37    Closure(ClosureFunction),
38    /// 自定义函数:用户定义的扩展函数
39    Mf(MfFunction),
40}
41
42impl TryFrom<&str> for FunctionKind {
43    type Error = strum::ParseError;
44
45    /// 从字符串解析函数类型
46    ///
47    /// 按优先级顺序尝试匹配:内置函数 > 已废弃函数 > 闭包函数 > 自定义函数
48    fn try_from(value: &str) -> Result<Self, Self::Error> {
49        InternalFunction::try_from(value)
50            .map(FunctionKind::Internal)
51            .or_else(|_| {
52                DeprecatedFunction::try_from(value)
53                    .map(FunctionKind::Deprecated)
54            })
55            .or_else(|_| {
56                ClosureFunction::try_from(value).map(FunctionKind::Closure)
57            })
58            .or_else(|_| MfFunction::try_from(value).map(FunctionKind::Mf))
59    }
60}
61
62impl Display for FunctionKind {
63    fn fmt(
64        &self,
65        f: &mut std::fmt::Formatter<'_>,
66    ) -> std::fmt::Result {
67        match self {
68            FunctionKind::Internal(i) => write!(f, "{i}"),
69            FunctionKind::Deprecated(d) => write!(f, "{d}"),
70            FunctionKind::Closure(c) => write!(f, "{c}"),
71            FunctionKind::Mf(m) => write!(f, "{m}"),
72        }
73    }
74}
75
76/// 闭包函数枚举
77///
78/// 定义了用于数组操作的特殊闭包函数
79#[derive(
80    Debug,
81    PartialEq,
82    Eq,
83    Hash,
84    Display,
85    EnumString,
86    EnumIter,
87    IntoStaticStr,
88    Clone,
89    Copy,
90)]
91#[strum(serialize_all = "camelCase")]
92pub enum ClosureFunction {
93    /// 全部匹配:检查数组中所有元素是否都满足条件
94    All,
95    /// 无匹配:检查数组中是否没有元素满足条件
96    None,
97    /// 存在匹配:检查数组中是否有元素满足条件
98    Some,
99    /// 唯一匹配:检查数组中是否只有一个元素满足条件
100    One,
101    /// 过滤:返回满足条件的元素组成的新数组
102    Filter,
103    /// 映射:对每个元素应用函数,返回结果数组
104    Map,
105    /// 扁平映射:对每个元素应用函数并展平结果
106    FlatMap,
107    /// 计数:统计满足条件的元素数量
108    Count,
109}