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
use crate::io::RenderKotlin;
use crate::spec::{AnnotationTarget, Argument, ClassLikeTypeName, CodeBlock};
use crate::tokens;

/// Represents an annotation in Kotlin. Used for adding meta information for code entities.
///
/// [Official documentation reference](https://kotlinlang.org/docs/annotations.html)
///
/// # Examples
///
/// ```rust
/// use std::str::FromStr;
/// use kotlin_poet_rs::io::RenderKotlin;
/// use kotlin_poet_rs::spec::{Annotation, Argument, ClassLikeTypeName, CodeBlock, Name, Package};
///
/// let annotation = Annotation::new(
///     ClassLikeTypeName::top_level(
///         Package::from_str("a.b.c").unwrap(),
///         Name::from("MyAnnotation")
///     )
/// ).argument(
///     Argument::new_named("value", CodeBlock::atom("1"))
/// ).argument(
///     Argument::new_named("name", CodeBlock::atom("\"name_value\""))
/// );
///
/// assert_eq!(
///     annotation.render_string(),
///     "@a.b.c.MyAnnotation(value = 1, name = \"name_value\")"
/// );
/// ```
#[derive(Debug, Clone)]
pub struct Annotation {
    type_name: ClassLikeTypeName,
    arguments: Vec<Argument>,
    target: Option<AnnotationTarget>,
}

#[derive(Debug, Clone)]
pub(crate) enum  AnnotationSlotRenderMode { Vertical, Horizontal }

#[derive(Debug, Clone)]
pub(crate) struct AnnotationSlot {
    inner: Vec<Annotation>,
    render_mode: AnnotationSlotRenderMode
}

impl AnnotationSlot {

    pub(crate) fn vertical() -> AnnotationSlot {
        AnnotationSlot {
            inner: Vec::new(),
            render_mode: AnnotationSlotRenderMode::Vertical
        }
    }

    pub(crate) fn horizontal() -> AnnotationSlot {
        AnnotationSlot {
            inner: Vec::new(),
            render_mode: AnnotationSlotRenderMode::Horizontal
        }
    }

    pub(crate) fn push(&mut self, new: Annotation) {
        self.inner.push(new)
    }
}

impl RenderKotlin for AnnotationSlot {
    fn render_into(&self, block: &mut CodeBlock) {
        for annotation in &self.inner {
            block.push_renderable(annotation);
            match self.render_mode {
                AnnotationSlotRenderMode::Vertical => {
                    block.push_new_line()
                }
                AnnotationSlotRenderMode::Horizontal => {
                    block.push_space()
                }
            }
        }
    }
}

impl Annotation {
    pub fn new<ClassLikeTypeNameLike: Into<ClassLikeTypeName>>(type_name: ClassLikeTypeNameLike) -> Self {
        Annotation {
            type_name: type_name.into(),
            arguments: Vec::new(),
            target: None,
        }
    }

    pub fn argument(mut self, argument: Argument) -> Self {
        self.arguments.push(argument);
        self
    }

    pub fn target(mut self, target: AnnotationTarget) -> Self {
        self.target = Some(target);
        self
    }
}

impl RenderKotlin for Annotation {
    fn render_into(&self, block: &mut CodeBlock) {
        block.push_static_atom(tokens::AT);
        if let Some(target) = &self.target {
            block.push_renderable(target);
            block.push_static_atom(tokens::COLON);
        }
        block.push_renderable(&self.type_name);
        block.push_round_brackets(|inner_code| {
            inner_code.push_comma_separated(&self.arguments)
        });
    }
}

macro_rules! mixin_annotation_mutators {
    () => {
        /// Adds [Annotation] to this entity.
        /// They will appear in order this method is called.
        pub fn annotation(mut self, annotation: Annotation) -> Self {
            self.annotation_slot.push(annotation);
            self
        }
    };
}

pub(crate) use mixin_annotation_mutators;

#[cfg(test)]
mod tests {
    use std::str::FromStr;
    use crate::io::RenderKotlin;
    use crate::spec::{Annotation, AnnotationTarget, Argument, ClassLikeTypeName, CodeBlock, Package};

    #[test]
    fn test_annotation() {
        let annotation = Annotation::new(
            ClassLikeTypeName::top_level(Package::from_str("a.b.c").unwrap(), "MyAnnotation")
        ).argument(
            Argument::new_named("value", CodeBlock::atom("1"))
        ).argument(
            Argument::new_named("name", CodeBlock::atom("\"name_value\""))
        );

        let code = annotation.render_string();

        assert_eq!(
            code,
            "@a.b.c.MyAnnotation(value = 1, name = \"name_value\")"
        );
    }

    #[test]
    fn test_annotation_with_target() {
        let annotation = Annotation::new(
            ClassLikeTypeName::top_level(Package::from_str("a.b.c").unwrap(), "MyAnnotation")
        ).argument(
            Argument::new_named("value", CodeBlock::atom("1"))
        ).argument(
            Argument::new_named("name", CodeBlock::atom("\"name_value\""))
        ).target(
            AnnotationTarget::Field
        );

        let code = annotation.render_string();

        assert_eq!(
            code,
            "@field:a.b.c.MyAnnotation(value = 1, name = \"name_value\")"
        );
    }
}