@ts-for-gir/templates 4.0.0-beta.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +24 -0
- package/templates/README-GJS.md +129 -0
- package/templates/README.md +88 -0
- package/templates/cogl-2-0.d.ts +1 -0
- package/templates/gi.d.ts +8 -0
- package/templates/gimp-3.0.d.ts +24 -0
- package/templates/gio-2.0.d.ts +37 -0
- package/templates/gjs/cairo-1.0.d.ts +0 -0
- package/templates/gjs/cairo.d.ts +855 -0
- package/templates/gjs/cairo.js +4 -0
- package/templates/gjs/console.d.ts +30 -0
- package/templates/gjs/console.js +4 -0
- package/templates/gjs/dom.d.ts +312 -0
- package/templates/gjs/dom.js +1 -0
- package/templates/gjs/gettext.d.ts +48 -0
- package/templates/gjs/gettext.js +4 -0
- package/templates/gjs/gjs-ambient.d.ts +26 -0
- package/templates/gjs/gjs-ambient.js +1 -0
- package/templates/gjs/gjs.d.ts +753 -0
- package/templates/gjs/gjs.js +4 -0
- package/templates/gjs/system.d.ts +187 -0
- package/templates/gjs/system.js +4 -0
- package/templates/glib-2.0.d.ts +1125 -0
- package/templates/gobject-2.0.d.ts +361 -0
- package/templates/granite-1.0.d.ts +8 -0
- package/templates/granite-7.0.d.ts +8 -0
- package/templates/gstbase-0.10.d.ts +6 -0
- package/templates/index-locally.d.ts +8 -0
- package/templates/index.d.ts +17 -0
- package/templates/index.js +4 -0
- package/templates/module-ambient.d.ts +18 -0
- package/templates/module-ambient.js +1 -0
- package/templates/module-import.d.ts +16 -0
- package/templates/module-import.js +2 -0
- package/templates/module.append.d.ts +17 -0
- package/templates/module.d.ts +53 -0
- package/templates/module.js +4 -0
- package/templates/package.json +95 -0
- package/templates/rygelserver-2.6.d.ts +4 -0
- package/templates/tsconfig.json +32 -0
- package/templates/typedoc.json +26 -0
|
@@ -0,0 +1,1125 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
|
|
3
|
+
<%_ if(!noAdvancedVariants){ -%>
|
|
4
|
+
// Advanced variant type inference for GLib.Variant
|
|
5
|
+
// Provides sophisticated type-level parsing of GVariant type signatures
|
|
6
|
+
// enabling automatic TypeScript type inference for variant operations.
|
|
7
|
+
//
|
|
8
|
+
// Variant parsing inspired by https://github.com/jamiebuilds/json-parser-in-typescript-very-bad-idea-please-dont-use.
|
|
9
|
+
//
|
|
10
|
+
// When disabled, basic Variant types from introspection are used instead.
|
|
11
|
+
// This reduces compilation time but loses advanced type safety features.
|
|
12
|
+
|
|
13
|
+
// Utility types for variant parsing
|
|
14
|
+
type VariantTypeError<T extends string> = { error: true } & T;
|
|
15
|
+
|
|
16
|
+
// === Core parsing utilities ===
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Maps basic GVariant type characters to TypeScript types
|
|
20
|
+
*/
|
|
21
|
+
type BasicTypeMap<T extends string> =
|
|
22
|
+
T extends 'b' ? boolean :
|
|
23
|
+
T extends 's' | 'o' | 'g' ? string :
|
|
24
|
+
T extends 'n' | 'q' | 't' | 'd' | 'u' | 'i' | 'x' | 'y' ? number :
|
|
25
|
+
T extends 'h' | '?' ? unknown :
|
|
26
|
+
T extends 'v' ? Variant :
|
|
27
|
+
never;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Creates index type for dictionaries based on key type
|
|
31
|
+
*/
|
|
32
|
+
type CreateIndexType<Key extends string, Value extends any> =
|
|
33
|
+
Key extends 's' | 'o' | 'g' ? { [key: string]: Value } :
|
|
34
|
+
Key extends 'n' | 'q' | 't' | 'd' | 'u' | 'i' | 'x' | 'y' ? { [key: number]: Value } :
|
|
35
|
+
never;
|
|
36
|
+
|
|
37
|
+
// === Deep unpacking types (deepUnpack method) ===
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Parses dictionary content for deep unpacking
|
|
41
|
+
* For a{sv}, deepUnpack() preserves Variant values (GJS test behavior)
|
|
42
|
+
*/
|
|
43
|
+
type $ParseDeepVariantDict<State extends string> =
|
|
44
|
+
string extends State
|
|
45
|
+
? VariantTypeError<"$ParseDeepVariantDict: 'string' is not a supported type.">
|
|
46
|
+
: State extends `${infer Key}${infer ValueType}}${infer Remaining}`
|
|
47
|
+
? Key extends 's' | 'o' | 'g' | 'n' | 'q' | 't' | 'd' | 'u' | 'i' | 'x' | 'y'
|
|
48
|
+
? ValueType extends 'v'
|
|
49
|
+
? [CreateIndexType<Key, Variant>, Remaining] // a{sv} preserves Variant
|
|
50
|
+
: $ParseDeepVariantValue<ValueType> extends [infer V, '']
|
|
51
|
+
? [CreateIndexType<Key, V>, Remaining]
|
|
52
|
+
: VariantTypeError<`Invalid dictionary value type: ${ValueType}`>
|
|
53
|
+
: VariantTypeError<`Invalid dictionary key type: ${Key}`>
|
|
54
|
+
: VariantTypeError<`Invalid dictionary format: ${State}`>;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Parses tuple/struct content for deep unpacking
|
|
58
|
+
*/
|
|
59
|
+
type $ParseDeepVariantTuple<State extends string, Memo extends any[] = []> =
|
|
60
|
+
string extends State
|
|
61
|
+
? VariantTypeError<"$ParseDeepVariantTuple: 'string' is not a supported type.">
|
|
62
|
+
: State extends `)${infer Remaining}`
|
|
63
|
+
? [Memo, Remaining]
|
|
64
|
+
: $ParseDeepVariantValue<State> extends [infer Value, infer NextState]
|
|
65
|
+
? NextState extends string
|
|
66
|
+
? $ParseDeepVariantTuple<NextState, [...Memo, Value]>
|
|
67
|
+
: VariantTypeError<`$ParseDeepVariantTuple: NextState is not string`>
|
|
68
|
+
: VariantTypeError<`$ParseDeepVariantTuple: Invalid state: ${State}`>;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Parses key-value pair for deep unpacking
|
|
72
|
+
*/
|
|
73
|
+
type $ParseDeepVariantKeyValue<State extends string> =
|
|
74
|
+
string extends State
|
|
75
|
+
? VariantTypeError<"$ParseDeepVariantKeyValue: 'string' is not a supported type.">
|
|
76
|
+
: State extends `${infer Key}${infer ValueType}}${infer Remaining}`
|
|
77
|
+
? Key extends 's' | 'o' | 'g' | 'n' | 'q' | 't' | 'd' | 'u' | 'i' | 'x' | 'y'
|
|
78
|
+
? ValueType extends 'v'
|
|
79
|
+
? [[BasicTypeMap<Key>, Variant], Remaining] // Value remains Variant for 'v'
|
|
80
|
+
: $ParseDeepVariantValue<ValueType> extends [infer V, '']
|
|
81
|
+
? [[BasicTypeMap<Key>, V], Remaining]
|
|
82
|
+
: VariantTypeError<`Invalid key-value value type: ${ValueType}`>
|
|
83
|
+
: VariantTypeError<`Invalid key-value key type: ${Key}`>
|
|
84
|
+
: VariantTypeError<`Invalid key-value format: ${State}`>;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Main deep variant value parser
|
|
88
|
+
*/
|
|
89
|
+
type $ParseDeepVariantValue<State extends string> =
|
|
90
|
+
string extends State
|
|
91
|
+
? unknown
|
|
92
|
+
// Basic types
|
|
93
|
+
: State extends `${infer Type}${infer Remaining}`
|
|
94
|
+
? Type extends 's' | 'o' | 'g' | 'b' | 'n' | 'q' | 't' | 'd' | 'u' | 'i' | 'x' | 'y' | 'h' | '?'
|
|
95
|
+
? [BasicTypeMap<Type>, Remaining]
|
|
96
|
+
: Type extends 'v'
|
|
97
|
+
? [Variant, Remaining]
|
|
98
|
+
// Container types
|
|
99
|
+
: Type extends '('
|
|
100
|
+
? $ParseDeepVariantTuple<Remaining>
|
|
101
|
+
: Type extends 'a'
|
|
102
|
+
? Remaining extends `y${infer Rest}`
|
|
103
|
+
? [Uint8Array, Rest]
|
|
104
|
+
: Remaining extends `{${infer DictContent}`
|
|
105
|
+
? $ParseDeepVariantDict<DictContent>
|
|
106
|
+
: $ParseDeepVariantValue<Remaining> extends [infer ElementType, infer Rest]
|
|
107
|
+
? Rest extends string
|
|
108
|
+
? [ElementType[], Rest]
|
|
109
|
+
: VariantTypeError<`Array parsing failed`>
|
|
110
|
+
: VariantTypeError<`Array element parsing failed`>
|
|
111
|
+
: Type extends '{'
|
|
112
|
+
? $ParseDeepVariantKeyValue<Remaining>
|
|
113
|
+
: Type extends 'm'
|
|
114
|
+
? $ParseDeepVariantValue<Remaining> extends [infer Value, infer Rest]
|
|
115
|
+
? Rest extends string
|
|
116
|
+
? [Value | null, Rest]
|
|
117
|
+
: VariantTypeError<`Maybe parsing failed`>
|
|
118
|
+
: VariantTypeError<`Maybe content parsing failed`>
|
|
119
|
+
: VariantTypeError<`Unknown type: ${Type}`>
|
|
120
|
+
: VariantTypeError<`Invalid variant string: ${State}`>;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Main entry point for deep variant parsing
|
|
124
|
+
*/
|
|
125
|
+
type $ParseDeepVariant<T extends string> =
|
|
126
|
+
$ParseDeepVariantValue<T> extends infer Result
|
|
127
|
+
? Result extends [infer Value, string]
|
|
128
|
+
? Value
|
|
129
|
+
: Result extends VariantTypeError<any>
|
|
130
|
+
? Result
|
|
131
|
+
: unknown
|
|
132
|
+
: unknown;
|
|
133
|
+
|
|
134
|
+
// === Shallow unpacking types (unpack method) ===
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Main shallow variant value parser - only unpacks the top level
|
|
138
|
+
*/
|
|
139
|
+
type $ParseShallowVariantValue<State extends string> =
|
|
140
|
+
string extends State
|
|
141
|
+
? unknown
|
|
142
|
+
// Basic types
|
|
143
|
+
: State extends `${infer Type}${infer Remaining}`
|
|
144
|
+
? Type extends 's' | 'o' | 'g' | 'b' | 'n' | 'q' | 't' | 'd' | 'u' | 'i' | 'x' | 'y' | 'h' | '?'
|
|
145
|
+
? [BasicTypeMap<Type>, Remaining]
|
|
146
|
+
: Type extends 'v'
|
|
147
|
+
? [Variant, Remaining]
|
|
148
|
+
// Container types - return Variant arrays/objects
|
|
149
|
+
: Type extends '('
|
|
150
|
+
? $ParseShallowVariantTuple<Remaining>
|
|
151
|
+
: Type extends 'a'
|
|
152
|
+
? Remaining extends `y${infer Rest}`
|
|
153
|
+
? [Uint8Array, Rest]
|
|
154
|
+
: Remaining extends `{${infer DictContent}`
|
|
155
|
+
? $ParseShallowVariantDict<DictContent>
|
|
156
|
+
: [Variant[], Remaining] // Arrays contain Variant objects
|
|
157
|
+
: Type extends '{'
|
|
158
|
+
? $ParseShallowVariantKeyValue<Remaining>
|
|
159
|
+
: Type extends 'm'
|
|
160
|
+
? $ParseShallowVariantValue<Remaining> extends [infer Value, infer Rest]
|
|
161
|
+
? Rest extends string
|
|
162
|
+
? [Value | null, Rest]
|
|
163
|
+
: VariantTypeError<`Maybe parsing failed`>
|
|
164
|
+
: VariantTypeError<`Maybe content parsing failed`>
|
|
165
|
+
: VariantTypeError<`Unknown type: ${Type}`>
|
|
166
|
+
: VariantTypeError<`Invalid variant string: ${State}`>;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Parses tuple for shallow unpacking - returns array of Variants
|
|
170
|
+
*/
|
|
171
|
+
type $ParseShallowVariantTuple<State extends string, Memo extends any[] = []> =
|
|
172
|
+
string extends State
|
|
173
|
+
? VariantTypeError<"$ParseShallowVariantTuple: 'string' is not a supported type.">
|
|
174
|
+
: State extends `)${infer Remaining}`
|
|
175
|
+
? [Memo, Remaining]
|
|
176
|
+
: $SkipToNextElement<State> extends [infer NextState]
|
|
177
|
+
? NextState extends string
|
|
178
|
+
? $ParseShallowVariantTuple<NextState, [...Memo, Variant]>
|
|
179
|
+
: VariantTypeError<`$ParseShallowVariantTuple: Invalid state`>
|
|
180
|
+
: VariantTypeError<`$ParseShallowVariantTuple: Failed to skip element`>;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Skips a single variant element to find the next element boundary
|
|
184
|
+
*/
|
|
185
|
+
type $SkipToNextElement<State extends string, Depth extends number = 0> =
|
|
186
|
+
string extends State
|
|
187
|
+
? VariantTypeError<"Invalid state">
|
|
188
|
+
// Basic types - single character
|
|
189
|
+
: State extends `${infer Type}${infer Rest}`
|
|
190
|
+
? Type extends 's' | 'o' | 'g' | 'b' | 'n' | 'q' | 't' | 'd' | 'u' | 'i' | 'x' | 'y' | 'h' | '?' | 'v'
|
|
191
|
+
? [Rest]
|
|
192
|
+
: Type extends 'a'
|
|
193
|
+
? Rest extends `y${infer R}`
|
|
194
|
+
? [R]
|
|
195
|
+
: Rest extends `{${infer Inner}`
|
|
196
|
+
? $SkipUntil<Inner, '}'> extends [infer R]
|
|
197
|
+
? [R]
|
|
198
|
+
: VariantTypeError<`Failed to skip dictionary`>
|
|
199
|
+
: $SkipToNextElement<Rest> extends [infer R]
|
|
200
|
+
? [R]
|
|
201
|
+
: VariantTypeError<`Failed to skip array element`>
|
|
202
|
+
: Type extends 'm'
|
|
203
|
+
? $SkipToNextElement<Rest>
|
|
204
|
+
: Type extends '('
|
|
205
|
+
? $SkipUntil<Rest, ')'> extends [infer R]
|
|
206
|
+
? [R]
|
|
207
|
+
: VariantTypeError<`Failed to skip tuple`>
|
|
208
|
+
: Type extends '{'
|
|
209
|
+
? $SkipUntil<Rest, '}'> extends [infer R]
|
|
210
|
+
? [R]
|
|
211
|
+
: VariantTypeError<`Failed to skip key-value`>
|
|
212
|
+
: VariantTypeError<`Unknown type: ${Type}`>
|
|
213
|
+
: VariantTypeError<`Invalid format`>;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Generic utility to skip until a closing delimiter
|
|
217
|
+
*/
|
|
218
|
+
type $SkipUntil<State extends string, Delimiter extends string, Depth extends number = 1> =
|
|
219
|
+
string extends State
|
|
220
|
+
? never
|
|
221
|
+
: Depth extends 0
|
|
222
|
+
? [State]
|
|
223
|
+
: State extends `${infer Char}${infer Rest}`
|
|
224
|
+
? Char extends Delimiter
|
|
225
|
+
? Depth extends 1
|
|
226
|
+
? [Rest]
|
|
227
|
+
: $SkipUntil<Rest, Delimiter, Depth extends 2 ? 1 : Depth extends 3 ? 2 : Depth extends 4 ? 3 : 1>
|
|
228
|
+
: Char extends '(' | '{' // Opening delimiters increase depth
|
|
229
|
+
? $SkipUntil<Rest, Delimiter, Depth extends 1 ? 2 : Depth extends 2 ? 3 : Depth extends 3 ? 4 : 4>
|
|
230
|
+
: $SkipUntil<Rest, Delimiter, Depth>
|
|
231
|
+
: never;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Parses dictionary for shallow unpacking - values remain as Variants
|
|
235
|
+
*/
|
|
236
|
+
type $ParseShallowVariantDict<State extends string> =
|
|
237
|
+
string extends State
|
|
238
|
+
? VariantTypeError<"$ParseShallowVariantDict: 'string' is not a supported type.">
|
|
239
|
+
: State extends `${string}}${infer Remaining}`
|
|
240
|
+
? [{ [key: string]: Variant }, Remaining]
|
|
241
|
+
: VariantTypeError<`Invalid dictionary format`>;
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Parses key-value for shallow unpacking
|
|
245
|
+
*/
|
|
246
|
+
type $ParseShallowVariantKeyValue<State extends string> =
|
|
247
|
+
string extends State
|
|
248
|
+
? VariantTypeError<"$ParseShallowVariantKeyValue: 'string' is not a supported type.">
|
|
249
|
+
: State extends `${string}}${infer Remaining}`
|
|
250
|
+
? [[any, Variant], Remaining]
|
|
251
|
+
: VariantTypeError<`Invalid key-value format`>;
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Main entry point for shallow variant parsing
|
|
255
|
+
*/
|
|
256
|
+
type $ParseShallowVariant<T extends string> =
|
|
257
|
+
$ParseShallowVariantValue<T> extends infer Result
|
|
258
|
+
? Result extends [infer Value, string]
|
|
259
|
+
? Value
|
|
260
|
+
: Result extends VariantTypeError<any>
|
|
261
|
+
? Result
|
|
262
|
+
: unknown
|
|
263
|
+
: unknown;
|
|
264
|
+
|
|
265
|
+
// === Recursive unpacking types (recursiveUnpack method) ===
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Main recursive variant value parser - unpacks all Variants to native values
|
|
269
|
+
*/
|
|
270
|
+
type $ParseRecursiveVariantValue<State extends string> =
|
|
271
|
+
string extends State
|
|
272
|
+
? unknown
|
|
273
|
+
// Basic types
|
|
274
|
+
: State extends `${infer Type}${infer Remaining}`
|
|
275
|
+
? Type extends 's' | 'o' | 'g' | 'b' | 'n' | 'q' | 't' | 'd' | 'u' | 'i' | 'x' | 'y' | 'h' | '?'
|
|
276
|
+
? [BasicTypeMap<Type>, Remaining]
|
|
277
|
+
: Type extends 'v'
|
|
278
|
+
? [any, Remaining] // Variants are fully unpacked to any
|
|
279
|
+
// Container types
|
|
280
|
+
: Type extends '('
|
|
281
|
+
? $ParseRecursiveVariantTuple<Remaining>
|
|
282
|
+
: Type extends 'a'
|
|
283
|
+
? Remaining extends `y${infer Rest}`
|
|
284
|
+
? [Uint8Array, Rest]
|
|
285
|
+
: Remaining extends `{${infer DictContent}`
|
|
286
|
+
? $ParseRecursiveVariantDict<DictContent>
|
|
287
|
+
: $ParseRecursiveVariantValue<Remaining> extends [infer ElementType, infer Rest]
|
|
288
|
+
? Rest extends string
|
|
289
|
+
? [ElementType[], Rest]
|
|
290
|
+
: VariantTypeError<`Array parsing failed`>
|
|
291
|
+
: VariantTypeError<`Array element parsing failed`>
|
|
292
|
+
: Type extends '{'
|
|
293
|
+
? $ParseRecursiveVariantKeyValue<Remaining>
|
|
294
|
+
: Type extends 'm'
|
|
295
|
+
? $ParseRecursiveVariantValue<Remaining> extends [infer Value, infer Rest]
|
|
296
|
+
? Rest extends string
|
|
297
|
+
? [Value | null, Rest]
|
|
298
|
+
: VariantTypeError<`Maybe parsing failed`>
|
|
299
|
+
: VariantTypeError<`Maybe content parsing failed`>
|
|
300
|
+
: VariantTypeError<`Unknown type: ${Type}`>
|
|
301
|
+
: VariantTypeError<`Invalid variant string: ${State}`>;
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Parses tuple for recursive unpacking - fully unpacks all elements
|
|
305
|
+
*/
|
|
306
|
+
type $ParseRecursiveVariantTuple<State extends string, Memo extends any[] = []> =
|
|
307
|
+
string extends State
|
|
308
|
+
? VariantTypeError<"$ParseRecursiveVariantTuple: 'string' is not a supported type.">
|
|
309
|
+
: State extends `)${infer Remaining}`
|
|
310
|
+
? [Memo, Remaining]
|
|
311
|
+
: $ParseRecursiveVariantValue<State> extends [infer Value, infer NextState]
|
|
312
|
+
? NextState extends string
|
|
313
|
+
? $ParseRecursiveVariantTuple<NextState, [...Memo, Value]>
|
|
314
|
+
: VariantTypeError<`$ParseRecursiveVariantTuple: Invalid state`>
|
|
315
|
+
: VariantTypeError<`$ParseRecursiveVariantTuple: Parsing failed`>;
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Parses dictionary for recursive unpacking - fully unpacks all values
|
|
319
|
+
*/
|
|
320
|
+
type $ParseRecursiveVariantDict<State extends string> =
|
|
321
|
+
string extends State
|
|
322
|
+
? VariantTypeError<"$ParseRecursiveVariantDict: 'string' is not a supported type.">
|
|
323
|
+
: State extends `${infer Key}${infer ValueType}}${infer Remaining}`
|
|
324
|
+
? Key extends 's' | 'o' | 'g' | 'n' | 'q' | 't' | 'd' | 'u' | 'i' | 'x' | 'y'
|
|
325
|
+
? ValueType extends 'v'
|
|
326
|
+
? [CreateIndexType<Key, any>, Remaining] // a{sv} becomes any when recursively unpacked
|
|
327
|
+
: $ParseRecursiveVariantValue<ValueType> extends [infer V, '']
|
|
328
|
+
? [CreateIndexType<Key, V>, Remaining]
|
|
329
|
+
: VariantTypeError<`Invalid dictionary value type: ${ValueType}`>
|
|
330
|
+
: VariantTypeError<`Invalid dictionary key type: ${Key}`>
|
|
331
|
+
: VariantTypeError<`Invalid dictionary format: ${State}`>;
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Parses key-value for recursive unpacking
|
|
335
|
+
*/
|
|
336
|
+
type $ParseRecursiveVariantKeyValue<State extends string> =
|
|
337
|
+
string extends State
|
|
338
|
+
? VariantTypeError<"$ParseRecursiveVariantKeyValue: 'string' is not a supported type.">
|
|
339
|
+
: State extends `${infer Key}${infer ValueType}}${infer Remaining}`
|
|
340
|
+
? Key extends 's' | 'o' | 'g' | 'n' | 'q' | 't' | 'd' | 'u' | 'i' | 'x' | 'y'
|
|
341
|
+
? ValueType extends 'v'
|
|
342
|
+
? [[BasicTypeMap<Key>, any], Remaining] // Value is fully unpacked to any
|
|
343
|
+
: $ParseRecursiveVariantValue<ValueType> extends [infer V, '']
|
|
344
|
+
? [[BasicTypeMap<Key>, V], Remaining]
|
|
345
|
+
: VariantTypeError<`Invalid key-value value type: ${ValueType}`>
|
|
346
|
+
: VariantTypeError<`Invalid key-value key type: ${Key}`>
|
|
347
|
+
: VariantTypeError<`Invalid key-value format: ${State}`>;
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Main entry point for recursive variant parsing
|
|
351
|
+
*/
|
|
352
|
+
type $ParseRecursiveVariant<T extends string> =
|
|
353
|
+
$ParseRecursiveVariantValue<T> extends infer Result
|
|
354
|
+
? Result extends [infer Value, string]
|
|
355
|
+
? Value
|
|
356
|
+
: Result extends VariantTypeError<any>
|
|
357
|
+
? Result
|
|
358
|
+
: unknown
|
|
359
|
+
: unknown;
|
|
360
|
+
|
|
361
|
+
// === Constructor input types ===
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Parser for constructor input values
|
|
365
|
+
*/
|
|
366
|
+
type $ParseConstructorInputValue<State extends string> =
|
|
367
|
+
string extends State
|
|
368
|
+
? unknown
|
|
369
|
+
// Basic types
|
|
370
|
+
: State extends `${infer Type}${infer Remaining}`
|
|
371
|
+
? Type extends 's' | 'o' | 'g' | 'b' | 'n' | 'q' | 't' | 'd' | 'u' | 'i' | 'x' | 'y' | 'h' | '?'
|
|
372
|
+
? [BasicTypeMap<Type>, Remaining]
|
|
373
|
+
: Type extends 'v'
|
|
374
|
+
? [Variant, Remaining]
|
|
375
|
+
// Container types
|
|
376
|
+
: Type extends '('
|
|
377
|
+
? $ParseConstructorInputTuple<Remaining>
|
|
378
|
+
: Type extends 'a'
|
|
379
|
+
? Remaining extends `y${infer Rest}`
|
|
380
|
+
? [Uint8Array | string, Rest] // ay accepts both Uint8Array and string
|
|
381
|
+
: Remaining extends `{${infer DictContent}`
|
|
382
|
+
? $ParseConstructorInputDict<DictContent>
|
|
383
|
+
: $ParseConstructorInputValue<Remaining> extends [infer ElementType, infer Rest]
|
|
384
|
+
? Rest extends string
|
|
385
|
+
? [ElementType[], Rest]
|
|
386
|
+
: VariantTypeError<`Array parsing failed`>
|
|
387
|
+
: VariantTypeError<`Array element parsing failed`>
|
|
388
|
+
: Type extends '{'
|
|
389
|
+
? $ParseConstructorInputKeyValue<Remaining>
|
|
390
|
+
: Type extends 'm'
|
|
391
|
+
? $ParseConstructorInputValue<Remaining> extends [infer Value, infer Rest]
|
|
392
|
+
? Rest extends string
|
|
393
|
+
? [Value | null, Rest]
|
|
394
|
+
: VariantTypeError<`Maybe parsing failed`>
|
|
395
|
+
: VariantTypeError<`Maybe content parsing failed`>
|
|
396
|
+
: VariantTypeError<`Unknown type: ${Type}`>
|
|
397
|
+
: VariantTypeError<`Invalid variant string: ${State}`>;
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Parses tuple for constructor input
|
|
401
|
+
*/
|
|
402
|
+
type $ParseConstructorInputTuple<State extends string, Memo extends any[] = []> =
|
|
403
|
+
string extends State
|
|
404
|
+
? VariantTypeError<"Invalid tuple state">
|
|
405
|
+
: State extends `)${infer Remaining}`
|
|
406
|
+
? [Memo, Remaining]
|
|
407
|
+
: $ParseConstructorInputValue<State> extends [infer Value, infer NextState]
|
|
408
|
+
? NextState extends string
|
|
409
|
+
? $ParseConstructorInputTuple<NextState, [...Memo, Value]>
|
|
410
|
+
: VariantTypeError<`Invalid tuple parsing`>
|
|
411
|
+
: VariantTypeError<`Tuple element parsing failed`>;
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Parses dictionary for constructor input
|
|
415
|
+
*/
|
|
416
|
+
type $ParseConstructorInputDict<State extends string> =
|
|
417
|
+
string extends State
|
|
418
|
+
? VariantTypeError<"Invalid dictionary state">
|
|
419
|
+
: State extends `${infer Key}${infer ValueType}}${infer Remaining}`
|
|
420
|
+
? Key extends 's' | 'o' | 'g' | 'n' | 'q' | 't' | 'd' | 'u' | 'i' | 'x' | 'y'
|
|
421
|
+
? $ParseConstructorInputValue<ValueType> extends [infer V, '']
|
|
422
|
+
? [CreateIndexType<Key, V>, Remaining]
|
|
423
|
+
: VariantTypeError<`Invalid dictionary value type`>
|
|
424
|
+
: VariantTypeError<`Invalid dictionary key type`>
|
|
425
|
+
: VariantTypeError<`Invalid dictionary format`>;
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Parses key-value for constructor input
|
|
429
|
+
*/
|
|
430
|
+
type $ParseConstructorInputKeyValue<State extends string> =
|
|
431
|
+
string extends State
|
|
432
|
+
? VariantTypeError<"Invalid key-value state">
|
|
433
|
+
: State extends `${infer Key}${infer ValueType}}${infer Remaining}`
|
|
434
|
+
? Key extends 's' | 'o' | 'g' | 'n' | 'q' | 't' | 'd' | 'u' | 'i' | 'x' | 'y'
|
|
435
|
+
? $ParseConstructorInputValue<ValueType> extends [infer V, '']
|
|
436
|
+
? [[BasicTypeMap<Key>, V], Remaining]
|
|
437
|
+
: VariantTypeError<`Invalid key-value value type`>
|
|
438
|
+
: VariantTypeError<`Invalid key-value key type`>
|
|
439
|
+
: VariantTypeError<`Invalid key-value format`>;
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Main entry point for constructor input parsing
|
|
443
|
+
*/
|
|
444
|
+
type $ParseConstructorInput<T extends string> =
|
|
445
|
+
$ParseConstructorInputValue<T> extends infer Result
|
|
446
|
+
? Result extends [infer Value, string]
|
|
447
|
+
? Value
|
|
448
|
+
: Result extends VariantTypeError<any>
|
|
449
|
+
? Result
|
|
450
|
+
: any
|
|
451
|
+
: any;
|
|
452
|
+
|
|
453
|
+
// === Type aliases for unpacking methods ===
|
|
454
|
+
|
|
455
|
+
type $ParseVariant<T extends string> = $ParseShallowVariant<T>;
|
|
456
|
+
|
|
457
|
+
// === Utility types for Variant and VariantBuilder ===
|
|
458
|
+
|
|
459
|
+
type $VariantTypeToString<T extends VariantType> = T extends VariantType<infer S> ? S : never;
|
|
460
|
+
|
|
461
|
+
type $ToTuple<T extends readonly VariantType[]> =
|
|
462
|
+
T extends [] ? '' :
|
|
463
|
+
T extends [VariantType<infer S>] ? `${S}` :
|
|
464
|
+
T extends [VariantType<infer S>, ...infer U] ? (
|
|
465
|
+
U extends [...VariantType[]] ? `${S}${$ToTuple<U>}` : never) :
|
|
466
|
+
'?';
|
|
467
|
+
|
|
468
|
+
type $ElementSig<E extends any> =
|
|
469
|
+
E extends [infer Element] ? Element :
|
|
470
|
+
E extends [infer Element, ...infer Elements] ? Element | $ElementSig<Elements> :
|
|
471
|
+
E extends globalThis.Array<infer Element> ? Element : never;
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* GLib.Variant is a value container whose types are determined at construction.
|
|
475
|
+
*
|
|
476
|
+
* It serves as a reliable and efficient format for storing structured data that can be
|
|
477
|
+
* serialized while preserving type information. Comparable to JSON, but with strong typing
|
|
478
|
+
* and support for special values like file handles.
|
|
479
|
+
*
|
|
480
|
+
* GVariant is used throughout the GNOME Platform including GDBus, GSettings, GAction,
|
|
481
|
+
* GMenu and many other APIs. All D-Bus method, property and signal values are GVariant objects.
|
|
482
|
+
*
|
|
483
|
+
* @example
|
|
484
|
+
* ```typescript
|
|
485
|
+
* // Create variants using constructor with type signature
|
|
486
|
+
* const stringVariant = new GLib.Variant('s', 'Hello World');
|
|
487
|
+
* const numberVariant = new GLib.Variant('i', 42);
|
|
488
|
+
* const boolVariant = new GLib.Variant('b', true);
|
|
489
|
+
*
|
|
490
|
+
* // Create complex variants like dictionaries
|
|
491
|
+
* const dictVariant = new GLib.Variant('a{sv}', {
|
|
492
|
+
* 'name': GLib.Variant.new_string('Mario'),
|
|
493
|
+
* 'lives': GLib.Variant.new_uint32(3),
|
|
494
|
+
* 'active': GLib.Variant.new_boolean(true)
|
|
495
|
+
* });
|
|
496
|
+
*
|
|
497
|
+
* // Unpack variants to JavaScript values
|
|
498
|
+
* const stringValue = stringVariant.unpack(); // → "Hello World"
|
|
499
|
+
* const dictValue = dictVariant.deepUnpack(); // → { name: Variant<"s">, lives: Variant<"u">, active: Variant<"b"> }
|
|
500
|
+
* ```
|
|
501
|
+
*
|
|
502
|
+
* @see {@link https://gjs.guide/guides/glib/gvariant.html|GJS Guide: GVariant}
|
|
503
|
+
* @see {@link https://docs.gtk.org/glib/struct.Variant.html|GLib Documentation: GVariant}
|
|
504
|
+
*/
|
|
505
|
+
export class Variant<S extends string = any> {
|
|
506
|
+
static $gtype: GObject.GType<Variant>;
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* Creates a new GVariant with the specified type signature and value.
|
|
510
|
+
*
|
|
511
|
+
* @param sig The GVariant type signature (e.g., 's' for string, 'i' for int32, 'a{sv}' for dictionary)
|
|
512
|
+
* @param value The JavaScript value to pack into the variant
|
|
513
|
+
* @example
|
|
514
|
+
* ```typescript
|
|
515
|
+
* const variant = new GLib.Variant('s', 'Hello');
|
|
516
|
+
* const arrayVariant = new GLib.Variant('as', ['one', 'two', 'three']);
|
|
517
|
+
* ```
|
|
518
|
+
*/
|
|
519
|
+
constructor(sig: S, value: $ParseConstructorInput<S>);
|
|
520
|
+
constructor(copy: Variant<S>);
|
|
521
|
+
_init(sig: S, value: any): Variant<S>;
|
|
522
|
+
|
|
523
|
+
// Constructors
|
|
524
|
+
/**
|
|
525
|
+
* Creates a new GVariant with the specified type signature and value.
|
|
526
|
+
*
|
|
527
|
+
* This is equivalent to using the constructor directly.
|
|
528
|
+
*
|
|
529
|
+
* @param sig The GVariant type signature
|
|
530
|
+
* @param value The JavaScript value to pack
|
|
531
|
+
* @returns A new GVariant instance
|
|
532
|
+
*/
|
|
533
|
+
static ["new"]<S extends string>(sig: S, value: $ParseConstructorInput<S>): Variant<S>;
|
|
534
|
+
static _new_internal<S extends string>(sig: S, value: $ParseConstructorInput<S>): Variant<S>;
|
|
535
|
+
static new_array<C extends string = "a?">(
|
|
536
|
+
child_type: VariantType<C> | null,
|
|
537
|
+
children: typeof child_type extends VariantType<any>
|
|
538
|
+
? Variant<$VariantTypeToString<typeof child_type>>[]
|
|
539
|
+
: Variant<C>[]
|
|
540
|
+
): Variant<`a${C}`>;
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Creates a new boolean GVariant instance.
|
|
544
|
+
*
|
|
545
|
+
* @param value The boolean value to pack
|
|
546
|
+
* @returns A new GVariant with type signature 'b'
|
|
547
|
+
* @example
|
|
548
|
+
* ```typescript
|
|
549
|
+
* const variant = GLib.Variant.new_boolean(true);
|
|
550
|
+
* const unpacked = variant.get_boolean(); // → true
|
|
551
|
+
* ```
|
|
552
|
+
*/
|
|
553
|
+
static new_boolean(value: boolean): Variant<'b'>;
|
|
554
|
+
|
|
555
|
+
static new_byte(value: number): Variant<'y'>;
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Creates a new bytestring GVariant instance from a Uint8Array or string.
|
|
559
|
+
*
|
|
560
|
+
* @param string The string or byte array to pack
|
|
561
|
+
* @returns A new GVariant with type signature 'ay'
|
|
562
|
+
*/
|
|
563
|
+
static new_bytestring(string: Uint8Array | string): Variant<'ay'>;
|
|
564
|
+
|
|
565
|
+
static new_bytestring_array(strv: string[]): Variant<'aay'>;
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* Creates a new dictionary entry GVariant.
|
|
569
|
+
*
|
|
570
|
+
* @param key The key variant
|
|
571
|
+
* @param value The value variant
|
|
572
|
+
* @returns A new GVariant representing a key-value pair
|
|
573
|
+
*/
|
|
574
|
+
static new_dict_entry(key: Variant, value: Variant): Variant<'{vv}'>;
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Creates a new double-precision floating point GVariant.
|
|
578
|
+
*
|
|
579
|
+
* @param value The number value to pack
|
|
580
|
+
* @returns A new GVariant with type signature 'd'
|
|
581
|
+
*/
|
|
582
|
+
static new_double(value: number): Variant<'d'>;
|
|
583
|
+
|
|
584
|
+
static new_fixed_array<C extends string = 'a?'>(element_type: VariantType<C>, elements: Variant<$VariantTypeToString<typeof element_type>>[] | null, n_elements: number, element_size: number): Variant<`a${C}`>;
|
|
585
|
+
static new_from_bytes<C extends string>(type: VariantType<C>, bytes: Bytes | Uint8Array, trusted: boolean): Variant<C>;
|
|
586
|
+
static new_from_data<C extends string>(type: VariantType<C>, data: Uint8Array | string, trusted: boolean, user_data?: any | null): Variant<C>;
|
|
587
|
+
static new_handle(value: number): Variant<'h'>;
|
|
588
|
+
static new_int16(value: number): Variant<'n'>;
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Creates a new 32-bit signed integer GVariant.
|
|
592
|
+
*
|
|
593
|
+
* @param value The integer value to pack
|
|
594
|
+
* @returns A new GVariant with type signature 'i'
|
|
595
|
+
* @example
|
|
596
|
+
* ```typescript
|
|
597
|
+
* const variant = GLib.Variant.new_int32(-42);
|
|
598
|
+
* const unpacked = variant.get_int32(); // → -42
|
|
599
|
+
* ```
|
|
600
|
+
*/
|
|
601
|
+
static new_int32(value: number): Variant<'i'>;
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Creates a new 64-bit signed integer GVariant.
|
|
605
|
+
*
|
|
606
|
+
* Note: As of GJS v1.68, all numeric types are still Number values,
|
|
607
|
+
* so some 64-bit values may not be fully supported. BigInt support to come.
|
|
608
|
+
*
|
|
609
|
+
* @param value The integer value to pack
|
|
610
|
+
* @returns A new GVariant with type signature 'x'
|
|
611
|
+
*/
|
|
612
|
+
static new_int64(value: number): Variant<'x'>;
|
|
613
|
+
|
|
614
|
+
static new_maybe(child_type?: VariantType | null, child?: Variant | null): Variant<'mv'>;
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* Creates a new object path GVariant.
|
|
618
|
+
*
|
|
619
|
+
* @param object_path A valid D-Bus object path string
|
|
620
|
+
* @returns A new GVariant with type signature 'o'
|
|
621
|
+
*/
|
|
622
|
+
static new_object_path(object_path: string): Variant<'o'>;
|
|
623
|
+
|
|
624
|
+
static new_objv(strv: string[]): Variant<'ao'>;
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* Creates a new D-Bus signature GVariant.
|
|
628
|
+
*
|
|
629
|
+
* @param signature A valid D-Bus type signature string
|
|
630
|
+
* @returns A new GVariant with type signature 'g'
|
|
631
|
+
*/
|
|
632
|
+
static new_signature(signature: string): Variant<'g'>;
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* Creates a new string GVariant instance.
|
|
636
|
+
*
|
|
637
|
+
* @param string The string value to pack
|
|
638
|
+
* @returns A new GVariant with type signature 's'
|
|
639
|
+
* @example
|
|
640
|
+
* ```typescript
|
|
641
|
+
* const variant = GLib.Variant.new_string('Hello World');
|
|
642
|
+
* const [value, length] = variant.get_string(); // → ['Hello World', 11]
|
|
643
|
+
* const unpacked = variant.unpack(); // → 'Hello World'
|
|
644
|
+
* ```
|
|
645
|
+
*/
|
|
646
|
+
static new_string(string: string): Variant<'s'>;
|
|
647
|
+
|
|
648
|
+
/**
|
|
649
|
+
* Creates a new string array GVariant instance.
|
|
650
|
+
*
|
|
651
|
+
* @param strv Array of strings to pack
|
|
652
|
+
* @returns A new GVariant with type signature 'as'
|
|
653
|
+
* @example
|
|
654
|
+
* ```typescript
|
|
655
|
+
* const variant = GLib.Variant.new_strv(['one', 'two', 'three']);
|
|
656
|
+
* const unpacked = variant.get_strv(); // → ['one', 'two', 'three']
|
|
657
|
+
* const deepUnpacked = variant.deepUnpack(); // → ['one', 'two', 'three']
|
|
658
|
+
* ```
|
|
659
|
+
*/
|
|
660
|
+
static new_strv(strv: string[]): Variant<'as'>;
|
|
661
|
+
|
|
662
|
+
static new_tuple<Items extends (ReadonlyArray<VariantType> | readonly [VariantType])>(children: Items): Variant<`(${$ToTuple<Items>})`>;
|
|
663
|
+
static new_uint16(value: number): Variant<'q'>;
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
* Creates a new 32-bit unsigned integer GVariant.
|
|
667
|
+
*
|
|
668
|
+
* @param value The unsigned integer value to pack
|
|
669
|
+
* @returns A new GVariant with type signature 'u'
|
|
670
|
+
*/
|
|
671
|
+
static new_uint32(value: number): Variant<'u'>;
|
|
672
|
+
|
|
673
|
+
static new_uint64(value: number): Variant<'t'>;
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* Creates a new variant GVariant that contains another variant.
|
|
677
|
+
*
|
|
678
|
+
* @param value The variant to wrap
|
|
679
|
+
* @returns A new GVariant with type signature 'v'
|
|
680
|
+
*/
|
|
681
|
+
static new_variant(value: Variant): Variant<'v'>;
|
|
682
|
+
// Members
|
|
683
|
+
byteswap(): Variant;
|
|
684
|
+
check_format_string(format_string: string, copy_only: boolean): boolean;
|
|
685
|
+
classify(): VariantClass;
|
|
686
|
+
compare(two: Variant): number;
|
|
687
|
+
dup_bytestring(): Uint8Array;
|
|
688
|
+
dup_bytestring_array(): string[];
|
|
689
|
+
dup_objv(): string[];
|
|
690
|
+
dup_string(): [string, number];
|
|
691
|
+
dup_strv(): string[];
|
|
692
|
+
/**
|
|
693
|
+
* Checks if two variants are equal.
|
|
694
|
+
*
|
|
695
|
+
* @param two The variant to compare with
|
|
696
|
+
* @returns true if the variants are equal, false otherwise
|
|
697
|
+
* @example
|
|
698
|
+
* ```typescript
|
|
699
|
+
* const variant1 = GLib.Variant.new_string('test');
|
|
700
|
+
* const variant2 = GLib.Variant.new_string('test');
|
|
701
|
+
* const areEqual = variant1.equal(variant2); // → true
|
|
702
|
+
* ```
|
|
703
|
+
*/
|
|
704
|
+
equal(two: Variant): boolean;
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* Extracts a boolean value from a boolean variant.
|
|
708
|
+
*
|
|
709
|
+
* @returns The boolean value
|
|
710
|
+
* @throws Error if the variant is not of type 'b'
|
|
711
|
+
*/
|
|
712
|
+
get_boolean(): boolean;
|
|
713
|
+
|
|
714
|
+
get_byte(): number;
|
|
715
|
+
|
|
716
|
+
/**
|
|
717
|
+
* Extracts a bytestring from a bytestring variant.
|
|
718
|
+
*
|
|
719
|
+
* @returns The byte array
|
|
720
|
+
*/
|
|
721
|
+
get_bytestring(): Uint8Array;
|
|
722
|
+
|
|
723
|
+
get_bytestring_array(): string[];
|
|
724
|
+
|
|
725
|
+
/**
|
|
726
|
+
* Gets a child variant by index from a container variant.
|
|
727
|
+
*
|
|
728
|
+
* @param index_ The index of the child to retrieve
|
|
729
|
+
* @returns The child variant at the specified index
|
|
730
|
+
* @example
|
|
731
|
+
* ```typescript
|
|
732
|
+
* const tuple = new GLib.Variant('(si)', ['hello', 42]);
|
|
733
|
+
* const firstChild = tuple.get_child_value(0); // → Variant<'s'> containing 'hello'
|
|
734
|
+
* const secondChild = tuple.get_child_value(1); // → Variant<'i'> containing 42
|
|
735
|
+
* ```
|
|
736
|
+
*/
|
|
737
|
+
get_child_value(index_: number): Variant;
|
|
738
|
+
|
|
739
|
+
get_data(): any | null;
|
|
740
|
+
get_data_as_bytes(): Bytes;
|
|
741
|
+
get_double(): number;
|
|
742
|
+
get_handle(): number;
|
|
743
|
+
get_int16(): number;
|
|
744
|
+
|
|
745
|
+
/**
|
|
746
|
+
* Extracts a 32-bit signed integer from an integer variant.
|
|
747
|
+
*
|
|
748
|
+
* @returns The integer value
|
|
749
|
+
* @throws Error if the variant is not of type 'i'
|
|
750
|
+
*/
|
|
751
|
+
get_int32(): number;
|
|
752
|
+
|
|
753
|
+
get_int64(): number;
|
|
754
|
+
get_maybe(): Variant | null;
|
|
755
|
+
get_normal_form(): Variant;
|
|
756
|
+
get_objv(): string[];
|
|
757
|
+
get_size(): number;
|
|
758
|
+
|
|
759
|
+
/**
|
|
760
|
+
* Extracts a string value from a string variant.
|
|
761
|
+
*
|
|
762
|
+
* @returns A tuple containing the string value and its length
|
|
763
|
+
* @example
|
|
764
|
+
* ```typescript
|
|
765
|
+
* const variant = GLib.Variant.new_string('hello');
|
|
766
|
+
* const [value, length] = variant.get_string(); // → ['hello', 5]
|
|
767
|
+
* ```
|
|
768
|
+
*/
|
|
769
|
+
get_string(): [string, number | null];
|
|
770
|
+
|
|
771
|
+
/**
|
|
772
|
+
* Extracts a string array from a string array variant.
|
|
773
|
+
*
|
|
774
|
+
* @returns Array of strings
|
|
775
|
+
*/
|
|
776
|
+
get_strv(): string[];
|
|
777
|
+
|
|
778
|
+
/**
|
|
779
|
+
* Gets the type of the variant.
|
|
780
|
+
*
|
|
781
|
+
* @returns The VariantType representing this variant's type
|
|
782
|
+
*/
|
|
783
|
+
get_type(): VariantType<S>;
|
|
784
|
+
|
|
785
|
+
/**
|
|
786
|
+
* Gets the type signature string of the variant.
|
|
787
|
+
*
|
|
788
|
+
* This is very useful for debugging and type checking.
|
|
789
|
+
*
|
|
790
|
+
* @returns The type signature string (e.g., 's', 'i', 'a{sv}')
|
|
791
|
+
* @example
|
|
792
|
+
* ```typescript
|
|
793
|
+
* const stringVariant = GLib.Variant.new_string('test');
|
|
794
|
+
* const typeString = stringVariant.get_type_string(); // → 's'
|
|
795
|
+
*
|
|
796
|
+
* const dictVariant = new GLib.Variant('a{sv}', {});
|
|
797
|
+
* const dictType = dictVariant.get_type_string(); // → 'a{sv}'
|
|
798
|
+
* ```
|
|
799
|
+
*/
|
|
800
|
+
get_type_string(): string;
|
|
801
|
+
|
|
802
|
+
get_uint16(): number;
|
|
803
|
+
get_uint32(): number;
|
|
804
|
+
get_uint64(): number;
|
|
805
|
+
get_variant(): Variant;
|
|
806
|
+
hash(): number;
|
|
807
|
+
|
|
808
|
+
/**
|
|
809
|
+
* Checks if the variant is a container type.
|
|
810
|
+
*
|
|
811
|
+
* Container types include arrays, tuples, dictionaries, and maybes.
|
|
812
|
+
*
|
|
813
|
+
* @returns true if the variant is a container
|
|
814
|
+
*/
|
|
815
|
+
is_container(): boolean;
|
|
816
|
+
|
|
817
|
+
is_floating(): boolean;
|
|
818
|
+
is_normal_form(): boolean;
|
|
819
|
+
is_of_type(type: VariantType): boolean;
|
|
820
|
+
lookup_value(key: string, expected_type?: VariantType | null): Variant;
|
|
821
|
+
|
|
822
|
+
/**
|
|
823
|
+
* Gets the number of children in a container variant.
|
|
824
|
+
*
|
|
825
|
+
* @returns The number of child elements
|
|
826
|
+
* @example
|
|
827
|
+
* ```typescript
|
|
828
|
+
* const tuple = new GLib.Variant('(si)', ['hello', 42]);
|
|
829
|
+
* const childCount = tuple.n_children(); // → 2
|
|
830
|
+
*
|
|
831
|
+
* const array = GLib.Variant.new_strv(['a', 'b', 'c']);
|
|
832
|
+
* const arrayLength = array.n_children(); // → 3
|
|
833
|
+
* ```
|
|
834
|
+
*/
|
|
835
|
+
n_children(): number;
|
|
836
|
+
|
|
837
|
+
/**
|
|
838
|
+
* Creates a string representation of the variant.
|
|
839
|
+
*
|
|
840
|
+
* This is extremely useful for debugging GVariant structures.
|
|
841
|
+
*
|
|
842
|
+
* @param type_annotate Whether to include type annotations in the output
|
|
843
|
+
* @returns A string representation of the variant
|
|
844
|
+
* @example
|
|
845
|
+
* ```typescript
|
|
846
|
+
* const variant = new GLib.Variant('a{sv}', {
|
|
847
|
+
* 'name': GLib.Variant.new_string('Mario'),
|
|
848
|
+
* 'lives': GLib.Variant.new_uint32(3)
|
|
849
|
+
* });
|
|
850
|
+
*
|
|
851
|
+
* // Without type annotations
|
|
852
|
+
* print(variant.print(false)); // → "{'name': 'Mario', 'lives': 3}"
|
|
853
|
+
*
|
|
854
|
+
* // With type annotations
|
|
855
|
+
* print(variant.print(true)); // → "{'name': <'Mario'>, 'lives': <uint32 3>}"
|
|
856
|
+
* ```
|
|
857
|
+
*/
|
|
858
|
+
print(type_annotate: boolean): string;
|
|
859
|
+
ref(): Variant;
|
|
860
|
+
ref_sink(): Variant;
|
|
861
|
+
store(data: any): void;
|
|
862
|
+
take_ref(): Variant;
|
|
863
|
+
unref(): void;
|
|
864
|
+
static is_object_path(string: string): boolean;
|
|
865
|
+
static is_signature(string: string): boolean;
|
|
866
|
+
static parse(type: VariantType | null, text: string, limit?: string | null, endptr?: string | null): Variant;
|
|
867
|
+
static parse_error_print_context(error: Error, source_str: string): string;
|
|
868
|
+
static parse_error_quark(): Quark;
|
|
869
|
+
static parser_get_error_quark(): Quark;
|
|
870
|
+
/**
|
|
871
|
+
* Unpacks the variant's data into a JavaScript value.
|
|
872
|
+
*
|
|
873
|
+
* This performs a **shallow unpacking operation** - only unpacking the top level.
|
|
874
|
+
* For containers like arrays or dictionaries, child elements remain as Variant objects.
|
|
875
|
+
*
|
|
876
|
+
* @example
|
|
877
|
+
* ```typescript
|
|
878
|
+
* // Simple types are fully unpacked
|
|
879
|
+
* const boolVariant = GLib.Variant.new_boolean(true);
|
|
880
|
+
* const boolValue = boolVariant.unpack(); // → true
|
|
881
|
+
*
|
|
882
|
+
* // String values are unpacked (discarding length information)
|
|
883
|
+
* const stringVariant = GLib.Variant.new_string("hello");
|
|
884
|
+
* const stringValue = stringVariant.unpack(); // → "hello"
|
|
885
|
+
*
|
|
886
|
+
* // Arrays are unpacked but elements remain as Variants
|
|
887
|
+
* const arrayVariant = GLib.Variant.new_strv(["one", "two"]);
|
|
888
|
+
* const arrayValue = arrayVariant.unpack(); // → [Variant<"s">, Variant<"s">]
|
|
889
|
+
* ```
|
|
890
|
+
*
|
|
891
|
+
* @returns The unpacked JavaScript value with child Variants preserved
|
|
892
|
+
* @see {@link deepUnpack} for unpacking one level deeper
|
|
893
|
+
* @see {@link recursiveUnpack} for full recursive unpacking
|
|
894
|
+
*/
|
|
895
|
+
unpack(): $ParseShallowVariant<S>;
|
|
896
|
+
unpack<T>(): T;
|
|
897
|
+
unpack(): $ParseShallowVariant<S>; // Duplicate overload ensures optimal type inference for ReturnType<...>
|
|
898
|
+
|
|
899
|
+
/**
|
|
900
|
+
* Recursively unpacks the variant's data into JavaScript values.
|
|
901
|
+
*
|
|
902
|
+
* This method unpacks a variant **and its direct children**, but only up to one level deep.
|
|
903
|
+
* It's the most commonly used unpacking method for D-Bus operations and GSettings.
|
|
904
|
+
*
|
|
905
|
+
* With advanced variants enabled, this method provides automatic type inference
|
|
906
|
+
* based on the variant's type signature. You can also explicitly specify a type
|
|
907
|
+
* parameter for backward compatibility.
|
|
908
|
+
*
|
|
909
|
+
* @example
|
|
910
|
+
* ```typescript
|
|
911
|
+
* // Simple dictionary (a{ss}) - fully unpacked
|
|
912
|
+
* const simpleDict = new GLib.Variant('a{ss}', {
|
|
913
|
+
* 'key1': 'value1',
|
|
914
|
+
* 'key2': 'value2'
|
|
915
|
+
* });
|
|
916
|
+
* const simple = simpleDict.deepUnpack(); // → { key1: "value1", key2: "value2" }
|
|
917
|
+
*
|
|
918
|
+
* // Complex dictionary (a{sv}) - values remain as Variants
|
|
919
|
+
* const complexDict = new GLib.Variant('a{sv}', {
|
|
920
|
+
* 'name': GLib.Variant.new_string('Mario'),
|
|
921
|
+
* 'active': GLib.Variant.new_boolean(true)
|
|
922
|
+
* });
|
|
923
|
+
* const complex = complexDict.deepUnpack(); // → { name: Variant<"s">, active: Variant<"b"> }
|
|
924
|
+
*
|
|
925
|
+
* // Automatic type inference (Advanced Variants)
|
|
926
|
+
* const autoInferred = variant.deepUnpack(); // Types inferred from signature
|
|
927
|
+
*
|
|
928
|
+
* // Explicit type parameter (backward compatibility)
|
|
929
|
+
* const explicit = variant.deepUnpack<{ [key: string]: GLib.Variant }>();
|
|
930
|
+
*
|
|
931
|
+
* // String arrays are fully unpacked
|
|
932
|
+
* const strArray = GLib.Variant.new_strv(['one', 'two']);
|
|
933
|
+
* const strings = strArray.deepUnpack(); // → ["one", "two"]
|
|
934
|
+
* ```
|
|
935
|
+
*
|
|
936
|
+
* @template T The expected return type (defaults to automatically inferred type)
|
|
937
|
+
* @returns The deeply unpacked JavaScript value with one level of children unpacked
|
|
938
|
+
* @see {@link unpack} for shallow unpacking only
|
|
939
|
+
* @see {@link recursiveUnpack} for full recursive unpacking
|
|
940
|
+
*/
|
|
941
|
+
// Overloads: concrete first so call-sites infer precisely; generic allows explicit override; concrete repeated last so ReturnType<...> is precise
|
|
942
|
+
deepUnpack(): $ParseDeepVariant<S>;
|
|
943
|
+
deepUnpack<T>(): T;
|
|
944
|
+
deepUnpack(): $ParseDeepVariant<S>; // Duplicate overload ensures optimal type inference for ReturnType<...>
|
|
945
|
+
|
|
946
|
+
/**
|
|
947
|
+
* Alias for {@link deepUnpack} method.
|
|
948
|
+
*
|
|
949
|
+
* Recursively unpacks the variant's data into JavaScript values up to one level deep.
|
|
950
|
+
* This is the snake_case version of the same functionality.
|
|
951
|
+
*
|
|
952
|
+
* @returns The deeply unpacked JavaScript value
|
|
953
|
+
* @see {@link deepUnpack} for the camelCase version with full documentation
|
|
954
|
+
*/
|
|
955
|
+
deep_unpack(): $ParseDeepVariant<S>;
|
|
956
|
+
deep_unpack<T>(): T;
|
|
957
|
+
deep_unpack(): $ParseDeepVariant<S>; // Duplicate overload ensures optimal type inference for ReturnType<...>
|
|
958
|
+
|
|
959
|
+
/**
|
|
960
|
+
* Recursively unpacks the variant and **all its descendants** into native JavaScript values.
|
|
961
|
+
*
|
|
962
|
+
* **Available since GJS 1.64 (GNOME 3.36)**
|
|
963
|
+
*
|
|
964
|
+
* This method performs complete recursive unpacking, converting all nested Variants
|
|
965
|
+
* to their native JavaScript equivalents. **Type information may be lost** during
|
|
966
|
+
* this process, so you'll need to know the original types to repack values.
|
|
967
|
+
*
|
|
968
|
+
* @example
|
|
969
|
+
* ```typescript
|
|
970
|
+
* // Complex nested structure fully unpacked
|
|
971
|
+
* const complexDict = new GLib.Variant('a{sv}', {
|
|
972
|
+
* 'name': GLib.Variant.new_string('Mario'),
|
|
973
|
+
* 'lives': GLib.Variant.new_uint32(3),
|
|
974
|
+
* 'active': GLib.Variant.new_boolean(true)
|
|
975
|
+
* });
|
|
976
|
+
*
|
|
977
|
+
* const fullyUnpacked = complexDict.recursiveUnpack();
|
|
978
|
+
* // → { name: "Mario", lives: 3, active: true }
|
|
979
|
+
*
|
|
980
|
+
* // All nested Variants are converted to native values
|
|
981
|
+
* const nestedTuple = new GLib.Variant('(sa{sv})', [
|
|
982
|
+
* 'player',
|
|
983
|
+
* { 'score': GLib.Variant.new_int32(100) }
|
|
984
|
+
* ]);
|
|
985
|
+
* const result = nestedTuple.recursiveUnpack();
|
|
986
|
+
* // → ["player", { score: 100 }]
|
|
987
|
+
* ```
|
|
988
|
+
*
|
|
989
|
+
* @returns The recursively unpacked JavaScript value with all Variants converted to native types
|
|
990
|
+
* @see {@link deepUnpack} for one-level unpacking with type preservation
|
|
991
|
+
* @see {@link unpack} for shallow unpacking only
|
|
992
|
+
* @since GJS 1.64 (GNOME 3.36)
|
|
993
|
+
*/
|
|
994
|
+
recursiveUnpack(): $ParseRecursiveVariant<S>;
|
|
995
|
+
recursiveUnpack<T>(): T;
|
|
996
|
+
recursiveUnpack(): $ParseRecursiveVariant<S>; // Duplicate overload ensures optimal type inference for ReturnType<...>
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
/**
|
|
1000
|
+
* A utility class for building complex GVariant structures incrementally.
|
|
1001
|
+
*
|
|
1002
|
+
* VariantBuilder is useful when you need to construct variants dynamically
|
|
1003
|
+
* or when dealing with complex nested structures. It provides a way to
|
|
1004
|
+
* build variants step by step rather than constructing the entire structure at once.
|
|
1005
|
+
*
|
|
1006
|
+
* @example
|
|
1007
|
+
* ```typescript
|
|
1008
|
+
* // Building an array of variants
|
|
1009
|
+
* const builder = new GLib.VariantBuilder(new GLib.VariantType('av'));
|
|
1010
|
+
* builder.add_value(GLib.Variant.new_string('first'));
|
|
1011
|
+
* builder.add_value(GLib.Variant.new_int32(42));
|
|
1012
|
+
* builder.add_value(GLib.Variant.new_boolean(true));
|
|
1013
|
+
* const arrayVariant = builder.end(); // → Variant<'av'>
|
|
1014
|
+
*
|
|
1015
|
+
* // Building a dictionary incrementally
|
|
1016
|
+
* const dictBuilder = new GLib.VariantBuilder(new GLib.VariantType('a{sv}'));
|
|
1017
|
+
* dictBuilder.add_value(GLib.Variant.new_dict_entry(
|
|
1018
|
+
* GLib.Variant.new_string('name'),
|
|
1019
|
+
* GLib.Variant.new_variant(GLib.Variant.new_string('Mario'))
|
|
1020
|
+
* ));
|
|
1021
|
+
* const dict = dictBuilder.end();
|
|
1022
|
+
* ```
|
|
1023
|
+
*/
|
|
1024
|
+
export class VariantBuilder<S extends string = 'a*'> {
|
|
1025
|
+
static $gtype: GObject.GType<VariantBuilder>;
|
|
1026
|
+
constructor(type: VariantType<S>);
|
|
1027
|
+
constructor(copy: VariantBuilder<S>);
|
|
1028
|
+
|
|
1029
|
+
// Constructors
|
|
1030
|
+
/**
|
|
1031
|
+
* Creates a new VariantBuilder for the specified type.
|
|
1032
|
+
*
|
|
1033
|
+
* @param type The type of variant to build
|
|
1034
|
+
* @returns A new VariantBuilder instance
|
|
1035
|
+
*/
|
|
1036
|
+
static ["new"]<S extends string = 'a*'>(type: VariantType<S>): VariantBuilder<S>;
|
|
1037
|
+
|
|
1038
|
+
// Members
|
|
1039
|
+
/**
|
|
1040
|
+
* Adds a value to the variant being built.
|
|
1041
|
+
*
|
|
1042
|
+
* @param value The value to add (must match the expected element type)
|
|
1043
|
+
*/
|
|
1044
|
+
add_value(value: $ElementSig<$ParseDeepVariant<S>>): void;
|
|
1045
|
+
|
|
1046
|
+
/**
|
|
1047
|
+
* Closes the current container being built.
|
|
1048
|
+
*/
|
|
1049
|
+
close(): void;
|
|
1050
|
+
|
|
1051
|
+
/**
|
|
1052
|
+
* Completes the building process and returns the constructed variant.
|
|
1053
|
+
*
|
|
1054
|
+
* @returns The completed variant
|
|
1055
|
+
*/
|
|
1056
|
+
end(): Variant<S>;
|
|
1057
|
+
|
|
1058
|
+
/**
|
|
1059
|
+
* Opens a new subcontainer of the specified type.
|
|
1060
|
+
*
|
|
1061
|
+
* @param type The type of the subcontainer to open
|
|
1062
|
+
*/
|
|
1063
|
+
open(type: VariantType): void;
|
|
1064
|
+
|
|
1065
|
+
ref(): VariantBuilder;
|
|
1066
|
+
unref(): void;
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
export class VariantDict {
|
|
1070
|
+
static $gtype: GObject.GType<VariantDict>;
|
|
1071
|
+
constructor(from_asv?: Variant | null);
|
|
1072
|
+
constructor(copy: VariantDict);
|
|
1073
|
+
// Constructors
|
|
1074
|
+
static ["new"](from_asv?: Variant | null): VariantDict;
|
|
1075
|
+
// Members
|
|
1076
|
+
clear(): void;
|
|
1077
|
+
contains(key: string): boolean;
|
|
1078
|
+
end(): Variant;
|
|
1079
|
+
insert_value(key: string, value: Variant): void;
|
|
1080
|
+
lookup_value(key: string, expected_type?: VariantType | null): Variant;
|
|
1081
|
+
ref(): VariantDict;
|
|
1082
|
+
remove(key: string): boolean;
|
|
1083
|
+
unref(): void;
|
|
1084
|
+
lookup(key: any, variantType?: any, deep?: boolean): any;
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
export class VariantType<S extends string = any> {
|
|
1088
|
+
static $gtype: GObject.GType<VariantType>;
|
|
1089
|
+
constructor(type_string: S);
|
|
1090
|
+
constructor(copy: VariantType<S>);
|
|
1091
|
+
// Constructors
|
|
1092
|
+
static ["new"]<S extends string>(type_string: S): VariantType<S>;
|
|
1093
|
+
static new_array<S extends string>(element: VariantType<S>): VariantType<`a${S}`>;
|
|
1094
|
+
static new_dict_entry<K extends string, V extends string>(key: VariantType<K>, value: VariantType<V>): VariantType<`{${K}${V}}`>;
|
|
1095
|
+
static new_maybe<S extends string>(element: VariantType<S>): VariantType<`m${S}`>;
|
|
1096
|
+
static new_tuple<Items extends (ReadonlyArray<VariantType> | readonly [VariantType])>(items: Items): VariantType<`(${$ToTuple<Items>})`>;
|
|
1097
|
+
// Members
|
|
1098
|
+
copy(): VariantType<S>;
|
|
1099
|
+
dup_string(): string;
|
|
1100
|
+
element(): VariantType;
|
|
1101
|
+
equal(type2: VariantType): boolean;
|
|
1102
|
+
first(): VariantType;
|
|
1103
|
+
free(): void;
|
|
1104
|
+
get_string_length(): number;
|
|
1105
|
+
hash(): number;
|
|
1106
|
+
is_array(): boolean;
|
|
1107
|
+
is_basic(): boolean;
|
|
1108
|
+
is_container(): boolean;
|
|
1109
|
+
is_definite(): boolean;
|
|
1110
|
+
is_dict_entry(): boolean;
|
|
1111
|
+
is_maybe(): boolean;
|
|
1112
|
+
is_subtype_of(supertype: VariantType): boolean;
|
|
1113
|
+
is_tuple(): boolean;
|
|
1114
|
+
is_variant(): boolean;
|
|
1115
|
+
key(): VariantType;
|
|
1116
|
+
n_items(): number;
|
|
1117
|
+
next(): VariantType;
|
|
1118
|
+
value(): VariantType;
|
|
1119
|
+
static checked_(arg0: string): VariantType;
|
|
1120
|
+
static string_get_depth_(type_string: string): number;
|
|
1121
|
+
static string_is_valid(type_string: string): boolean;
|
|
1122
|
+
static string_scan(string: string, limit?: string | null): [boolean, string | null];
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
<%_ } -%>
|