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
// MIT License
//
// Copyright (c) 2025 Takatoshi Kondo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use Vec;
use fmt;
use mem;
use Builder;
use IoSlice;
use ;
use Serialize;
use ;
use crate;
use crateVariableByteInteger;
use crateGenericPacketDisplay;
use crateGenericPacketTrait;
use crateIsPacketId;
use crateSubEntry;
use crateMqttError;
/// MQTT 3.1.1 SUBSCRIBE packet representation
///
/// The SUBSCRIBE packet is sent by a client to subscribe to one or more topic filters
/// on the server. Each subscription establishes a flow of messages from the server to
/// the client based on the matching topic filters and their associated Quality of Service (QoS) levels.
///
/// According to MQTT 3.1.1 specification section 3.8, the SUBSCRIBE packet contains:
/// - Fixed header with packet type (1000), reserved flags (0010), and remaining length
/// - Variable header with packet identifier
/// - Payload containing one or more topic filter entries with QoS levels
///
/// # Fixed Header
///
/// The SUBSCRIBE packet uses a fixed header with:
/// - **Packet Type**: 8 (1000 binary) in bits 7-4 of the first byte
/// - **Reserved Flags**: 2 (0010 binary) in bits 3-0 - these flags are reserved and must be set as shown
/// - **Remaining Length**: Variable length encoding of the remaining packet data
///
/// # Variable Header
///
/// The variable header contains:
/// - **Packet Identifier**: A 16-bit identifier used to match SUBSCRIBE with SUBACK packets
///
/// # Payload
///
/// The payload contains one or more subscription entries, each consisting of:
/// - **Topic Filter**: UTF-8 encoded string that may contain wildcards (+ and #)
/// - **QoS Level**: Single byte indicating the maximum QoS level for messages (0, 1, or 2)
///
/// # Quality of Service (QoS)
///
/// Each subscription specifies a maximum QoS level:
/// - **QoS 0**: At most once delivery
/// - **QoS 1**: At least once delivery
/// - **QoS 2**: Exactly once delivery
///
/// # Topic Filters and Wildcards
///
/// Topic filters can include wildcards:
/// - **Single-level wildcard (+)**: Matches exactly one topic level (e.g., "sport/+/player1")
/// - **Multi-level wildcard (#)**: Matches any number of topic levels (e.g., "sport/#")
///
/// # Protocol Differences from MQTT 5.0
///
/// MQTT 3.1.1 SUBSCRIBE packets differ from MQTT 5.0 in several ways:
/// - No properties section in the variable header
/// - Subscription options are limited to QoS level only
/// - No subscription identifiers, user properties, or advanced subscription options
/// - Simpler packet structure with fewer configuration options
///
/// # Generic Type Parameter
///
/// The `PacketIdType` generic parameter allows using packet identifiers larger than
/// the standard u16, which can be useful for broker clusters to avoid packet ID
/// exhaustion when extending the MQTT protocol.
///
/// # Examples
///
/// ```ignore
/// use mqtt_protocol_core::mqtt;
/// use mqtt_protocol_core::mqtt::packet::qos::Qos;
/// use mqtt_protocol_core::mqtt::packet::SubEntry;
///
/// // Create a simple SUBSCRIBE packet for a single topic
/// let subscribe = mqtt::packet::v3_1_1::Subscribe::builder()
/// .packet_id(42)
/// .entries(vec![
/// SubEntry::new(
/// "sensors/temperature",
/// mqtt::packet::SubOpts::new().set_qos(Qos::AtLeastOnce)
/// )?
/// ])
/// .build()
/// .unwrap();
///
/// assert_eq!(subscribe.packet_id(), 42);
/// assert_eq!(subscribe.entries().len(), 1);
/// assert_eq!(subscribe.entries()[0].topic_filter(), "sensors/temperature");
///
/// // Create a SUBSCRIBE packet with multiple topics and different QoS levels
/// let subscribe = mqtt::packet::v3_1_1::Subscribe::builder()
/// .packet_id(123)
/// .entries(vec![
/// SubEntry::new(
/// "home/+/temperature",
/// mqtt::packet::SubOpts::new().set_qos(Qos::AtMostOnce)
/// )?,
/// SubEntry::new(
/// "alerts/#",
/// mqtt::packet::SubOpts::new().set_qos(Qos::ExactlyOnce)
/// )?
/// ])
/// .build()
/// .unwrap();
///
/// assert_eq!(subscribe.packet_id(), 123);
/// assert_eq!(subscribe.entries().len(), 2);
///
/// // Serialize to bytes for network transmission
/// let buffers = subscribe.to_buffers();
/// let total_size = subscribe.size();
/// ```
/// Type alias for SUBSCRIBE packet with standard u16 packet identifiers
///
/// This is the most commonly used SUBSCRIBE packet type for standard MQTT 3.1.1
/// implementations that use 16-bit packet identifiers as specified in the protocol.
///
/// # Examples
///
/// ```ignore
/// use mqtt_protocol_core::mqtt;
/// use mqtt_protocol_core::mqtt::packet::qos::Qos;
/// use mqtt_protocol_core::mqtt::packet::SubEntry;
///
/// let subscribe = mqtt::packet::v3_1_1::Subscribe::builder()
/// .packet_id(1)
/// .entries(vec![
/// SubEntry::new(
/// "my/topic",
/// mqtt::packet::SubOpts::new().set_qos(Qos::AtLeastOnce)
/// )?
/// ])
/// .build()
/// .unwrap();
/// ```
pub type Subscribe = ;
/// Builder implementation for constructing SUBSCRIBE packets
///
/// The builder provides a fluent interface for constructing SUBSCRIBE packets with
/// validation of required fields and protocol compliance. It ensures that all
/// SUBSCRIBE packets have a valid packet identifier and at least one subscription entry.
/// Display trait implementation for SUBSCRIBE packets
///
/// Provides a human-readable JSON representation of the SUBSCRIBE packet,
/// including the packet type, packet identifier, and subscription entries.
/// This is useful for debugging and logging purposes.
///
/// # Examples
///
/// ```ignore
/// use mqtt_protocol_core::mqtt;
/// use mqtt_protocol_core::mqtt::packet::qos::Qos;
/// use mqtt_protocol_core::mqtt::packet::SubEntry;
///
/// let subscribe = mqtt::packet::v3_1_1::Subscribe::builder()
/// .packet_id(42)
/// .entries(vec![
/// SubEntry::new(
/// "test/topic",
/// mqtt::packet::SubOpts::new().set_qos(Qos::AtLeastOnce)
/// )?
/// ])
/// .build()
/// .unwrap();
///
/// println!("{}", subscribe);
/// // Output: {"type":"subscribe","packet_id":42,"entries":[...]}
/// ```
/// Debug trait implementation for SUBSCRIBE packets
///
/// Provides the same output as the Display trait, showing a JSON representation
/// of the packet contents. This is consistent with other packet types in the library.
///
/// # Examples
///
/// ```ignore
/// use mqtt_protocol_core::mqtt;
/// use mqtt_protocol_core::mqtt::packet::qos::Qos;
/// use mqtt_protocol_core::mqtt::packet::SubEntry;
///
/// let subscribe = mqtt::packet::v3_1_1::Subscribe::builder()
/// .packet_id(1)
/// .entries(vec![
/// SubEntry::new(
/// "debug/topic",
/// mqtt::packet::SubOpts::new().set_qos(Qos::AtMostOnce)
/// )?
/// ])
/// .build()
/// .unwrap();
///
/// println!("{:?}", subscribe);
/// ```
/// Serde Serialize trait implementation for SUBSCRIBE packets
///
/// Serializes the SUBSCRIBE packet to a structured format (typically JSON)
/// containing the packet type, packet identifier, and subscription entries
/// (if any). Empty collections are omitted from the output.
///
/// # Serialized Fields
///
/// - `type`: Always "subscribe"
/// - `packet_id`: The packet identifier
/// - `entries`: Array of subscription entries (only if non-empty)
///
/// # Examples
///
/// ```ignore
/// use mqtt_protocol_core::mqtt;
/// use mqtt_protocol_core::mqtt::packet::qos::Qos;
/// use mqtt_protocol_core::mqtt::packet::SubEntry;
///
/// let subscribe = mqtt::packet::v3_1_1::Subscribe::builder()
/// .packet_id(123)
/// .entries(vec![
/// SubEntry::new(
/// "home/sensor",
/// mqtt::packet::SubOpts::new().set_qos(Qos::ExactlyOnce)
/// )?
/// ])
/// .build()
/// .unwrap();
///
/// let json = serde_json::to_string(&subscribe).unwrap();
/// // json contains: {"type":"subscribe","packet_id":123,"entries":[...]}
/// ```
/// Generic packet trait implementation for SUBSCRIBE packets
///
/// Provides the common packet interface used by the MQTT protocol handler.
/// This allows SUBSCRIBE packets to be treated uniformly with other packet types
/// for size calculation and serialization operations.
///
/// # Methods
///
/// - `size()`: Returns the total packet size in bytes
/// - `to_buffers()`: Returns I/O slices for efficient transmission
/// Generic packet display trait implementation for SUBSCRIBE packets
///
/// Provides unified display formatting for packet types, supporting both
/// debug and display formatting through a common interface. This is used
/// by the packet handling infrastructure for consistent logging and debugging.