Skip to content

Commit 2ec40ea

Browse files
authored
cxx-qt-lib: add bindings for QJsonValue methods toTypeX accepting a default value (#1479)
Use these overloads instead of auxiliary helper functions that provide default-value handling.
1 parent f7851cd commit 2ec40ea

3 files changed

Lines changed: 42 additions & 78 deletions

File tree

crates/cxx-qt-lib/include/core/qjsonvalue.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,6 @@ namespace cxxqtlib1 {
2727

2828
QJsonValue
2929
qjsonvalueFromI64(::rust::i64 value);
30-
bool
31-
qjsonvalueToBool(const QJsonValue& value);
32-
::rust::f64
33-
qjsonvalueToDouble(const QJsonValue& value);
34-
::rust::i32
35-
qjsonvalueToInt(const QJsonValue& value);
36-
QString
37-
qjsonvalueToQString(const QJsonValue& value);
38-
QJsonArray
39-
qjsonvalueToArray(const QJsonValue& value);
40-
QJsonObject
41-
qjsonvalueToObject(const QJsonValue& value);
4230

4331
} // namespace cxxqtlib1
4432
} // namespace rust

crates/cxx-qt-lib/src/core/qjsonvalue.cpp

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -49,41 +49,5 @@ qjsonvalueFromI64(::rust::i64 value)
4949
return QJsonValue(static_cast<qint64>(value));
5050
}
5151

52-
bool
53-
qjsonvalueToBool(const QJsonValue& value)
54-
{
55-
return value.toBool(false);
56-
}
57-
58-
::rust::f64
59-
qjsonvalueToDouble(const QJsonValue& value)
60-
{
61-
return value.toDouble(0.0);
62-
}
63-
64-
::rust::i32
65-
qjsonvalueToInt(const QJsonValue& value)
66-
{
67-
return static_cast<::rust::i32>(value.toInt(0));
68-
}
69-
70-
QString
71-
qjsonvalueToQString(const QJsonValue& value)
72-
{
73-
return value.toString(QString{});
74-
}
75-
76-
QJsonArray
77-
qjsonvalueToArray(const QJsonValue& value)
78-
{
79-
return value.toArray(QJsonArray{});
80-
}
81-
82-
QJsonObject
83-
qjsonvalueToObject(const QJsonValue& value)
84-
{
85-
return value.toObject(QJsonObject{});
86-
}
87-
8852
} // namespace cxxqtlib1
8953
} // namespace rust

crates/cxx-qt-lib/src/core/qjsonvalue.rs

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,42 @@ mod ffi {
5050
/// Returns `true` if the value contains an object.
5151
#[rust_name = "is_object"]
5252
fn isObject(self: &QJsonValue) -> bool;
53+
54+
/// Converts the value to a `bool` and returns it.
55+
///
56+
/// If the value does not contain a boolean, `default_value` is returned.
57+
#[rust_name = "to_bool_or"]
58+
fn toBool(self: &QJsonValue, default_value: bool) -> bool;
59+
60+
/// Converts the value to a `f64` and returns it.
61+
///
62+
/// If the value does not contain a double, `default_value` is returned.
63+
#[rust_name = "to_double_or"]
64+
fn toDouble(self: &QJsonValue, default_value: f64) -> f64;
65+
66+
/// Converts the value to an `i32` and returns it.
67+
///
68+
/// If the value does not contain a double, or is not a whole number, `default_value` is returned.
69+
#[rust_name = "to_int_or"]
70+
fn toInt(self: &QJsonValue, default_value: i32) -> i32;
71+
72+
/// Converts the value to a [`QString`] and returns it.
73+
///
74+
/// If the value does not contain a string, `default_value` is returned.
75+
#[rust_name = "to_string_or"]
76+
fn toString(self: &QJsonValue, default_value: &QString) -> QString;
77+
78+
/// Converts the value to a [`QJsonArray`] and returns it.
79+
///
80+
/// If the value does not contain an array, `default_value` is returned.
81+
#[rust_name = "to_array_or"]
82+
fn toArray(self: &QJsonValue, default_value: &QJsonArray) -> QJsonArray;
83+
84+
/// Converts the value to a [`QJsonObject`] and returns it.
85+
///
86+
/// If the value does not contain an object, `default_value` is returned.
87+
#[rust_name = "to_object_or"]
88+
fn toObject(self: &QJsonValue, default_value: &QJsonObject) -> QJsonObject;
5389
}
5490

5591
#[namespace = "rust::cxxqtlib1"]
@@ -99,30 +135,6 @@ mod ffi {
99135
#[doc(hidden)]
100136
#[rust_name = "qjsonvalue_to_debug_qstring"]
101137
fn toDebugQString(value: &QJsonValue) -> QString;
102-
103-
#[doc(hidden)]
104-
#[rust_name = "to_bool"]
105-
fn qjsonvalueToBool(value: &QJsonValue) -> bool;
106-
107-
#[doc(hidden)]
108-
#[rust_name = "to_double"]
109-
fn qjsonvalueToDouble(value: &QJsonValue) -> f64;
110-
111-
#[doc(hidden)]
112-
#[rust_name = "to_int"]
113-
fn qjsonvalueToInt(value: &QJsonValue) -> i32;
114-
115-
#[doc(hidden)]
116-
#[rust_name = "to_string"]
117-
fn qjsonvalueToQString(value: &QJsonValue) -> QString;
118-
119-
#[doc(hidden)]
120-
#[rust_name = "to_array"]
121-
fn qjsonvalueToArray(value: &QJsonValue) -> QJsonArray;
122-
123-
#[doc(hidden)]
124-
#[rust_name = "to_object"]
125-
fn qjsonvalueToObject(value: &QJsonValue) -> QJsonObject;
126138
}
127139
}
128140

@@ -216,34 +228,34 @@ unsafe impl cxx::ExternType for QJsonValue {
216228
impl QJsonValue {
217229
/// Converts the value to a `bool`. Returns `false` if the value is not a boolean.
218230
pub fn to_bool(&self) -> bool {
219-
ffi::to_bool(self)
231+
self.to_bool_or(false)
220232
}
221233

222234
/// Converts the value to a `f64`. Returns `0.0` if the value is not a double.
223235
pub fn to_double(&self) -> f64 {
224-
ffi::to_double(self)
236+
self.to_double_or(0.0)
225237
}
226238

227239
/// Converts the value to a `i32`. Returns `0` if the value is not an integer.
228240
pub fn to_int(&self) -> i32 {
229-
ffi::to_int(self)
241+
self.to_int_or(0)
230242
}
231243

232244
/// Converts the value to a [`QString`]. Returns an empty string if the value is
233245
/// not a string.
234246
pub fn to_string(&self) -> QString {
235-
ffi::to_string(self)
247+
self.to_string_or(&QString::default())
236248
}
237249

238250
/// Converts the value to a [`QJsonArray`]. Returns an empty array if the value
239251
/// is not an array.
240252
pub fn to_array(&self) -> QJsonArray {
241-
ffi::to_array(self)
253+
self.to_array_or(&QJsonArray::default())
242254
}
243255

244256
/// Converts the value to a [`QJsonObject`]. Returns an empty object if the value
245257
/// is not an object.
246258
pub fn to_object(&self) -> QJsonObject {
247-
ffi::to_object(self)
259+
self.to_object_or(&QJsonObject::default())
248260
}
249261
}

0 commit comments

Comments
 (0)