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
use crate::Expression;
use crate::{Flag, Flaggable};
use crate::Nullable;
use serde::Serialize;

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

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

impl Nullable for Property {
    fn is_nullable(&self) -> bool {
        if self.type_hint.is_none() {
            return false;
        }

        self.type_hint.clone().unwrap().starts_with("?")
    }
}

impl Flaggable for Property {
    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
    }
}