cxx_qt_build/qml_modules.rs
1// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
2// SPDX-FileContributor: Be Wilson <be.wilson@kdab.com>
3//
4// SPDX-License-Identifier: MIT OR Apache-2.0
5
6//! This Rust module contains structs for registering QML modules.
7
8use std::path::{Path, PathBuf};
9
10/// Metadata for registering a QML module with [crate::CxxQtBuilder::qml_module]
11pub struct QmlModule<'a, A, B>
12where
13 A: AsRef<Path>,
14 // Use a separate generic to allow using different types that impl AsRef<Path>
15 B: AsRef<Path>,
16{
17 /// The URI of the QML module
18 pub uri: &'a str,
19 /// The major version of the QML module
20 pub version_major: usize,
21 /// The minor version of the QML module
22 pub version_minor: usize,
23 /// The `.rs` files containing a `#[cxx_qt::bridge]` module with at least one QObject type annotated with `#[qml_element]`
24 pub rust_files: &'a [A],
25 /// `.qml` files included in the module
26 pub qml_files: &'a [B],
27 /// Other QRC resources (such as images) included in the module
28 //
29 // Reuse the `A` generic from rust_files to allow the compiler to infer the
30 // type when constructing the struct with Default::default. Using a separate
31 // generic for this field would be more flexible, but it would require users
32 // to explicitly specify the type even for an empty slice (like `&[] as &[&str; 0]`)
33 // and an empty slice is likely desired in most cases; most users probably don't
34 // care about this field.
35 pub qrc_files: &'a [A],
36}
37
38impl<A, B> Default for QmlModule<'_, A, B>
39where
40 A: AsRef<Path>,
41 B: AsRef<Path>,
42{
43 fn default() -> Self {
44 QmlModule {
45 uri: "com.example.cxx_qt_module",
46 version_major: 1,
47 version_minor: 0,
48 rust_files: &[],
49 qml_files: &[],
50 qrc_files: &[],
51 }
52 }
53}
54
55/// Same as [QmlModule], but this struct owns the data instead of referencing it.
56/// This avoids needing to specify generics to instantiate a [crate::CxxQtBuilder], which
57/// contains a `Vec<OwningQmlModule>` member.
58pub(crate) struct OwningQmlModule {
59 pub uri: String,
60 pub version_major: usize,
61 pub version_minor: usize,
62 pub rust_files: Vec<PathBuf>,
63 pub qml_files: Vec<PathBuf>,
64 pub qrc_files: Vec<PathBuf>,
65}
66
67fn collect_pathbuf_vec(asref: &[impl AsRef<Path>]) -> Vec<PathBuf> {
68 asref.iter().map(|p| p.as_ref().to_path_buf()).collect()
69}
70
71impl<A: AsRef<Path>, B: AsRef<Path>> From<QmlModule<'_, A, B>> for OwningQmlModule {
72 fn from(other: QmlModule<'_, A, B>) -> Self {
73 OwningQmlModule {
74 uri: other.uri.to_string(),
75 version_major: other.version_major,
76 version_minor: other.version_minor,
77 rust_files: collect_pathbuf_vec(other.rust_files),
78 qml_files: collect_pathbuf_vec(other.qml_files),
79 qrc_files: collect_pathbuf_vec(other.qrc_files),
80 }
81 }
82}