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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use crate::io::RenderKotlin;
use crate::spec::{Annotation, AnnotationTarget, Class, ClassLikeTypeName, CodeBlock, Comment, Function, Import, Package, Property, TypeAlias};
use crate::tokens;

#[derive(Debug, Clone)]
enum KotlinFileNode {
    Property(Property),
    Function(Function),
    TypeAlias(TypeAlias),
    Class(Class),
}

/// Represents a Kotlin file.
#[derive(Debug, Clone)]
pub struct KotlinFile {
    package: Package,
    imports: Vec<Import>,
    nodes: Vec<KotlinFileNode>,
    annotations: Vec<Annotation>,
    header_comments: Vec<Comment>,
}

impl KotlinFile {
    /// Creates file in specified [package]
    pub fn new<PackageLike: Into<Package>>(package: PackageLike) -> Self {
        KotlinFile {
            package: package.into(),
            imports: Vec::new(),
            nodes: Vec::new(),
            annotations: Vec::new(),
            header_comments: Vec::new(),
        }
    }

    /// Creates new file without package statement
    pub fn root() -> Self {
        KotlinFile {
            package: Package::root(),
            imports: Vec::new(),
            nodes: Vec::new(),
            annotations: Vec::new(),
            header_comments: Vec::new(),
        }
    }

    /// Adds new comment in first line.
    ///
    /// This method can be called multiple times to add multiple comments,
    /// they will appear in order on enw lines.
    pub fn header_comment<CommentLike: Into<Comment>>(mut self, comment: CommentLike) -> Self {
        self.header_comments.push(comment.into());
        self
    }

    /// Adds new import to the file.
    pub fn import(mut self, import: Import) -> Self {
        self.imports.push(import);
        self
    }

    /// Adds new property to the file.
    pub fn property(mut self, property: Property) -> Self {
        self.nodes.push(KotlinFileNode::Property(property));
        self
    }

    /// Adds new function to the file.
    pub fn function(mut self, function: Function) -> Self {
        self.nodes.push(KotlinFileNode::Function(function));
        self
    }

    /// Adds new type alias to the file.
    pub fn type_alias(mut self, type_alias: TypeAlias) -> Self {
        self.nodes.push(KotlinFileNode::TypeAlias(type_alias));
        self
    }

    /// Adds new class to the file.
    pub fn class(mut self, class: Class) -> Self {
        self.nodes.push(KotlinFileNode::Class(class));
        self
    }

    /// Adds new annotation to the file.
    /// Added annotation will be forced to have [AnnotationTarget::File] target.
    pub fn annotation(mut self, annotation: Annotation) -> Self {
        self.annotations.push(
            annotation
                .target(AnnotationTarget::File)
        );
        self
    }
}

impl From<ClassLikeTypeName> for KotlinFile {
    fn from(value: ClassLikeTypeName) -> Self {
        let package = value.package;
        KotlinFile::new(package)
    }
}

impl RenderKotlin for KotlinFile {
    fn render_into(&self, block: &mut CodeBlock) {
        if !self.header_comments.is_empty() {
            for comment in &self.header_comments {
                block.push_renderable(comment);
                block.push_new_line();
            }
            block.push_new_line()
        }

        for annotation in &self.annotations {
            block.push_renderable(annotation);
            block.push_new_line();
        }
        if !self.annotations.is_empty() {
            block.push_new_line();
        }

        if !self.package.is_root() {
            block.push_static_atom(tokens::keyword::PACKAGE);
            block.push_space();
            block.push_renderable(&self.package);
            block.push_new_line();
        }

        for import in &self.imports {
            block.push_renderable(import);
            block.push_new_line();
        }

        for node in &self.nodes {
            match node {
                KotlinFileNode::Property(property) => {
                    block.push_new_line();
                    block.push_renderable(property);
                    block.push_new_line();
                }
                KotlinFileNode::Function(function) => {
                    block.push_new_line();
                    block.push_renderable(function);
                    block.push_new_line();
                }
                KotlinFileNode::TypeAlias(type_alias) => {
                    block.push_new_line();
                    block.push_renderable(type_alias);
                    block.push_new_line();
                }
                KotlinFileNode::Class(class) => {
                    block.push_new_line();
                    block.push_renderable(class);
                    block.push_new_line();
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::io::RenderKotlin;
    use crate::spec::{Comment, Function, KotlinFile};

    #[test]
    fn test_root_file() {
        let file = KotlinFile::root()
            .function(Function::new("main"));

        assert_eq!(
            file.render_string(),
            "public fun main(): kotlin.Unit",
        )
    }

    #[test]
    fn test_file_with_header_comments() {
        let file = KotlinFile::new("com.example")
            .header_comment(Comment::from("This is a header comment"))
            .header_comment(Comment::from("This is another header comment"));

        assert_eq!(
            file.render_string(),
            "// This is a header comment\n// This is another header comment\n\npackage com.example"
        )
    }
}