Skip to content

Commit 7f8c6d6

Browse files
Mikhail Zolotukhinogoffart
authored andcommitted
feat(qmetaobject): add basic "is" wrappers for QJSValue
1 parent e912556 commit 7f8c6d6

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

qmetaobject/src/qtdeclarative.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,24 @@ cpp_class!(
927927
);
928928

929929
impl QJSValue {
930+
pub fn is_bool(&self) -> bool {
931+
cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" {
932+
return self->isBool();
933+
})
934+
}
935+
936+
pub fn is_number(&self) -> bool {
937+
cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" {
938+
return self->isNumber();
939+
})
940+
}
941+
942+
pub fn is_string(&self) -> bool {
943+
cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" {
944+
return self->isString();
945+
})
946+
}
947+
930948
pub fn to_string(&self) -> QString {
931949
cpp!(unsafe [self as "const QJSValue *"] -> QString as "QString" {
932950
return self->toString();
@@ -1026,6 +1044,33 @@ mod qjsvalue_tests {
10261044
assert_eq!(foo.to_variant().to_qbytearray(), "45".into());
10271045
}
10281046

1047+
#[test]
1048+
fn test_is_bool() {
1049+
let bool_value = QJSValue::from(true);
1050+
let num_value = QJSValue::from(42);
1051+
1052+
assert!(bool_value.is_bool());
1053+
assert!(!num_value.is_bool());
1054+
}
1055+
1056+
#[test]
1057+
fn test_is_number() {
1058+
let string_value = QJSValue::from(QString::from("Konqui"));
1059+
let num_value = QJSValue::from(42);
1060+
1061+
assert!(num_value.is_number());
1062+
assert!(!string_value.is_number());
1063+
}
1064+
1065+
#[test]
1066+
fn test_is_string() {
1067+
let string_value = QJSValue::from(QString::from("Konqui"));
1068+
let num_value = QJSValue::from(42);
1069+
1070+
assert!(string_value.is_string());
1071+
assert!(!num_value.is_string());
1072+
}
1073+
10291074
#[test]
10301075
fn test_qvariantlist_from_iter() {
10311076
let v = vec![1u32, 2u32, 3u32];

0 commit comments

Comments
 (0)