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
188
189
190
191
192
193
194
195
196
197
198
199
use std::str::FromStr;

use crate::io::RenderKotlin;
use crate::spec::{ClassLikeTypeName, CodeBlock, FunctionType, Name, Package};
use crate::spec::class_like_type::ClassLikeType;
use crate::tokens;
use crate::util::{SemanticConversionError, yolo_from_str};

// region stdlib types codegen
macro_rules! fn_basic_type_factory {
    ($identifier:ident, $($package:tt).+, $class:ident) => {
        #[doc = concat!(
            "Creates `",
            stringify!($($package).+),
            ".",
            stringify!($class)
        )]
        pub fn $identifier() -> Type {
            use std::str::FromStr;

            let package = Package::from_str(stringify!($($package).+)).unwrap();
            let name = Name::from_str(stringify!($class)).unwrap();

            Type::ClassLike(
                ClassLikeType::new(
                    ClassLikeTypeName::top_level(
                        package,
                        name
                    )
                )
            )
        }
    };
}

macro_rules! fn_generic_type_factory {
    ($identifier:ident, $($package:tt).+, $class:ident<$($generic:ident),+>) => {
        #[doc = concat!(
            "Creates `",
            stringify!($($package).+),
            ".",
            stringify!($class)
        )]
        pub fn $identifier($($generic: Type,)+) -> Type {
            use std::str::FromStr;

            let package = Package::from_str(stringify!($($package).+)).unwrap();
            let name = Name::from_str(stringify!($class)).unwrap();

            let mut inner_type = ClassLikeType::new(
                ClassLikeTypeName::top_level(
                    package,
                    name,
                )
            );

            $(
            inner_type = inner_type.generic_argument($generic);
            )+

            Type::ClassLike(
                inner_type
            )
        }
    };
}
// endregion stdlib types codegen

/// Kotlin fully resolved / qualified type
#[derive(PartialEq, Debug, Clone)]
pub enum Type {
    /// Type that behaves like class (e.g. `kotlin.String`, `kotlin.collections.List<String>`)
    ClassLike(ClassLikeType),
    /// Functional type (e.g. `(Int) -> String`)
    Function(FunctionType),
    /// Generic argument as type (e.g. `T`)
    Generic(Name),
}

impl Type {
    /// Creates generic type
    pub fn generic<NameLike: Into<Name>>(name: NameLike) -> Type {
        Type::Generic(name.into())
    }

    // Integer numbers
    fn_basic_type_factory!(int, kotlin, Int);
    fn_basic_type_factory!(long, kotlin, Long);
    fn_basic_type_factory!(short, kotlin, Short);
    fn_basic_type_factory!(byte, kotlin, Byte);

    // Floating point numbers
    fn_basic_type_factory!(float, kotlin, Float);
    fn_basic_type_factory!(double, kotlin, Double);

    // Logic
    fn_basic_type_factory!(boolean, kotlin, Boolean);

    // Text
    fn_basic_type_factory!(char, kotlin, Char);
    fn_basic_type_factory!(string, kotlin, String);

    // Control Types
    fn_basic_type_factory!(unit, kotlin, Unit);
    fn_basic_type_factory!(any, kotlin, Any);
    fn_basic_type_factory!(nothing, kotlin, Nothing);

    // Collections
    fn_generic_type_factory!(map, kotlin.collections, Map<key, value>);
    fn_generic_type_factory!(list, kotlin.collections, List<value>);
    fn_generic_type_factory!(set, kotlin.collections, Set<value>);
    fn_generic_type_factory!(array, kotlin, Array<value>);
}

impl From<ClassLikeTypeName> for Type {
    fn from(value: ClassLikeTypeName) -> Self {
        Type::ClassLike(ClassLikeType::new(value))
    }
}

impl From<ClassLikeType> for Type {
    fn from(value: ClassLikeType) -> Self {
        Type::ClassLike(value)
    }
}

yolo_from_str!(Type);
impl FromStr for Type {
    type Err = SemanticConversionError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let clear = s.trim();

        if !clear.contains(tokens::DOT) {
            return Ok(
                Type::Generic(
                    Name::from_str(clear)?
                )
            );
        }

        if clear.contains(tokens::ROUND_BRACKET_LEFT) {
            return Err(
                SemanticConversionError::new(
                    "Function types are not supported by Type::from_str"
                )
            );
        }

        Ok(
            Type::ClassLike(
                ClassLikeType::from_str(clear)?
            )
        )
    }
}

impl RenderKotlin for Type {
    fn render_into(&self, block: &mut CodeBlock) {
        match self {
            Type::ClassLike(class_like) => block.push_renderable(class_like),
            Type::Generic(name) => block.push_renderable(name),
            Type::Function(lambda) => block.push_renderable(lambda)
        }
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

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

    #[test]
    fn render_generic_parameter() {
        let name = Name::from_str("T").unwrap();
        let parameter = Type::Generic(name);
        assert_eq!(parameter.render_string(), "T");
    }

    #[test]
    fn parse_fn_type() {
        let new_type = Type::from_str("() -> String");
        assert!(matches!(
            new_type,
            Err(SemanticConversionError)
        ));
    }

    #[test]
    fn parse_generic() {
        let new_type = Type::from_str("T");
        assert_eq!(
            new_type.unwrap(),
            Type::generic("T")
        );
    }
}