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
//
// Wildland Project
//
// Copyright © 2022 Golem Foundation,
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as published by
// the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use crate::binding_types::{ExternFunction, RustWrapperType, WrapperType};
use crate::extern_module_translator::Exceptions;

pub fn create_extern_imports(extern_functions: &[ExternFunction]) -> String {
    extern_functions
        .iter()
        .map(|function| {
            let args = function
                .arguments
                .iter()
                .map(|arg| match arg {
                    WrapperType {
                        wrapper_name,
                        rust_type: RustWrapperType::Primitive,
                        reference_parameters: None,
                        ..
                    } => wrapper_name.clone(),
                    WrapperType {
                        wrapper_name,
                        rust_type:
                            RustWrapperType::FieldlessEnum
                            | RustWrapperType::Exceptions(Exceptions::Primitive(_)),
                        reference_parameters: None,
                        ..
                    } => format!("enum {}", wrapper_name.clone()),
                    _ => "void*".to_owned(),
                })
                .collect::<Vec<String>>()
                .join(", ");
            let function_name = &function.name;
            let return_type = match &function.return_type {
                Some(WrapperType {
                    wrapper_name,
                    rust_type: RustWrapperType::Primitive,
                    reference_parameters: None,
                    ..
                }) => wrapper_name.clone(),
                Some(WrapperType {
                    wrapper_name,
                    rust_type: RustWrapperType::FieldlessEnum,
                    reference_parameters: None,
                    ..
                }) => format!("enum {}", wrapper_name.clone()),
                Some(WrapperType { .. }) => "void*".to_owned(),
                _ => "void".to_owned(),
            };
            format!("{return_type} {function_name}({args});\n")
        })
        .collect::<String>()
}

#[cfg(test)]

mod tests {
    use pretty_assertions::assert_eq;
    use syn::parse_quote;

    use super::*;

    #[test]
    fn should_create_function_returning_void() {
        let function_without_return_type = ExternFunction {
            arguments: vec![],
            return_type: None,
            name: "foo".to_owned(),
            tokens: parse_quote!(
                fn foo();
            ),
        };
        let result = create_extern_imports(&[function_without_return_type]);
        let expected = "void foo();\n";
        assert_eq!(result, expected);
    }

    #[test]
    fn should_create_function_returning_primitive() {
        let function_returning_primitive = ExternFunction {
            arguments: vec![],
            return_type: Some(WrapperType {
                original_type_name: parse_quote!(u32),
                wrapper_name: "u32".to_string(),
                rust_type: RustWrapperType::Primitive,
                reference_parameters: None,
            }),
            name: "foo".to_owned(),
            tokens: parse_quote!(
                fn foo();
            ),
        };
        let result = create_extern_imports(&[function_returning_primitive]);
        let expected = "u32 foo();\n";
        assert_eq!(result, expected);
    }

    #[test]
    fn should_create_function_returning_fieldless_enum() {
        let function_returning_fieldless_enum = ExternFunction {
            arguments: vec![],
            return_type: Some(WrapperType {
                original_type_name: parse_quote!(SomeEnum),
                wrapper_name: "SomeEnum".to_string(),
                rust_type: RustWrapperType::FieldlessEnum,
                reference_parameters: None,
            }),
            name: "foo".to_owned(),
            tokens: parse_quote!(
                fn foo() -> SomeEnum;
            ),
        };
        let result = create_extern_imports(&[function_returning_fieldless_enum]);
        let expected = "enum SomeEnum foo();\n";
        assert_eq!(result, expected);
    }

    #[test]
    fn should_create_function_returning_data_enum() {
        let function_returning_data_enum = ExternFunction {
            arguments: vec![],
            return_type: Some(WrapperType {
                original_type_name: parse_quote!(SomeDataEnum),
                wrapper_name: "SomeDataEnum".to_string(),
                rust_type: RustWrapperType::DataEnum,
                reference_parameters: None,
            }),
            name: "foo".to_owned(),
            tokens: parse_quote!(
                fn foo() -> SomeDataEnum;
            ),
        };
        let result = create_extern_imports(&[function_returning_data_enum]);
        let expected = "void* foo();\n";
        assert_eq!(result, expected);
    }

    #[test]
    fn should_create_function_returning_custom_type() {
        let function_returning_custom_type = ExternFunction {
            arguments: vec![],
            return_type: Some(WrapperType {
                original_type_name: parse_quote!(SomeCustomType),
                wrapper_name: "SomeCustomType".to_string(),
                rust_type: RustWrapperType::Custom,
                reference_parameters: None,
            }),
            name: "foo".to_owned(),
            tokens: parse_quote!(
                fn foo() -> SomeCustomType;
            ),
        };
        let result = create_extern_imports(&[function_returning_custom_type]);
        let expected = "void* foo();\n";
        assert_eq!(result, expected);
    }
}