Skip to main content

cxx_qt_lib/core/
qjsonarray.rs

1// SPDX-FileCopyrightText: 2026 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
2// SPDX-FileContributor: Yuri Knigavko <yuri.knigavko@qt.io>
3//
4// SPDX-License-Identifier: MIT OR Apache-2.0
5
6use crate::QJsonValue;
7use std::fmt;
8use std::mem::MaybeUninit;
9
10#[cxx::bridge]
11mod ffi {
12    unsafe extern "C++" {
13        include!("cxx-qt-lib/qjsonarray.h");
14        type QJsonArray = super::QJsonArray;
15
16        include!("cxx-qt-lib/qjsonvalue.h");
17        type QJsonValue = crate::QJsonValue;
18
19        include!("cxx-qt-lib/qstring.h");
20        type QString = crate::QString;
21
22        /// Inserts `value` at the end of the array.
23        fn append(self: &mut QJsonArray, value: &QJsonValue);
24    }
25
26    #[namespace = "rust::cxxqtlib1"]
27    unsafe extern "C++" {
28        include!("cxx-qt-lib/common.h");
29
30        #[doc(hidden)]
31        #[rust_name = "qjsonarray_drop"]
32        fn drop(qjsonarray: &mut QJsonArray);
33
34        #[doc(hidden)]
35        #[rust_name = "qjsonarray_init_default"]
36        fn construct() -> QJsonArray;
37
38        #[doc(hidden)]
39        #[rust_name = "qjsonarray_init_from_qjsonarray"]
40        fn construct(qjsonarray: &QJsonArray) -> QJsonArray;
41
42        #[doc(hidden)]
43        #[rust_name = "qjsonarray_eq"]
44        fn operatorEq(a: &QJsonArray, b: &QJsonArray) -> bool;
45
46        #[doc(hidden)]
47        #[rust_name = "qjsonarray_to_debug_qstring"]
48        fn toDebugQString(array: &QJsonArray) -> QString;
49
50        #[doc(hidden)]
51        #[rust_name = "qjsonarray_len"]
52        fn qjsonarrayLen(array: &QJsonArray) -> isize;
53
54        #[doc(hidden)]
55        #[rust_name = "qjsonarray_at"]
56        fn qjsonarrayAt(array: &QJsonArray, i: isize) -> QJsonValue;
57    }
58}
59
60/// The `QJsonArray` class encapsulates a JSON array.
61///
62/// Qt Documentation: [QJsonArray](https://doc.qt.io/qt/qjsonarray.html#details)
63#[repr(C)]
64pub struct QJsonArray {
65    #[cfg(cxxqt_qt_version_major = "5")]
66    _d: MaybeUninit<usize>,
67    _a: MaybeUninit<usize>,
68}
69
70impl Drop for QJsonArray {
71    fn drop(&mut self) {
72        ffi::qjsonarray_drop(self);
73    }
74}
75
76impl Default for QJsonArray {
77    fn default() -> Self {
78        ffi::qjsonarray_init_default()
79    }
80}
81
82impl Clone for QJsonArray {
83    fn clone(&self) -> Self {
84        ffi::qjsonarray_init_from_qjsonarray(self)
85    }
86}
87
88impl PartialEq for QJsonArray {
89    fn eq(&self, other: &Self) -> bool {
90        ffi::qjsonarray_eq(self, other)
91    }
92}
93
94impl fmt::Debug for QJsonArray {
95    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96        ffi::qjsonarray_to_debug_qstring(self).fmt(f)
97    }
98}
99
100// Safety:
101//
102// Static checks on the C++ side ensure that QJsonArray is trivial.
103unsafe impl cxx::ExternType for QJsonArray {
104    type Id = cxx::type_id!("QJsonArray");
105    type Kind = cxx::kind::Trivial;
106}
107
108impl QJsonArray {
109    /// Returns the number of elements in the array.
110    pub fn len(&self) -> isize {
111        ffi::qjsonarray_len(self)
112    }
113
114    /// Returns `true` if the array is empty.
115    pub fn is_empty(&self) -> bool {
116        self.len() == 0
117    }
118
119    /// Returns the element at position `i`.
120    /// The returned QJsonValue is Undefined, if `i` is out of bounds.
121    pub fn at(&self, i: isize) -> QJsonValue {
122        ffi::qjsonarray_at(self, i)
123    }
124
125    /// Returns an iterator over the elements of the array.
126    pub fn iter(&self) -> Iter<'_> {
127        Iter {
128            array: self,
129            index: 0,
130        }
131    }
132}
133
134/// An iterator over the elements of a [`QJsonArray`].
135///
136/// This struct is created by [`QJsonArray::iter`].
137pub struct Iter<'a> {
138    array: &'a QJsonArray,
139    index: isize,
140}
141
142impl Iterator for Iter<'_> {
143    type Item = QJsonValue;
144
145    fn next(&mut self) -> Option<Self::Item> {
146        if self.index >= self.array.len() {
147            return None;
148        }
149        let next = self.array.at(self.index);
150        self.index += 1;
151        Some(next)
152    }
153
154    fn size_hint(&self) -> (usize, Option<usize>) {
155        let len = self.len();
156        (len, Some(len))
157    }
158}
159
160impl ExactSizeIterator for Iter<'_> {
161    fn len(&self) -> usize {
162        (self.array.len() - self.index) as usize
163    }
164}
165
166impl<'a> IntoIterator for &'a QJsonArray {
167    type Item = QJsonValue;
168    type IntoIter = Iter<'a>;
169
170    fn into_iter(self) -> Self::IntoIter {
171        self.iter()
172    }
173}