Skip to content

Commit 5c3f034

Browse files
author
Mikhail Zolotukhin
committed
feat(qmetaobject): add "is" wrappers for special QJSValues
1 parent 791f4b9 commit 5c3f034

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
@@ -922,13 +922,29 @@ cpp_class!(
922922
pub unsafe struct QJSValue as "QJSValue"
923923
);
924924

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

942+
pub fn is_null(&self) -> bool {
943+
cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" {
944+
return self->isNull();
945+
})
946+
}
947+
932948
pub fn is_number(&self) -> bool {
933949
cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" {
934950
return self->isNumber();
@@ -941,6 +957,12 @@ impl QJSValue {
941957
})
942958
}
943959

960+
pub fn is_undefined(&self) -> bool {
961+
cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" {
962+
return self->isUndefined();
963+
})
964+
}
965+
944966
pub fn to_string(&self) -> QString {
945967
cpp!(unsafe [self as "const QJSValue *"] -> QString as "QString" {
946968
return self->toString();
@@ -1022,6 +1044,14 @@ impl From<bool> for QJSValue {
10221044
}
10231045
}
10241046

1047+
impl From<QJSValueSpecialValue> for QJSValue {
1048+
fn from(a: QJSValueSpecialValue) -> QJSValue {
1049+
cpp!(unsafe [a as "QJSValue::SpecialValue"] -> QJSValue as "QJSValue" {
1050+
return QJSValue(a);
1051+
})
1052+
}
1053+
}
1054+
10251055
impl QMetaType for QJSValue {
10261056
fn register(_name: Option<&CStr>) -> i32 {
10271057
cpp!(unsafe [] -> i32 as "int" { return qMetaTypeId<QJSValue>(); })
@@ -1049,6 +1079,15 @@ mod qjsvalue_tests {
10491079
assert!(!num_value.is_bool());
10501080
}
10511081

1082+
#[test]
1083+
fn test_is_null() {
1084+
let null_value = QJSValue::from(QJSValueSpecialValue::NullValue);
1085+
let num_value = QJSValue::from(42);
1086+
1087+
assert!(null_value.is_null());
1088+
assert!(!num_value.is_null());
1089+
}
1090+
10521091
#[test]
10531092
fn test_is_number() {
10541093
let string_value = QJSValue::from(QString::from("Konqui"));
@@ -1067,6 +1106,15 @@ mod qjsvalue_tests {
10671106
assert!(!num_value.is_string());
10681107
}
10691108

1109+
#[test]
1110+
fn test_is_undefined() {
1111+
let undefined_value = QJSValue::from(QJSValueSpecialValue::UndefinedValue);
1112+
let num_value = QJSValue::from(42);
1113+
1114+
assert!(undefined_value.is_undefined());
1115+
assert!(!num_value.is_undefined());
1116+
}
1117+
10701118
#[test]
10711119
fn test_qvariantlist_from_iter() {
10721120
let v = vec![1u32, 2u32, 3u32];

0 commit comments

Comments
 (0)