Skip to content

Commit

Permalink
feat(qmetaobject): add "is" wrappers for special QJSValues
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Zolotukhin committed Jan 8, 2022
1 parent 7f8c6d6 commit f1dd1d4
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions qmetaobject/src/qtdeclarative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,13 +926,29 @@ 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" {
return self->isBool();
})
}

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();
Expand All @@ -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();
Expand Down Expand Up @@ -1026,6 +1048,14 @@ impl From<bool> for QJSValue {
}
}

impl From<QJSValueSpecialValue> 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<QJSValue>(); })
Expand Down Expand Up @@ -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"));
Expand All @@ -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];
Expand Down

0 comments on commit f1dd1d4

Please sign in to comment.