forked from KDAB/cxx-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
106 lines (94 loc) · 3.06 KB
/
mod.rs
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
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
mod constructor;
pub mod fragment;
pub mod inherit;
pub mod invokable;
pub mod property;
pub mod qobject;
pub mod signal;
pub mod threading;
pub mod types;
use crate::parser::Parser;
use qobject::GeneratedCppQObject;
use syn::Result;
/// Representation of the generated C++ code for a group of QObjects
pub struct GeneratedCppBlocks {
/// Stem of the CXX header to include
pub cxx_file_stem: String,
/// Ident of the common namespace of the QObjects
pub namespace: String,
/// Generated QObjects
pub qobjects: Vec<GeneratedCppQObject>,
}
impl GeneratedCppBlocks {
pub fn from(parser: &Parser) -> Result<GeneratedCppBlocks> {
Ok(GeneratedCppBlocks {
cxx_file_stem: parser.cxx_file_stem.clone(),
namespace: parser.cxx_qt_data.namespace.clone(),
qobjects: parser
.cxx_qt_data
.qobjects
.values()
.map(|qobject| GeneratedCppQObject::from(qobject, &parser.cxx_qt_data.cxx_mappings))
.collect::<Result<Vec<GeneratedCppQObject>>>()?,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parser::Parser;
use syn::{parse_quote, ItemMod};
#[test]
fn test_generated_cpp_blocks() {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge]
mod ffi {
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObjectRust;
}
}
};
let parser = Parser::from(module).unwrap();
let cpp = GeneratedCppBlocks::from(&parser).unwrap();
assert_eq!(cpp.cxx_file_stem, "ffi");
assert_eq!(cpp.namespace, "");
assert_eq!(cpp.qobjects.len(), 1);
}
#[test]
fn test_generated_cpp_blocks_cxx_file_stem() {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(cxx_file_stem = "my_object")]
mod ffi {
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObjectRust;
}
}
};
let parser = Parser::from(module).unwrap();
let cpp = GeneratedCppBlocks::from(&parser).unwrap();
assert_eq!(cpp.cxx_file_stem, "my_object");
assert_eq!(cpp.namespace, "");
assert_eq!(cpp.qobjects.len(), 1);
}
#[test]
fn test_generated_cpp_blocks_namespace() {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(namespace = "cxx_qt")]
mod ffi {
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObjectRust;
}
}
};
let parser = Parser::from(module).unwrap();
let cpp = GeneratedCppBlocks::from(&parser).unwrap();
assert_eq!(cpp.namespace, "cxx_qt");
}
}