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
55
56
57
58
59
60
61
62
63
64
65
use crate::Expression;
use crate::Statement;
use crate::{Flag, Flaggable};

use serde::Serialize;

#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum ClosureType {
    Long,
    Short,
}

#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct Function {
    pub name: Option<String>,
    parameters: Vec<FunctionParameter>,
    body: Vec<Statement>,
    return_type_hint: Option<String>,
    flags: Vec<Flag>,
    closure_type: Option<ClosureType>,
}

impl Function {
    pub fn new(name: Option<String>, parameters: Vec<FunctionParameter>, body: Vec<Statement>, return_type_hint: Option<String>, flags: Vec<Flag>, closure_type: Option<ClosureType>) -> Self {
        Self {
            name,
            parameters,
            body,
            return_type_hint,
            flags,
            closure_type,
        }
    }
}

impl Flaggable for Function {
    fn add_flag(&mut self, flag: Flag) {
        self.flags.push(flag)
    }

    fn has_flag(&self, flag: Flag) -> bool {
        self.flags.contains(&flag)
    }

    fn has_flags(&self) -> bool {
        !self.flags.is_empty()
    }

    fn has_visiblity_flag(&self) -> bool {
        self.flags.clone().into_iter().filter(|flag| flag.is_visibility_flag()).count() > 1
    }
}

#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct FunctionParameter {
    name: String,
    type_hint: Option<String>,
    default: Option<Expression>,
}

impl FunctionParameter {
    pub fn new(name: String, type_hint: Option<String>, default: Option<Expression>) -> Self {
        Self { name, type_hint, default }
    }
}