rusty_rules/
types.rs

1use std::sync::Arc;
2
3use serde_json::Value as JsonValue;
4
5pub use inner::*;
6
7use crate::{Operator, Value};
8
9#[cfg(not(feature = "send"))]
10mod inner {
11    use std::error::Error as StdError;
12    use std::future::Future;
13    use std::pin::Pin;
14    use std::sync::Arc;
15
16    pub trait MaybeSend {}
17    impl<T: ?Sized> MaybeSend for T {}
18
19    pub trait MaybeSync {}
20    impl<T: ?Sized> MaybeSync for T {}
21
22    pub(crate) type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
23
24    pub(crate) type DynError = Box<dyn StdError>;
25
26    /// Callback type for operator check function
27    pub type CheckFn<Ctx> = dyn Fn(&Ctx, crate::Value) -> Result<bool, DynError>;
28
29    /// Callback type for async operator check function
30    pub type AsyncCheckFn<Ctx> =
31        dyn for<'a> Fn(&'a Ctx, crate::Value<'a>) -> BoxFuture<'a, Result<bool, DynError>>;
32
33    pub(crate) type EvalFn<Ctx> = Arc<dyn Fn(&Ctx) -> Result<bool, DynError>>;
34
35    pub(crate) type AsyncEvalFn<Ctx> =
36        Arc<dyn for<'a> Fn(&'a Ctx) -> BoxFuture<'a, Result<bool, DynError>>>;
37}
38
39#[cfg(feature = "send")]
40mod inner {
41    use std::error::Error as StdError;
42    use std::future::Future;
43    use std::pin::Pin;
44    use std::sync::Arc;
45
46    pub trait MaybeSend: Send {}
47    impl<T: Send + ?Sized> MaybeSend for T {}
48
49    pub trait MaybeSync: Sync {}
50    impl<T: Sync + ?Sized> MaybeSync for T {}
51
52    pub(crate) type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
53
54    pub(crate) type DynError = Box<dyn StdError + Send + Sync>;
55
56    /// Callback type for operator check function
57    pub type CheckFn<Ctx> = dyn Fn(&Ctx, crate::Value) -> Result<bool, DynError> + Send + Sync;
58
59    /// Callback type for async operator check function
60    pub type AsyncCheckFn<Ctx> = dyn for<'a> Fn(&'a Ctx, crate::Value<'a>) -> BoxFuture<'a, Result<bool, DynError>>
61        + Send
62        + Sync;
63
64    pub(crate) type EvalFn<Ctx> = Arc<dyn Fn(&Ctx) -> Result<bool, DynError> + Send + Sync>;
65
66    pub(crate) type AsyncEvalFn<Ctx> =
67        Arc<dyn for<'a> Fn(&'a Ctx) -> BoxFuture<'a, Result<bool, DynError>> + Send + Sync>;
68}
69
70/// Callback type for fetchers
71pub type FetcherFn<Ctx> = for<'a> fn(&'a Ctx, &[String]) -> Result<Value<'a>, DynError>;
72
73/// Callback type for async fetchers
74pub type AsyncFetcherFn<Ctx> =
75    for<'a> fn(&'a Ctx, Arc<[String]>) -> BoxFuture<'a, Result<Value<'a>, DynError>>;
76
77pub trait ToOperator<Ctx: ?Sized>: MaybeSend + MaybeSync {
78    fn to_operator(&self, value: &JsonValue) -> Result<Operator<Ctx>, DynError>;
79
80    fn json_schema(&self) -> JsonValue {
81        serde_json::json!({})
82    }
83}
84
85impl<Ctx: ?Sized, F> ToOperator<Ctx> for F
86where
87    F: Fn(JsonValue) -> Result<Operator<Ctx>, DynError> + MaybeSend + MaybeSync + 'static,
88{
89    fn to_operator(&self, value: &JsonValue) -> Result<Operator<Ctx>, DynError> {
90        self(value.clone())
91    }
92}