Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(qmetaobject): add "is" wrappers for special QJSValues #224

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 63 additions & 5 deletions qmetaobject/src/qtdeclarative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,11 +506,7 @@ pub fn qml_register_type<T: QObject + Default + Sized>(
///
/// [qt]: https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterModule
#[cfg(qt_5_9)]
pub fn qml_register_module(
uri: &CStr,
version_major: u32,
version_minor: u32,
) {
pub fn qml_register_module(uri: &CStr, version_major: u32, version_minor: u32) {
gikari marked this conversation as resolved.
Show resolved Hide resolved
let uri_ptr = uri.as_ptr();

cpp!(unsafe [
Expand Down Expand Up @@ -926,13 +922,41 @@ 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 {
gikari marked this conversation as resolved.
Show resolved Hide resolved
NullValue = 0,
UndefinedValue = 1,
}

impl QJSValue {
pub fn null() -> Self {
cpp!(unsafe [] -> QJSValue as "QJSValue" {
return QJSValue(QJSValue::SpecialValue::NullValue);
})
}

pub fn undefined() -> Self {
cpp!(unsafe [] -> QJSValue as "QJSValue" {
return QJSValue(QJSValue::SpecialValue::UndefinedValue);
})
}

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 +969,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 +1056,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 +1091,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 +1118,17 @@ mod qjsvalue_tests {
assert!(!num_value.is_string());
}

#[test]
fn test_is_undefined() {
let undefined_value = QJSValue::from(QJSValueSpecialValue::UndefinedValue);
let default_value = QJSValue::default();
let num_value = QJSValue::from(42);

assert!(undefined_value.is_undefined());
assert!(default_value.is_undefined());
assert!(!num_value.is_undefined());
}

#[test]
fn test_qvariantlist_from_iter() {
let v = vec![1u32, 2u32, 3u32];
Expand Down