1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
mod context;
mod user_functions;
use crate::{error::Error, value::Value};
use async_trait::async_trait;
use displaydoc::Display as DisplayDoc;
use std::error;
use std::result;
pub use context::FunctionContext;
pub use user_functions::UserFunctions;
#[async_trait]
pub trait UserFunction {
async fn call(&self, params: Value) -> FunctionResult;
fn name(&self) -> &'static str;
fn cacheable(&self) -> bool {
true
}
}
pub type BoxedFunction = Box<dyn UserFunction + Send + Sync + 'static>;
#[derive(Debug, DisplayDoc, thiserror::Error)]
pub enum FunctionError {
InvalidParameter(Value, String),
Unspecified(#[from] Box<dyn error::Error + Send + Sync>),
}
impl From<Error> for FunctionError {
fn from(error: Error) -> Self {
match error {
Error::UnexpectedValueType(value, expected) => {
FunctionError::InvalidParameter(value, expected)
}
err => FunctionError::Unspecified(err.into()),
}
}
}
pub type FunctionResult = result::Result<Value, FunctionError>;