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
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

pub mod field;
pub mod fragment;
pub mod invokable;
pub mod property;
pub mod qobject;
pub mod signals;

use crate::generator::rust::qobject::GeneratedRustQObject;
use crate::parser::Parser;
use quote::quote;
use syn::{Item, ItemMod, Result};

/// Representation of the generated Rust code for a QObject
pub struct GeneratedRustBlocks {
    /// Module for the CXX bridge with passthrough items
    pub cxx_mod: ItemMod,
    /// Any global extra items for the CXX bridge
    pub cxx_mod_contents: Vec<Item>,
    /// Any global passthrough items for the implementation
    pub cxx_qt_mod_contents: Vec<Item>,
    /// Ident of the namespace of the QObject
    pub namespace: String,
    /// Generated QObject blocks
    pub qobjects: Vec<GeneratedRustQObject>,
}

impl GeneratedRustBlocks {
    pub fn from(parser: &Parser) -> Result<GeneratedRustBlocks> {
        Ok(GeneratedRustBlocks {
            cxx_mod: parser.passthrough_module.clone(),
            cxx_mod_contents: vec![generate_include(parser)?],
            cxx_qt_mod_contents: parser.cxx_qt_data.uses.clone(),
            namespace: parser.cxx_qt_data.namespace.clone(),
            qobjects: parser
                .cxx_qt_data
                .qobjects
                .values()
                .map(GeneratedRustQObject::from)
                .collect::<Result<Vec<GeneratedRustQObject>>>()?,
        })
    }
}

/// Generate the include line for this parsed block
fn generate_include(parser: &Parser) -> Result<Item> {
    let import_path = format!("cxx-qt-gen/{}.cxxqt.h", parser.cxx_file_stem);

    syn::parse2(quote! {
        unsafe extern "C++" {
            include!(#import_path);
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::tests::{assert_tokens_eq, tokens_to_syn};

    #[test]
    fn test_generated_rust_blocks() {
        let module: ItemMod = tokens_to_syn(quote! {
            #[cxx_qt::bridge]
            mod ffi {
                #[cxx_qt::qobject]
                struct MyObject;
            }
        });
        let parser = Parser::from(module).unwrap();

        let rust = GeneratedRustBlocks::from(&parser).unwrap();
        assert_eq!(rust.cxx_mod.content.unwrap().1.len(), 0);
        assert_eq!(rust.cxx_mod_contents.len(), 1);
        assert_tokens_eq(
            &rust.cxx_mod_contents[0],
            quote! {
                unsafe extern "C++" {
                    include!("cxx-qt-gen/ffi.cxxqt.h");
                }
            },
        );
        assert_eq!(rust.cxx_qt_mod_contents.len(), 0);
        assert_eq!(rust.namespace, "");
        assert_eq!(rust.qobjects.len(), 1);
    }

    #[test]
    fn test_generated_rust_blocks_namespace() {
        let module: ItemMod = tokens_to_syn(quote! {
            #[cxx_qt::bridge(namespace = "cxx_qt")]
            mod ffi {
                #[cxx_qt::qobject]
                struct MyObject;
            }
        });
        let parser = Parser::from(module).unwrap();

        let rust = GeneratedRustBlocks::from(&parser).unwrap();
        assert_eq!(rust.cxx_mod.content.unwrap().1.len(), 0);
        assert_eq!(rust.cxx_mod_contents.len(), 1);
        assert_eq!(rust.cxx_qt_mod_contents.len(), 0);
        assert_eq!(rust.namespace, "cxx_qt");
        assert_eq!(rust.qobjects.len(), 1);
    }

    #[test]
    fn test_generated_rust_blocks_uses() {
        let module: ItemMod = tokens_to_syn(quote! {
            #[cxx_qt::bridge(namespace = "cxx_qt")]
            mod ffi {
                use std::collections::HashMap;

                #[cxx_qt::qobject]
                struct MyObject;
            }
        });
        let parser = Parser::from(module).unwrap();

        let rust = GeneratedRustBlocks::from(&parser).unwrap();
        assert_eq!(rust.cxx_mod.content.unwrap().1.len(), 0);
        assert_eq!(rust.cxx_mod_contents.len(), 1);
        assert_eq!(rust.cxx_qt_mod_contents.len(), 1);
        assert_eq!(rust.namespace, "cxx_qt");
        assert_eq!(rust.qobjects.len(), 1);
    }

    #[test]
    fn test_generated_rust_blocks_cxx_file_stem() {
        let module: ItemMod = tokens_to_syn(quote! {
            #[cxx_qt::bridge(cxx_file_stem = "my_object")]
            mod ffi {
                #[cxx_qt::qobject]
                struct MyObject;
            }
        });
        let parser = Parser::from(module).unwrap();

        let rust = GeneratedRustBlocks::from(&parser).unwrap();
        assert_eq!(rust.cxx_mod.content.unwrap().1.len(), 0);
        assert_eq!(rust.cxx_mod_contents.len(), 1);
        assert_tokens_eq(
            &rust.cxx_mod_contents[0],
            quote! {
                unsafe extern "C++" {
                    include!("cxx-qt-gen/my_object.cxxqt.h");
                }
            },
        );
        assert_eq!(rust.cxx_qt_mod_contents.len(), 0);
        assert_eq!(rust.namespace, "");
        assert_eq!(rust.qobjects.len(), 1);
    }
}