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
use std::{fmt, ops::Deref};

use crate::compiler::{
    expression::Expr,
    parser::{Ident, Node},
};

#[derive(Clone, Debug, PartialEq)]
pub struct FunctionArgument {
    ident: Option<Node<Ident>>,
    expr: Node<Expr>,
}

impl FunctionArgument {
    pub(crate) fn new(ident: Option<Node<Ident>>, expr: Node<Expr>) -> Self {
        Self { ident, expr }
    }

    pub(crate) fn keyword(&self) -> Option<&str> {
        self.ident.as_ref().map(|node| node.as_ref().as_ref())
    }

    pub(crate) fn keyword_span(&self) -> Option<crate::parser::Span> {
        self.ident.as_ref().map(Node::span)
    }

    pub fn expr(&self) -> &Expr {
        self.expr.inner()
    }

    pub(crate) fn expr_span(&self) -> crate::parser::Span {
        self.expr.span()
    }

    pub(crate) fn into_inner(self) -> Expr {
        self.expr.into_inner()
    }
}

impl fmt::Display for FunctionArgument {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.expr.fmt(f)
    }
}

impl Deref for FunctionArgument {
    type Target = Node<Expr>;

    fn deref(&self) -> &Self::Target {
        &self.expr
    }
}