cxx_qt_lib/core/
qstringlist.rs

1// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
2// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
3//
4// SPDX-License-Identifier: MIT OR Apache-2.0
5use crate::{QList, QString};
6use core::mem::MaybeUninit;
7use cxx::{type_id, ExternType};
8
9#[cxx::bridge]
10mod ffi {
11    #[namespace = "Qt"]
12    unsafe extern "C++" {
13        include!("cxx-qt-lib/qt.h");
14        type CaseSensitivity = crate::CaseSensitivity;
15    }
16
17    unsafe extern "C++" {
18        include!("cxx-qt-lib/qstring.h");
19        type QString = crate::QString;
20
21        include!("cxx-qt-lib/qlist.h");
22        type QList_QString = crate::QList<QString>;
23
24        include!("cxx-qt-lib/qstringlist.h");
25        type QStringList = super::QStringList;
26
27        /// Returns true if the list contains the string str; otherwise returns false.
28        fn contains(self: &QStringList, str: &QString, cs: CaseSensitivity) -> bool;
29
30        /// Returns a list of all the strings containing the substring str.
31        fn filter(self: &QStringList, str: &QString, cs: CaseSensitivity) -> QStringList;
32
33        /// Joins all the string list's strings into a single string with each element
34        /// separated by the given separator (which can be an empty string).
35        fn join(self: &QStringList, separator: &QString) -> QString;
36
37        /// Sorts the list of strings in ascending order.
38        fn sort(self: &mut QStringList, cs: CaseSensitivity);
39
40        /// Returns a string list where every string has had the before text replaced with the after text wherever the before text is found.
41        /// The before text is matched case-sensitively or not depending on the cs flag.
42        #[rust_name = "replace_in_strings"]
43        fn replaceInStrings(
44            self: &mut QStringList,
45            before: &QString,
46            after: &QString,
47            cs: CaseSensitivity,
48        ) -> &mut QStringList;
49    }
50
51    #[namespace = "rust::cxxqtlib1"]
52    unsafe extern "C++" {
53        include!("cxx-qt-lib/common.h");
54
55        #[doc(hidden)]
56        #[rust_name = "qstringlist_clone"]
57        fn construct(list: &QStringList) -> QStringList;
58
59        #[doc(hidden)]
60        #[rust_name = "qstringlist_drop"]
61        fn drop(url: &mut QStringList);
62
63        #[doc(hidden)]
64        #[rust_name = "qstringlist_default"]
65        fn construct() -> QStringList;
66
67        #[doc(hidden)]
68        #[rust_name = "qstringlist_from_qstring"]
69        fn construct(string: &QString) -> QStringList;
70
71        #[doc(hidden)]
72        #[rust_name = "qstringlist_eq"]
73        fn operatorEq(a: &QStringList, b: &QStringList) -> bool;
74
75        #[doc(hidden)]
76        #[rust_name = "qstringlist_to_qstring"]
77        fn toQString(value: &QStringList) -> QString;
78    }
79
80    #[namespace = "rust::cxxqtlib1"]
81    unsafe extern "C++" {
82        #[doc(hidden)]
83        #[rust_name = "qstringlist_from_qlist_qstring"]
84        fn qstringlistFromQListQString(list: &QList_QString) -> QStringList;
85        #[doc(hidden)]
86        #[rust_name = "qstringlist_as_qlist_qstring"]
87        fn qstringlistAsQListQString(list: &QStringList) -> QList_QString;
88        #[doc(hidden)]
89        #[rust_name = "qstringlist_remove_duplicates"]
90        fn qstringlistRemoveDuplicates(list: &mut QStringList) -> isize;
91    }
92}
93
94/// The QStringList class provides a list of strings.
95#[repr(C)]
96pub struct QStringList {
97    /// The layout has changed between Qt 5 and Qt 6
98    ///
99    /// Qt5 QStringList has one pointer as a member
100    /// Qt6 QStringList has one member, which contains two pointers and a size_t
101    #[cfg(cxxqt_qt_version_major = "5")]
102    _space: MaybeUninit<usize>,
103    #[cfg(cxxqt_qt_version_major = "6")]
104    _space: MaybeUninit<[usize; 3]>,
105}
106
107impl QStringList {
108    /// This function removes duplicate entries from a list.
109    /// The entries do not have to be sorted. They will retain their original order.
110    pub fn remove_duplicates(&mut self) -> isize {
111        ffi::qstringlist_remove_duplicates(self)
112    }
113}
114
115impl Clone for QStringList {
116    /// Constructs a copy of other.
117    fn clone(&self) -> Self {
118        ffi::qstringlist_clone(self)
119    }
120}
121
122impl Default for QStringList {
123    /// Constructs an empty list.
124    fn default() -> Self {
125        ffi::qstringlist_default()
126    }
127}
128
129impl std::cmp::PartialEq for QStringList {
130    fn eq(&self, other: &Self) -> bool {
131        ffi::qstringlist_eq(self, other)
132    }
133}
134
135impl std::cmp::Eq for QStringList {}
136
137impl std::fmt::Display for QStringList {
138    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
139        write!(f, "{}", ffi::qstringlist_to_qstring(self))
140    }
141}
142
143impl std::fmt::Debug for QStringList {
144    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
145        write!(f, "{self}")
146    }
147}
148
149impl Drop for QStringList {
150    /// Destroys the list.
151    fn drop(&mut self) {
152        ffi::qstringlist_drop(self);
153    }
154}
155
156impl From<&QString> for QStringList {
157    /// Constructs a string list that contains the given string
158    fn from(string: &QString) -> Self {
159        ffi::qstringlist_from_qstring(string)
160    }
161}
162
163impl From<&QList<QString>> for QStringList {
164    /// Converts a `QList<QString>` into QStringList.
165    fn from(list: &QList<QString>) -> Self {
166        ffi::qstringlist_from_qlist_qstring(list)
167    }
168}
169
170impl From<&QStringList> for QList<QString> {
171    /// Converts a QStringList into a `QList<QString>`
172    fn from(list: &QStringList) -> Self {
173        ffi::qstringlist_as_qlist_qstring(list)
174    }
175}
176
177// Safety:
178//
179// Static checks on the C++ side to ensure the size is the same.
180unsafe impl ExternType for QStringList {
181    type Id = type_id!("QStringList");
182    type Kind = cxx::kind::Trivial;
183}