Skip to content

Commit f1dd1d4

Browse files
author
Mikhail Zolotukhin
committed
feat(qmetaobject): add "is" wrappers for special QJSValues
1 parent 7f8c6d6 commit f1dd1d4

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

qmetaobject/src/qtdeclarative.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,13 +926,29 @@ cpp_class!(
926926
pub unsafe struct QJSValue as "QJSValue"
927927
);
928928

929+
/// Wrapper for [`QJSValue::SpecialValue`][qt]
930+
///
931+
/// [qt]: https://doc.qt.io/qt-5/qjsvalue.html#SpecialValue-enum
932+
#[repr(u32)]
933+
#[derive(Clone, Copy, Debug, PartialEq)]
934+
pub enum QJSValueSpecialValue {
935+
NullValue = 0,
936+
UndefinedValue = 1,
937+
}
938+
929939
impl QJSValue {
930940
pub fn is_bool(&self) -> bool {
931941
cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" {
932942
return self->isBool();
933943
})
934944
}
935945

946+
pub fn is_null(&self) -> bool {
947+
cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" {
948+
return self->isNull();
949+
})
950+
}
951+
936952
pub fn is_number(&self) -> bool {
937953
cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" {
938954
return self->isNumber();
@@ -945,6 +961,12 @@ impl QJSValue {
945961
})
946962
}
947963

964+
pub fn is_undefined(&self) -> bool {
965+
cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" {
966+
return self->isUndefined();
967+
})
968+
}
969+
948970
pub fn to_string(&self) -> QString {
949971
cpp!(unsafe [self as "const QJSValue *"] -> QString as "QString" {
950972
return self->toString();
@@ -1026,6 +1048,14 @@ impl From<bool> for QJSValue {
10261048
}
10271049
}
10281050

1051+
impl From<QJSValueSpecialValue> for QJSValue {
1052+
fn from(a: QJSValueSpecialValue) -> QJSValue {
1053+
cpp!(unsafe [a as "QJSValue::SpecialValue"] -> QJSValue as "QJSValue" {
1054+
return QJSValue(a);
1055+
})
1056+
}
1057+
}
1058+
10291059
impl QMetaType for QJSValue {
10301060
fn register(_name: Option<&CStr>) -> i32 {
10311061
cpp!(unsafe [] -> i32 as "int" { return qMetaTypeId<QJSValue>(); })
@@ -1053,6 +1083,15 @@ mod qjsvalue_tests {
10531083
assert!(!num_value.is_bool());
10541084
}
10551085

1086+
#[test]
1087+
fn test_is_null() {
1088+
let null_value = QJSValue::from(QJSValueSpecialValue::NullValue);
1089+
let num_value = QJSValue::from(42);
1090+
1091+
assert!(null_value.is_null());
1092+
assert!(!num_value.is_null());
1093+
}
1094+
10561095
#[test]
10571096
fn test_is_number() {
10581097
let string_value = QJSValue::from(QString::from("Konqui"));
@@ -1071,6 +1110,15 @@ mod qjsvalue_tests {
10711110
assert!(!num_value.is_string());
10721111
}
10731112

1113+
#[test]
1114+
fn test_is_undefined() {
1115+
let undefined_value = QJSValue::from(QJSValueSpecialValue::UndefinedValue);
1116+
let num_value = QJSValue::from(42);
1117+
1118+
assert!(undefined_value.is_undefined());
1119+
assert!(!num_value.is_undefined());
1120+
}
1121+
10741122
#[test]
10751123
fn test_qvariantlist_from_iter() {
10761124
let v = vec![1u32, 2u32, 3u32];

0 commit comments

Comments
 (0)