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
101
102
103
use crate::io::RenderKotlin;
use crate::spec::{VisibilityModifier, CodeBlock, Parameter, Property};
use crate::tokens;

#[derive(Debug, Clone)]
enum PrimaryConstructorParameter {
    PropertyParameter(Property),
    FunctionLike(Parameter),
}

impl RenderKotlin for PrimaryConstructorParameter {
    fn render_into(&self, block: &mut CodeBlock) {
        match self {
            PrimaryConstructorParameter::PropertyParameter(property) =>
                block.push_renderable(property),
            PrimaryConstructorParameter::FunctionLike(param) =>
                block.push_renderable(param)
        }
    }
}

/// Defines [Class's primary constructor](https://kotlinlang.org/docs/classes.html#constructors)
#[derive(Debug, Clone)]
pub struct PrimaryConstructor {
    arguments: Vec<PrimaryConstructorParameter>,
    visibility_modifier: VisibilityModifier,
}

impl PrimaryConstructor {
    pub fn new() -> PrimaryConstructor {
        PrimaryConstructor {
            arguments: Vec::new(),
            visibility_modifier: VisibilityModifier::default(),
        }
    }

    pub fn parameter(mut self, parameter: Parameter) -> PrimaryConstructor {
        self.arguments.push(PrimaryConstructorParameter::FunctionLike(parameter));
        self
    }

    pub fn property(mut self, property: Property) -> PrimaryConstructor {
        self.arguments.push(PrimaryConstructorParameter::PropertyParameter(property));
        self
    }

    pub fn visibility_modifier(mut self, visibility_modifier: VisibilityModifier) -> PrimaryConstructor {
        self.visibility_modifier = visibility_modifier;
        self
    }
}

impl RenderKotlin for PrimaryConstructor {
    fn render_into(&self, block: &mut CodeBlock) {
        block.push_renderable(&self.visibility_modifier);
        block.push_space();
        block.push_static_atom(tokens::keyword::CONSTRUCTOR);
        block.push_round_brackets(|params_block| {
            params_block.push_comma_separated(&self.arguments)
        });
    }
}

#[cfg(test)]
mod tests {
    use crate::io::RenderKotlin;
    use crate::spec::{VisibilityModifier, CodeBlock, Parameter, PrimaryConstructor, Property, Type};

    #[test]
    fn primary_constructor_test() {
        let property = Property::new(
            "name",
            Type::string(),
        ).initializer(
            CodeBlock::atom("\"\"")
        );

        let function_parameter = Parameter::new(
            "age",
            Type::int(),
        );

        let primary_constructor = PrimaryConstructor::new()
            .property(property)
            .parameter(function_parameter);

        assert_eq!(
            primary_constructor.render_string(),
            "public constructor(public final val name: kotlin.String = \"\", age: kotlin.Int)"
        );
    }

    #[test]
    fn test_private_constructor() {
        let primary_constructor = PrimaryConstructor::new()
            .visibility_modifier(VisibilityModifier::Private);

        assert_eq!(
            primary_constructor.render_string(),
            "private constructor()"
        );
    }
}