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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use crate::io::RenderKotlin;
use crate::spec::{CodeBlock, Type};
use crate::tokens;

#[derive(PartialEq, Debug, Clone)]
pub struct FunctionType {
    receiver: Box<Option<Type>>,
    parameters: Vec<Type>,
    returns: Box<Type>,
    is_suspended: bool
}

impl FunctionType {

    pub fn new<TypeLike: Into<Type>>(returns: TypeLike) -> Self {
        FunctionType {
            receiver: Box::new(None),
            parameters: Vec::new(),
            returns: Box::new(returns.into()),
            is_suspended: false
        }
    }

    pub fn receiver<TypeLike: Into<Type>>(mut self, receiver: TypeLike) -> Self {
        self.receiver = Box::new(Some(receiver.into()));
        self
    }

    pub fn parameter<TypeLike: Into<Type>>(mut self, parameters: TypeLike) -> Self {
        self.parameters.push(parameters.into());
        self
    }

    pub fn returns<TypeLike: Into<Type>>(mut self, returns: TypeLike) -> Self {
        *self.returns = returns.into();
        self
    }

    pub fn suspended(mut self, flag: bool) -> Self {
        self.is_suspended = flag;
        self
    }
}

impl RenderKotlin for FunctionType {
    fn render_into(&self, block: &mut CodeBlock) {
        if let Some(receiver) = &*self.receiver {
            block.push_renderable(receiver);
            block.push_static_atom(tokens::DOT)
        }

        if self.is_suspended {
            block.push_static_atom(tokens::keyword::SUSPEND);
            block.push_space()
        }

        block.push_round_brackets(|parameters_code| {
            parameters_code.push_comma_separated(
                &self.parameters
            );
        });

        block.push_space();
        block.push_static_atom(tokens::ARROW);
        block.push_space();
        block.push_renderable(self.returns.as_ref());
    }
}

#[cfg(test)]
mod tests {

    use crate::io::RenderKotlin;
    use crate::spec::{FunctionType, Type};

    #[test]
    fn render_lambda_type() {
        let lambda_type = FunctionType::new(Type::int())
            .parameter(Type::string())
            .parameter(Type::boolean());
        assert_eq!(lambda_type.render_string(), "(kotlin.String, kotlin.Boolean) -> kotlin.Int");
    }

    #[test]
    fn render_lambda_type_with_receiver() {
        let lambda_type = FunctionType::new(Type::int())
            .receiver(Type::string())
            .parameter(Type::boolean());
        assert_eq!(lambda_type.render_string(), "kotlin.String.(kotlin.Boolean) -> kotlin.Int");
    }

    #[test]
    fn render_lambda_type_with_suspended() {
        let lambda_type = FunctionType::new(Type::int())
            .parameter(Type::string())
            .parameter(Type::boolean())
            .suspended(true);
        assert_eq!(lambda_type.render_string(), "suspend (kotlin.String, kotlin.Boolean) -> kotlin.Int");
    }
}