rusty_rules/
types.rs

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