gitql_core/
signature.rs

1use super::values::Value;
2
3use gitql_ast::types::DataType;
4
5/// Standard function accept array of values and return single [`Value`]
6pub type StandardFunction = fn(&[Box<dyn Value>]) -> Box<dyn Value>;
7
8/// Aggregation function accept a selected row values for each row in group and return single [`Value`]
9///
10/// [`Vec<Vec<Value>>`] represent the selected values from each row in group
11///
12/// For Example if we have three rows in group and select name and email from each one
13///
14/// [[name, email], [name, email], [name, email]]
15///
16/// This implementation allow aggregation function to accept more than one parameter,
17/// and also accept any Expression not only field name
18///
19pub type AggregationFunction = fn(&[Vec<Box<dyn Value>>]) -> Box<dyn Value>;
20
21/// Window function  a selected row values for each row in a specific frame and return single [`Value`]
22///
23/// [`Vec<Vec<Value>>`] represent the selected values from each row in frame of rows
24///
25/// For Example if we have three rows in frame of row and select name and email from each one
26///
27/// [[name, email], [name, email], [name, email]]
28///
29/// This implementation allow Window` function to accept more than one parameter,
30/// and also accept any Expression not only field name
31///
32pub type WindowFunction = fn(&[Vec<Box<dyn Value>>]) -> Vec<Box<dyn Value>>;
33
34/// Signature struct is a representation of function type
35///
36/// Function type in GitQL used to track parameters and return type for now
37/// but later can track parameter names to allow pass parameter by name and improve error messages
38///
39/// GitQL Function Signature has some rules to follow
40///
41/// Rules:
42/// - Parameters must contains 0 or 1 [`VarargsType`] parameter type only.
43/// - [`VarargsType`] must be the last parameter.
44/// - You can add 0 or more [`DataType::Optional`] parameters.
45/// - [`OptionalType`] parameters must be at the end but also before [`VarargsType`] if exists.
46///
47/// The return type can be a static [`DataType`] such as Int, Float or Dynamic
48/// so you can return a dynamic type depending on parameters.
49#[derive(Clone)]
50pub struct Signature {
51    pub parameters: Vec<Box<dyn DataType>>,
52    pub return_type: Box<dyn DataType>,
53}
54
55impl Signature {
56    /// Create Instance of [`Signature`] with parameters and return type
57    pub fn new(parameters: Vec<Box<dyn DataType>>, return_type: Box<dyn DataType>) -> Self {
58        Signature {
59            parameters,
60            return_type,
61        }
62    }
63
64    /// Create Instance of [`Signature`] with return type and zero parameters
65    pub fn with_return(return_type: Box<dyn DataType>) -> Self {
66        Signature {
67            parameters: Vec::default(),
68            return_type,
69        }
70    }
71
72    /// Add list of parameters to the [`Signature`]
73    pub fn add_parameters(mut self, mut parameters: Vec<Box<dyn DataType>>) -> Self {
74        self.parameters.append(&mut parameters);
75        self
76    }
77
78    /// Add parameter to the [`Signature`]
79    pub fn add_parameter(mut self, parameter: Box<dyn DataType>) -> Self {
80        self.parameters.push(parameter);
81        self
82    }
83}