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
70
use crate::ast::{
    ConstDirectives, ConstValue, FromTokens, ParseError, Tokens, TypeReference, Variable,
};
use crate::lexical_token::PunctuatorType;

#[derive(Debug)]
pub struct VariableDefinition<'a> {
    pub(crate) variable: Variable<'a>,
    pub(crate) r#type: TypeReference<'a>,
    pub(crate) default_value: Option<ConstValue<'a>>,
    pub(crate) 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 = TypeReference::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) -> &TypeReference {
        &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 TypeReference = TypeReference<'a>;
    type Directives = ConstDirectives<'a>;
    type Variable = Variable<'a>;

    fn variable(&self) -> &Self::Variable {
        &self.variable
    }

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

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

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