webdriverbidi 0.2.2

WebDriver BiDi client implementation in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use crate::model::browser::UserContext;
use crate::model::browsing_context::BrowsingContext;
use crate::model::common::{Extensible, JsUint};
use crate::model::result::EmptyResult;

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ScriptCommand {
    AddPreloadScript(AddPreloadScript),
    CallFunction(CallFunction),
    Disown(Disown),
    Evaluate(Evaluate),
    GetRealms(GetRealms),
    RemovePreloadScript(RemovePreloadScript),
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum ScriptResult {
    AddPreloadScriptResult(AddPreloadScriptResult),
    EvaluateResult(EvaluateResult),
    GetRealmsResult(GetRealmsResult),
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum ScriptEvent {
    Message(Message),
    RealmCreated(RealmCreated),
    RealmDestroyed(RealmDestroyed),
}

pub type Channel = String;

#[derive(Serialize, Deserialize, Debug)]
pub struct ChannelValue {
    #[serde(rename = "type")]
    pub value_type: String,
    pub value: ChannelProperties,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ChannelProperties {
    pub channel: Channel,
    #[serde(
        rename = "serializationOptions",
        skip_serializing_if = "Option::is_none"
    )]
    pub serialization_options: Option<SerializationOptions>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ownership: Option<ResultOwnership>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum EvaluateResult {
    EvaluateResultSuccess(EvaluateResultSuccess),
    EvaluateResultException(EvaluateResultException),
    EmptyResult(EmptyResult),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct EvaluateResultSuccess {
    #[serde(rename = "type")]
    pub result_type: String,
    pub result: RemoteValue,
    pub realm: Realm,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct EvaluateResultException {
    #[serde(rename = "type")]
    pub result_type: String,
    #[serde(rename = "exceptionDetails")]
    pub exception_details: ExceptionDetails,
    pub realm: Realm,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ExceptionDetails {
    #[serde(rename = "columnNumber")]
    pub column_number: JsUint,
    pub exception: RemoteValue,
    #[serde(rename = "lineNumber")]
    pub line_number: JsUint,
    #[serde(rename = "stackTrace")]
    pub stack_trace: StackTrace,
    pub text: String,
}

pub type Handle = String;

pub type InternalId = String;

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum LocalValue {
    RemoteReference(RemoteReference),
    PrimitiveProtocolValue(PrimitiveProtocolValue),
    ChannelValue(ChannelValue),
    ArrayLocalValue(ArrayLocalValue),
    DateLocalValue(DateLocalValue),
    MapLocalValue(MapLocalValue),
    ObjectLocalValue(ObjectLocalValue),
    RegExpLocalValue(RegExpLocalValue),
    SetLocalValue(SetLocalValue),
}

pub type ListLocalValue = Vec<LocalValue>;

#[derive(Serialize, Deserialize, Debug)]
pub struct ArrayLocalValue {
    #[serde(rename = "type")]
    pub value_type: String,
    pub value: ListLocalValue,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DateLocalValue {
    #[serde(rename = "type")]
    pub value_type: String,
    pub value: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct MappingLocalValue(pub Vec<(LocalValueOrText, LocalValue)>);

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum LocalValueOrText {
    LocalValue(LocalValue),
    Text(String),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct MapLocalValue {
    #[serde(rename = "type")]
    pub value_type: String,
    pub value: MappingLocalValue,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ObjectLocalValue {
    #[serde(rename = "type")]
    pub value_type: String,
    pub value: MappingLocalValue,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct RegExpValue {
    pub pattern: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flags: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct RegExpLocalValue {
    #[serde(rename = "type")]
    pub value_type: String,
    pub value: RegExpValue,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SetLocalValue {
    #[serde(rename = "type")]
    pub value_type: String,
    pub value: ListLocalValue,
}

pub type PreloadScript = String;

pub type Realm = String;

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum PrimitiveProtocolValue {
    StringValue(StringValue),
    NumberValue(NumberValue),
    BooleanValue(BooleanValue),
    BigIntValue(BigIntValue),
    NullValue(NullValue),
    UndefinedValue(UndefinedValue),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct UndefinedValue {
    #[serde(rename = "type")]
    pub value_type: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct NullValue {
    #[serde(rename = "type")]
    pub value_type: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct StringValue {
    #[serde(rename = "type")]
    pub value_type: String,
    pub value: String,
}

impl StringValue {
    pub fn new(value: String) -> Self {
        Self {
            value_type: "string".to_string(),
            value,
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub enum SpecialNumber {
    NaN,
    #[serde(rename = "-0")]
    NegativeZero,
    Infinity,
    #[serde(rename = "-Infinity")]
    NegativeInfinity,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct NumberValue {
    #[serde(rename = "type")]
    pub value_type: String,
    pub value: NumberOrSpecialNumber,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum NumberOrSpecialNumber {
    Number(f64),
    SpecialNumber(SpecialNumber),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct BooleanValue {
    #[serde(rename = "type")]
    pub value_type: String,
    pub value: bool,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct BigIntValue {
    #[serde(rename = "type")]
    pub value_type: String,
    pub value: String,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum RealmInfo {
    WindowRealmInfo(WindowRealmInfo),
    DedicatedWorkerRealmInfo(DedicatedWorkerRealmInfo),
    SharedWorkerRealmInfo(SharedWorkerRealmInfo),
    ServiceWorkerRealmInfo(ServiceWorkerRealmInfo),
    WorkerRealmInfo(WorkerRealmInfo),
    PaintWorkletRealmInfo(PaintWorkletRealmInfo),
    AudioWorkletRealmInfo(AudioWorkletRealmInfo),
    WorkletRealmInfo(WorkletRealmInfo),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct BaseRealmInfo {
    pub realm: Realm,
    pub origin: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct WindowRealmInfo {
    #[serde(flatten)]
    pub base: BaseRealmInfo,
    #[serde(rename = "type")]
    pub realm_type: String,
    pub context: BrowsingContext,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sandbox: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DedicatedWorkerRealmInfo {
    #[serde(flatten)]
    pub base: BaseRealmInfo,
    #[serde(rename = "type")]
    pub realm_type: String,
    pub owners: Vec<Realm>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SharedWorkerRealmInfo {
    #[serde(flatten)]
    pub base: BaseRealmInfo,
    #[serde(rename = "type")]
    pub realm_type: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ServiceWorkerRealmInfo {
    #[serde(flatten)]
    pub base: BaseRealmInfo,
    #[serde(rename = "type")]
    pub realm_type: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct WorkerRealmInfo {
    #[serde(flatten)]
    pub base: BaseRealmInfo,
    #[serde(rename = "type")]
    pub realm_type: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct PaintWorkletRealmInfo {
    #[serde(flatten)]
    pub base: BaseRealmInfo,
    #[serde(rename = "type")]
    pub realm_type: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct AudioWorkletRealmInfo {
    #[serde(flatten)]
    pub base: BaseRealmInfo,
    #[serde(rename = "type")]
    pub realm_type: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct WorkletRealmInfo {
    #[serde(flatten)]
    pub base: BaseRealmInfo,
    #[serde(rename = "type")]
    pub realm_type: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub enum RealmType {
    #[serde(rename = "window")]
    Window,
    #[serde(rename = "dedicated-worker")]
    DedicatedWorker,
    #[serde(rename = "shared-worker")]
    SharedWorker,
    #[serde(rename = "service-worker")]
    ServiceWorker,
    #[serde(rename = "worker")]
    Worker,
    #[serde(rename = "paint-worklet")]
    PaintWorklet,
    #[serde(rename = "audio-worklet")]
    AudioWorklet,
    #[serde(rename = "worklet")]
    Worklet,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum RemoteReference {
    SharedReference(SharedReference),
    RemoteObjectReference(RemoteObjectReference),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SharedReference {
    #[serde(rename = "sharedId")]
    pub shared_id: SharedId,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(flatten)]
    pub extensible: Extensible,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct RemoteObjectReference {
    pub handle: Handle,
    #[serde(rename = "sharedId", skip_serializing_if = "Option::is_none")]
    pub shared_id: Option<SharedId>,
    #[serde(flatten)]
    pub extensible: Extensible,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum RemoteValue {
    PrimitiveProtocolValue(PrimitiveProtocolValue),
    SymbolRemoteValue(SymbolRemoteValue),
    ArrayRemoteValue(ArrayRemoteValue),
    ObjectRemoteValue(ObjectRemoteValue),
    FunctionRemoteValue(FunctionRemoteValue),
    RegExpRemoteValue(RegExpRemoteValue),
    DateRemoteValue(DateRemoteValue),
    MapRemoteValue(MapRemoteValue),
    SetRemoteValue(SetRemoteValue),
    WeakMapRemoteValue(WeakMapRemoteValue),
    WeakSetRemoteValue(WeakSetRemoteValue),
    GeneratorRemoteValue(GeneratorRemoteValue),
    ErrorRemoteValue(ErrorRemoteValue),
    ProxyRemoteValue(ProxyRemoteValue),
    PromiseRemoteValue(PromiseRemoteValue),
    TypedArrayRemoteValue(TypedArrayRemoteValue),
    ArrayBufferRemoteValue(ArrayBufferRemoteValue),
    NodeListRemoteValue(NodeListRemoteValue),
    HTMLCollectionRemoteValue(HTMLCollectionRemoteValue),
    NodeRemoteValue(NodeRemoteValue),
    WindowProxyRemoteValue(WindowProxyRemoteValue),
}

pub type ListRemoteValue = Vec<RemoteValue>;

pub type MappingRemoteValue = Vec<(RemoteValueOrText, RemoteValue)>;

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum RemoteValueOrText {
    RemoteValue(RemoteValue),
    Text(String),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SymbolRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ArrayRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<ListRemoteValue>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ObjectRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<MappingRemoteValue>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct FunctionRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct RegExpRemoteValue {
    #[serde(flatten)]
    pub local_value: RegExpLocalValue,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DateRemoteValue {
    #[serde(flatten)]
    pub local_value: DateLocalValue,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct MapRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<MappingRemoteValue>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SetRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<ListRemoteValue>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct WeakMapRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct WeakSetRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GeneratorRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ErrorRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ProxyRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct PromiseRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct TypedArrayRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ArrayBufferRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct NodeListRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<ListRemoteValue>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct HTMLCollectionRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<ListRemoteValue>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct NodeRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    #[serde(rename = "sharedId", skip_serializing_if = "Option::is_none")]
    pub shared_id: Option<SharedId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<Box<NodeProperties>>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct NodeProperties {
    #[serde(rename = "nodeType")]
    pub node_type: JsUint,
    #[serde(rename = "childNodeCount")]
    pub child_node_count: JsUint,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attributes: Option<HashMap<String, String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub children: Option<Vec<NodeRemoteValue>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "localName")]
    pub local_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "mode")]
    pub mode: Option<NodePropertiesMode>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "namespaceURI")]
    pub namespace_uri: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "nodeValue")]
    pub node_value: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "shadowRoot")]
    pub shadow_root: Option<NodeRemoteValue>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")] // Match JSON values ("open" / "closed")
pub enum NodePropertiesMode {
    Open,
    Closed,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct WindowProxyRemoteValue {
    #[serde(rename = "type")]
    pub value_type: String,
    pub value: WindowProxyProperties,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<Handle>,
    #[serde(rename = "internalId", skip_serializing_if = "Option::is_none")]
    pub internal_id: Option<InternalId>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct WindowProxyProperties {
    pub context: BrowsingContext,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum ResultOwnership {
    Root,
    None,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SerializationOptions {
    #[serde(rename = "maxDomDepth", skip_serializing_if = "Option::is_none")]
    pub max_dom_depth: Option<JsUint>,
    #[serde(rename = "maxObjectDepth", skip_serializing_if = "Option::is_none")]
    pub max_object_depth: Option<JsUint>,
    #[serde(rename = "includeShadowTree", skip_serializing_if = "Option::is_none")]
    pub include_shadow_tree: Option<IncludeShadowTree>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum IncludeShadowTree {
    None,
    Open,
    All,
}

pub type SharedId = String;

#[derive(Serialize, Deserialize, Debug)]
pub struct StackFrame {
    #[serde(rename = "columnNumber")]
    pub column_number: JsUint,
    #[serde(rename = "functionName")]
    pub function_name: String,
    #[serde(rename = "lineNumber")]
    pub line_number: JsUint,
    pub url: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct StackTrace {
    #[serde(rename = "callFrames")]
    pub call_frames: Vec<StackFrame>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Source {
    pub realm: Realm,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context: Option<BrowsingContext>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RealmTarget {
    pub realm: Realm,
}

impl RealmTarget {
    pub fn new(realm: Realm) -> Self {
        Self { realm }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ContextTarget {
    pub context: BrowsingContext,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sandbox: Option<String>,
}

impl ContextTarget {
    pub fn new(context: BrowsingContext, sandbox: Option<String>) -> Self {
        Self { context, sandbox }
    }
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Target {
    ContextTarget(ContextTarget),
    RealmTarget(RealmTarget),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AddPreloadScript {
    pub method: String,
    pub params: AddPreloadScriptParameters,
}

impl AddPreloadScript {
    pub fn new(params: AddPreloadScriptParameters) -> Self {
        Self {
            method: "script.addPreloadScript".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AddPreloadScriptParameters {
    #[serde(rename = "functionDeclaration")]
    pub function_declaration: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub arguments: Option<Vec<ChannelValue>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contexts: Option<Vec<BrowsingContext>>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "userContexts")]
    pub user_contexts: Option<Vec<UserContext>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sandbox: Option<String>,
}

impl AddPreloadScriptParameters {
    pub fn new(
        function_declaration: String,
        arguments: Option<Vec<ChannelValue>>,
        contexts: Option<Vec<BrowsingContext>>,
        user_contexts: Option<Vec<UserContext>>,
        sandbox: Option<String>,
    ) -> Self {
        Self {
            function_declaration,
            arguments,
            contexts,
            user_contexts,
            sandbox,
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct AddPreloadScriptResult {
    pub script: PreloadScript,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Disown {
    pub method: String,
    pub params: DisownParameters,
}

impl Disown {
    pub fn new(params: DisownParameters) -> Self {
        Self {
            method: "script.disown".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DisownParameters {
    pub handles: Vec<Handle>,
    pub target: Target,
}

impl DisownParameters {
    pub fn new(handles: Vec<Handle>, target: Target) -> Self {
        Self { handles, target }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CallFunction {
    pub method: String,
    pub params: CallFunctionParameters,
}

impl CallFunction {
    pub fn new(params: CallFunctionParameters) -> Self {
        Self {
            method: "script.callFunction".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CallFunctionParameters {
    #[serde(rename = "functionDeclaration")]
    pub function_declaration: String,
    #[serde(rename = "awaitPromise")]
    pub await_promise: bool,
    pub target: Target,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub arguments: Option<Vec<LocalValue>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "resultOwnership")]
    pub result_ownership: Option<ResultOwnership>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "serializationOptions")]
    pub serialization_options: Option<SerializationOptions>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub this: Option<LocalValue>,
    #[serde(rename = "userActivation", skip_serializing_if = "Option::is_none")]
    pub user_activation: Option<bool>,
}

impl CallFunctionParameters {
    pub fn new(
        function_declaration: String,
        await_promise: bool,
        target: Target,
        arguments: Option<Vec<LocalValue>>,
        result_ownership: Option<ResultOwnership>,
        serialization_options: Option<SerializationOptions>,
        this: Option<LocalValue>,
        user_activation: Option<bool>,
    ) -> Self {
        Self {
            function_declaration,
            await_promise,
            target,
            arguments,
            result_ownership,
            serialization_options,
            this,
            user_activation,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Evaluate {
    pub method: String,
    pub params: EvaluateParameters,
}

impl Evaluate {
    pub fn new(params: EvaluateParameters) -> Self {
        Self {
            method: "script.evaluate".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct EvaluateParameters {
    pub expression: String,
    pub target: Target,
    #[serde(rename = "awaitPromise")]
    pub await_promise: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "resultOwnership")]
    pub result_ownership: Option<ResultOwnership>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "serializationOptions")]
    pub serialization_options: Option<SerializationOptions>,
    #[serde(rename = "userActivation", skip_serializing_if = "Option::is_none")]
    pub user_activation: Option<bool>,
}

impl EvaluateParameters {
    pub fn new(
        expression: String,
        target: Target,
        await_promise: bool,
        result_ownership: Option<ResultOwnership>,
        serialization_options: Option<SerializationOptions>,
        user_activation: Option<bool>,
    ) -> Self {
        Self {
            expression,
            target,
            await_promise,
            result_ownership,
            serialization_options,
            user_activation,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GetRealms {
    pub method: String,
    pub params: GetRealmsParameters,
}

impl GetRealms {
    pub fn new(params: GetRealmsParameters) -> Self {
        Self {
            method: "script.getRealms".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GetRealmsParameters {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context: Option<BrowsingContext>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "type")]
    pub realm_type: Option<RealmType>,
}

impl GetRealmsParameters {
    pub fn new(context: Option<BrowsingContext>, realm_type: Option<RealmType>) -> Self {
        Self {
            context,
            realm_type,
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GetRealmsResult {
    pub realms: Vec<RealmInfo>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RemovePreloadScript {
    pub method: String,
    pub params: RemovePreloadScriptParameters,
}

impl RemovePreloadScript {
    pub fn new(params: RemovePreloadScriptParameters) -> Self {
        Self {
            method: "script.removePreloadScript".to_string(),
            params,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RemovePreloadScriptParameters {
    pub script: PreloadScript,
}

impl RemovePreloadScriptParameters {
    pub fn new(script: PreloadScript) -> Self {
        Self { script }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Message {
    pub method: String,
    pub params: MessageParameters,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct MessageParameters {
    pub channel: Channel,
    pub data: RemoteValue,
    pub source: Source,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct RealmCreated {
    pub method: String,
    pub params: RealmInfo,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct RealmDestroyed {
    pub method: String,
    pub params: RealmDestroyedParameters,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct RealmDestroyedParameters {
    pub realm: Realm,
}