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
66
67
68
69
use crate::ast::{
    executable::VariableType, ConstDirectives, ConstValue, FromTokens, ParseError, Tokens, Variable,
};
use crate::lexical_token::PunctuatorType;

#[derive(Debug)]
pub struct VariableDefinition<'a> {
    variable: Variable<'a>,
    r#type: VariableType<'a>,
    default_value: Option<ConstValue<'a>>,
    directives: ConstDirectives<'a>,
}

impl<'a> FromTokens<'a> for VariableDefinition<'a> {
    fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
        let variable = Variable::from_tokens(tokens)?;
        tokens.expect_punctuator(PunctuatorType::Colon)?;
        let r#type = VariableType::from_tokens(tokens)?;
        let default_value: Option<ConstValue> =
            if tokens.next_if_punctuator(PunctuatorType::Equals).is_some() {
                Some(ConstValue::from_tokens(tokens)?)
            } else {
                None
            };
        let directives = ConstDirectives::from_tokens(tokens)?;
        Ok(Self {
            variable,
            r#type,
            default_value,
            directives,
        })
    }
}

impl<'a> VariableDefinition<'a> {
    pub fn variable(&self) -> &Variable {
        &self.variable
    }

    pub fn r#type(&self) -> &VariableType {
        &self.r#type
    }

    pub fn default_value(&self) -> Option<&ConstValue> {
        self.default_value.as_ref()
    }
}

impl<'a> bluejay_core::executable::VariableDefinition for VariableDefinition<'a> {
    type Value = ConstValue<'a>;
    type VariableType = VariableType<'a>;
    type Directives = ConstDirectives<'a>;

    fn variable(&self) -> &str {
        self.variable.name()
    }

    fn r#type(&self) -> &Self::VariableType {
        &self.r#type
    }

    fn directives(&self) -> &Self::Directives {
        &self.directives
    }

    fn default_value(&self) -> Option<&Self::Value> {
        self.default_value.as_ref()
    }
}