From f1dd1d4eacac71e1e100057d0958f853acbfd61d Mon Sep 17 00:00:00 2001 From: Mikhail Zolotukhin Date: Thu, 6 Jan 2022 16:20:57 +0300 Subject: [PATCH] feat(qmetaobject): add "is" wrappers for special QJSValues --- qmetaobject/src/qtdeclarative.rs | 48 ++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/qmetaobject/src/qtdeclarative.rs b/qmetaobject/src/qtdeclarative.rs index 55605b40..41520cb4 100644 --- a/qmetaobject/src/qtdeclarative.rs +++ b/qmetaobject/src/qtdeclarative.rs @@ -926,6 +926,16 @@ cpp_class!( pub unsafe struct QJSValue as "QJSValue" ); +/// Wrapper for [`QJSValue::SpecialValue`][qt] +/// +/// [qt]: https://doc.qt.io/qt-5/qjsvalue.html#SpecialValue-enum +#[repr(u32)] +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum QJSValueSpecialValue { + NullValue = 0, + UndefinedValue = 1, +} + impl QJSValue { pub fn is_bool(&self) -> bool { cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" { @@ -933,6 +943,12 @@ impl QJSValue { }) } + pub fn is_null(&self) -> bool { + cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" { + return self->isNull(); + }) + } + pub fn is_number(&self) -> bool { cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" { return self->isNumber(); @@ -945,6 +961,12 @@ impl QJSValue { }) } + pub fn is_undefined(&self) -> bool { + cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" { + return self->isUndefined(); + }) + } + pub fn to_string(&self) -> QString { cpp!(unsafe [self as "const QJSValue *"] -> QString as "QString" { return self->toString(); @@ -1026,6 +1048,14 @@ impl From for QJSValue { } } +impl From for QJSValue { + fn from(a: QJSValueSpecialValue) -> QJSValue { + cpp!(unsafe [a as "QJSValue::SpecialValue"] -> QJSValue as "QJSValue" { + return QJSValue(a); + }) + } +} + impl QMetaType for QJSValue { fn register(_name: Option<&CStr>) -> i32 { cpp!(unsafe [] -> i32 as "int" { return qMetaTypeId(); }) @@ -1053,6 +1083,15 @@ mod qjsvalue_tests { assert!(!num_value.is_bool()); } + #[test] + fn test_is_null() { + let null_value = QJSValue::from(QJSValueSpecialValue::NullValue); + let num_value = QJSValue::from(42); + + assert!(null_value.is_null()); + assert!(!num_value.is_null()); + } + #[test] fn test_is_number() { let string_value = QJSValue::from(QString::from("Konqui")); @@ -1071,6 +1110,15 @@ mod qjsvalue_tests { assert!(!num_value.is_string()); } + #[test] + fn test_is_undefined() { + let undefined_value = QJSValue::from(QJSValueSpecialValue::UndefinedValue); + let num_value = QJSValue::from(42); + + assert!(undefined_value.is_undefined()); + assert!(!num_value.is_undefined()); + } + #[test] fn test_qvariantlist_from_iter() { let v = vec![1u32, 2u32, 3u32];