matc 0.1.3

Matter protocol library (controller side)
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
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
//! Utilities to decode/encode matter tlv

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use core::fmt;
use std::io::{Cursor, Read, Result, Write};

/// Buffer to encode matter tlv. Create buffer, write elements then use data member which contains encoded tlv.
/// Example how to commission device using certificates pre-created in pem directory:
/// ```
/// # use matc::tlv;
/// # use anyhow::Result;
/// # fn main() -> Result<()> {
/// let mut tlv = tlv::TlvBuffer::new();
/// tlv.write_struct(1)?;
/// tlv.write_uint8(0, 100)?;
/// tlv.write_string(0, "test")?;
/// tlv.write_struct_end()?;
/// // now tlv.data contains encoded tlv buffer
/// # Ok(())
/// # }
/// ```
pub struct TlvBuffer {
    pub data: Vec<u8>,
}

const TYPE_INT_1: u8 = 0;
const TYPE_INT_2: u8 = 1;
const TYPE_INT_4: u8 = 2;
const TYPE_INT_8: u8 = 3;
const TYPE_UINT_1: u8 = 4;
const TYPE_UINT_2: u8 = 5;
const TYPE_UINT_4: u8 = 6;
const TYPE_UINT_8: u8 = 7;
const TYPE_BOOL_FALSE: u8 = 8;
const TYPE_BOOL_TRUE: u8 = 9;
const TYPE_UTF8_L1: u8 = 0xC;
const TYPE_OCTET_STRING_L1: u8 = 0x10;
const TYPE_OCTET_STRING_L2: u8 = 0x11;
const TYPE_OCTET_STRING_L4: u8 = 0x12;
const TYPE_OCTET_STRING_L8: u8 = 0x13;

const TYPE_STRUCT: u8 = 0x15;
const TYPE_ARRAY: u8 = 0x16;
const TYPE_LIST: u8 = 0x17;
const TYPE_END_CONTAINER: u8 = 0x18;

const CTRL_CTX_L1: u8 = 1 << 5;

impl TlvBuffer {
    pub fn new() -> Self {
        Self {
            data: Vec::with_capacity(1024),
        }
    }
    pub fn from_vec(v: Vec<u8>) -> Self {
        Self { data: v }
    }
    pub fn write_raw(&mut self, data: &[u8]) -> Result<()> {
        self.data.write_all(data)
    }
    pub fn write_anon_struct(&mut self) -> Result<()> {
        self.data.write_u8(TYPE_STRUCT)?;
        Ok(())
    }
    pub fn write_anon_list(&mut self) -> Result<()> {
        self.data.write_u8(TYPE_LIST)?;
        Ok(())
    }
    pub fn write_struct(&mut self, tag: u8) -> Result<()> {
        self.data.write_u8(CTRL_CTX_L1 | TYPE_STRUCT)?;
        self.data.write_u8(tag)?;
        Ok(())
    }
    pub fn write_array(&mut self, tag: u8) -> Result<()> {
        self.data.write_u8(CTRL_CTX_L1 | TYPE_ARRAY)?;
        self.data.write_u8(tag)?;
        Ok(())
    }
    pub fn write_list(&mut self, tag: u8) -> Result<()> {
        self.data.write_u8(CTRL_CTX_L1 | TYPE_LIST)?;
        self.data.write_u8(tag)?;
        Ok(())
    }
    pub fn write_struct_end(&mut self) -> Result<()> {
        self.data.write_u8(TYPE_END_CONTAINER)?;
        Ok(())
    }
    pub fn write_string(&mut self, tag: u8, data: &str) -> Result<()> {
        let ctrl = CTRL_CTX_L1 | TYPE_UTF8_L1;
        let bytes = data.as_bytes();
        self.data.write_u8(ctrl)?;
        self.data.write_u8(tag)?;
        self.data.write_u8(bytes.len() as u8)?;
        self.data.write_all(bytes)?;
        Ok(())
    }
    pub fn write_octetstring(&mut self, tag: u8, data: &[u8]) -> Result<()> {
        if data.len() > 0xff {
            self.data.write_u8(CTRL_CTX_L1 | TYPE_OCTET_STRING_L2)?;
            self.data.write_u8(tag)?;
            self.data.write_u16::<LittleEndian>(data.len() as u16)?;
        } else {
            self.data.write_u8(CTRL_CTX_L1 | TYPE_OCTET_STRING_L1)?;
            self.data.write_u8(tag)?;
            self.data.write_u8(data.len() as u8)?;
        }
        self.data.write_all(data)?;
        Ok(())
    }
    pub fn write_int8(&mut self, tag: u8, value: i8) -> Result<()> {
        self.data.write_u8(CTRL_CTX_L1 | TYPE_INT_1)?;
        self.data.write_u8(tag)?;
        self.data.write_i8(value)
    }
    pub fn write_int16(&mut self, tag: u8, value: i16) -> Result<()> {
        self.data.write_u8(CTRL_CTX_L1 | TYPE_INT_2)?;
        self.data.write_u8(tag)?;
        self.data.write_i16::<LittleEndian>(value)
    }
    pub fn write_int32(&mut self, tag: u8, value: i32) -> Result<()> {
        self.data.write_u8(CTRL_CTX_L1 | TYPE_INT_4)?;
        self.data.write_u8(tag)?;
        self.data.write_i32::<LittleEndian>(value)
    }
    pub fn write_int64(&mut self, tag: u8, value: i64) -> Result<()> {
        self.data.write_u8(CTRL_CTX_L1 | TYPE_INT_8)?;
        self.data.write_u8(tag)?;
        self.data.write_i64::<LittleEndian>(value)
    }
    pub fn write_uint8(&mut self, tag: u8, value: u8) -> Result<()> {
        self.data.write_u8(CTRL_CTX_L1 | TYPE_UINT_1)?;
        self.data.write_u8(tag)?;
        self.data.write_u8(value)
    }
    pub fn write_octetstring_notag(&mut self, data: &[u8]) -> Result<()> {
        if data.len() > 0xff {
            self.data.write_u8(TYPE_OCTET_STRING_L2)?;
            self.data.write_u16::<LittleEndian>(data.len() as u16)?;
        } else {
            self.data.write_u8(TYPE_OCTET_STRING_L1)?;
            self.data.write_u8(data.len() as u8)?;
        }
        self.data.write_all(data)
    }
    pub fn write_uint8_notag(&mut self, value: u8) -> Result<()> {
        self.data.write_u8(TYPE_UINT_1)?;
        self.data.write_u8(value)
    }
    pub fn write_uint16_notag(&mut self, value: u16) -> Result<()> {
        self.data.write_u8(TYPE_UINT_2)?;
        self.data.write_u16::<LittleEndian>(value)
    }
    pub fn write_uint32_notag(&mut self, value: u32) -> Result<()> {
        self.data.write_u8(TYPE_UINT_4)?;
        self.data.write_u32::<LittleEndian>(value)
    }
    pub fn write_uint16(&mut self, tag: u8, value: u16) -> Result<()> {
        self.data.write_u8(CTRL_CTX_L1 | TYPE_UINT_2)?;
        self.data.write_u8(tag)?;
        self.data.write_u16::<LittleEndian>(value)
    }
    pub fn write_uint32(&mut self, tag: u8, value: u32) -> Result<()> {
        self.data.write_u8(CTRL_CTX_L1 | TYPE_UINT_4)?;
        self.data.write_u8(tag)?;
        self.data.write_u32::<LittleEndian>(value)
    }
    pub fn write_uint64(&mut self, tag: u8, value: u64) -> Result<()> {
        self.data.write_u8(CTRL_CTX_L1 | TYPE_UINT_8)?;
        self.data.write_u8(tag)?;
        self.data.write_u64::<LittleEndian>(value)
    }
    pub fn write_bool(&mut self, tag: u8, value: bool) -> Result<()> {
        if value {
            self.data.write_u8(CTRL_CTX_L1 | TYPE_BOOL_TRUE)?;
        } else {
            self.data.write_u8(CTRL_CTX_L1 | TYPE_BOOL_FALSE)?;
        }
        self.data.write_u8(tag)
    }
}

impl Default for TlvBuffer {
    fn default() -> Self {
        Self::new()
    }
}

/// Enum containing data of decoded tlv element
#[derive(Clone, PartialEq)]
pub enum TlvItemValue {
    Int(u64),
    Bool(bool),
    String(String),
    OctetString(Vec<u8>),
    List(Vec<TlvItem>),
    Nil(),
    Invalid(),
}

impl From<TlvItemValue> for bool {
    fn from(value: TlvItemValue) -> Self {
        match value {
            TlvItemValue::Bool(b) => b,
            _ => false,
        }
    }
}
impl From<TlvItemValue> for String {
    fn from(value: TlvItemValue) -> Self {
        match value {
            TlvItemValue::String(s) => s,
            _ => String::new(),
        }
    }
}

impl<'a> TryFrom<&'a TlvItemValue> for &'a [u8] {
    type Error = &'static str;
    fn try_from(value: &'a TlvItemValue) -> std::result::Result<Self, Self::Error> {
        if let TlvItemValue::OctetString(ref s) = value {
            Ok(s.as_slice())
        } else {
            Err("Not an octet string")
        }
    }
}
impl From<TlvItemValue> for Vec<u8> {
    fn from(value: TlvItemValue) -> Self {
        match value {
            TlvItemValue::OctetString(s) => s,
            _ => Vec::new(),
        }
    }
}
impl From<TlvItemValue> for u64 {
    fn from(value: TlvItemValue) -> Self {
        match value {
            TlvItemValue::Int(i) => i,
            _ => 0,
        }
    }
}
impl From<TlvItemValue> for Vec<TlvItem> {
    fn from(value: TlvItemValue) -> Self {
        match value {
            TlvItemValue::List(lst) => lst,
            _ => panic!("Cannot convert to Vec<TlvItem>"),
        }
    }
}

/// Decoded tlv element returned by [decode_tlv]
#[derive(Debug, Clone, PartialEq)]
pub struct TlvItem {
    pub tag: u8,
    pub value: TlvItemValue,
}

impl fmt::Debug for TlvItemValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Int(arg0) => f.debug_tuple("Int").field(arg0).finish(),
            Self::Bool(arg0) => f.debug_tuple("Bool").field(arg0).finish(),
            Self::String(arg0) => f.debug_tuple("String").field(arg0).finish(),
            Self::OctetString(arg0) => f
                .debug_tuple("OctetString")
                .field(&hex::encode(arg0))
                .finish(),
            Self::List(arg0) => f.debug_tuple("List").field(arg0).finish(),
            Self::Nil() => f.debug_tuple("Nil").finish(),
            Self::Invalid() => f.debug_tuple("Invalid").finish(),
        }
    }
}

impl TlvItem {
    pub fn get(&self, tag: &[u8]) -> Option<&TlvItemValue> {
        if !tag.is_empty() {
            if let TlvItemValue::List(lst) = &self.value {
                for l in lst {
                    if l.tag == tag[0] {
                        return l.get(&tag[1..]);
                    };
                }
            }
            None
        } else {
            Some(&self.value)
        }
    }
    pub fn get_item(&self, tag: &[u8]) -> Option<&TlvItem> {
        if !tag.is_empty() {
            if let TlvItemValue::List(lst) = &self.value {
                for l in lst {
                    if l.tag == tag[0] {
                        return l.get_item(&tag[1..]);
                    };
                }
            }
            None
        } else {
            Some(self)
        }
    }
    pub fn get_int(&self, tag: &[u8]) -> Option<u64> {
        let found = self.get(tag);
        if let Some(TlvItemValue::Int(i)) = found {
            Some(*i)
        } else {
            None
        }
    }
    pub fn get_t<T>(&self, tag: &[u8]) -> Option<T>
    where
        T: From<TlvItemValue>,
    {
        self.get(tag).map(|f| f.clone().into())
    }

    pub fn get_bool(&self, tag: &[u8]) -> Option<bool> {
        self.get(tag).map(|f| f.clone().into())
        /*let found = self.get(tag);
        if let Some(TlvItemValue::Bool(i)) = found {
            Some(*i)
        } else {
            None
        }*/
    }
    pub fn get_u8(&self, tag: &[u8]) -> Option<u8> {
        let found = self.get(tag);
        if let Some(TlvItemValue::Int(i)) = found {
            Some(*i as u8)
        } else {
            None
        }
    }
    pub fn get_u16(&self, tag: &[u8]) -> Option<u16> {
        let found = self.get(tag);
        if let Some(TlvItemValue::Int(i)) = found {
            Some(*i as u16)
        } else {
            None
        }
    }
    pub fn get_u32(&self, tag: &[u8]) -> Option<u32> {
        let found = self.get(tag);
        if let Some(TlvItemValue::Int(i)) = found {
            Some(*i as u32)
        } else {
            None
        }
    }
    pub fn get_u64(&self, tag: &[u8]) -> Option<u64> {
        let found = self.get(tag);
        if let Some(TlvItemValue::Int(i)) = found {
            Some(*i)
        } else {
            None
        }
    }
    pub fn get_octet_string(&self, tag: &[u8]) -> Option<&[u8]> {
        let found = self.get(tag);
        if let Some(TlvItemValue::OctetString(o)) = found {
            Some(o)
        } else {
            None
        }
    }
    pub fn get_octet_string_owned(&self, tag: &[u8]) -> Option<Vec<u8>> {
        let found = self.get(tag);
        if let Some(TlvItemValue::OctetString(o)) = found {
            Some(o.to_owned())
        } else {
            None
        }
    }
    pub fn get_string_owned(&self, tag: &[u8]) -> Option<String> {
        let found = self.get(tag);
        if let Some(TlvItemValue::String(o)) = found {
            Some(o.clone())
        } else {
            None
        }
    }
    pub fn dump(&self, indent: usize) {
        match &self.value {
            TlvItemValue::List(vec) => {
                println!("{} {}", " ".to_owned().repeat(indent), self.tag);
                for v in vec {
                    v.dump(indent + 1);
                }
            }
            _ => {
                println!(
                    "{} {} {:?}",
                    " ".to_owned().repeat(indent),
                    self.tag,
                    self.value
                );
            }
        }
    }
}

fn read_tag(tagctrl: u8, cursor: &mut Cursor<&[u8]>) -> Result<u8> {
    if tagctrl == 1 {
        cursor.read_u8()
    } else {
        Ok(0)
    }
}

fn decode(cursor: &mut Cursor<&[u8]>, container: &mut Vec<TlvItem>) -> Result<()> {
    while cursor.position() < cursor.get_ref().len() as u64 {
        let fb = cursor.read_u8()?;
        let tp = fb & 0x1f;
        let tagctrl = fb >> 5;
        let tag = read_tag(tagctrl, cursor)?;
        match tp {
            TYPE_INT_1 => {
                let value = cursor.read_u8()?;
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::Int(value as u64),
                };
                container.push(item);
            }
            TYPE_INT_2 => {
                let value = cursor.read_i16::<LittleEndian>()?;
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::Int(value as u64),
                };
                container.push(item);
            }
            TYPE_INT_4 => {
                let value = cursor.read_i32::<LittleEndian>()?;
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::Int(value as u64),
                };
                container.push(item);
            }
            TYPE_INT_8 => {
                let value = cursor.read_i64::<LittleEndian>()?;
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::Int(value as u64),
                };
                container.push(item);
            }
            TYPE_UINT_1 => {
                let value = cursor.read_u8()?;
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::Int(value as u64),
                };
                container.push(item);
            }
            TYPE_UINT_2 => {
                let value = cursor.read_u16::<LittleEndian>()?;
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::Int(value as u64),
                };
                container.push(item);
            }
            TYPE_UINT_4 => {
                let value = cursor.read_u32::<LittleEndian>()?;
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::Int(value as u64),
                };
                container.push(item);
            }
            TYPE_UINT_8 => {
                let value = cursor.read_u64::<LittleEndian>()?;
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::Int(value),
                };
                container.push(item);
            }
            TYPE_BOOL_FALSE => {
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::Bool(false),
                };
                container.push(item);
            }
            TYPE_BOOL_TRUE => {
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::Bool(true),
                };
                container.push(item);
            }
            TYPE_UTF8_L1 => {
                // utf8 string
                let size = cursor.read_u8()?;
                let mut value = vec![0; size as usize];
                cursor.read_exact(&mut value)?;
                let str = String::from_utf8(value);
                let typ = match str {
                    Ok(s) => TlvItemValue::String(s),
                    Err(_) => TlvItemValue::Invalid(),
                };
                let item = TlvItem { tag, value: typ };
                container.push(item);
            }
            TYPE_OCTET_STRING_L1 => {
                // octet string
                let size = cursor.read_u8()?;
                let mut value = vec![0; size as usize];
                cursor.read_exact(&mut value)?;
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::OctetString(value),
                };
                container.push(item);
            }
            TYPE_OCTET_STRING_L2 => {
                // octet string large
                let size = cursor.read_u16::<LittleEndian>()?;
                let mut value = vec![0; size as usize];
                cursor.read_exact(&mut value)?;
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::OctetString(value),
                };
                container.push(item);
            }
            TYPE_OCTET_STRING_L4 => {
                // octet string very large
                let size = cursor.read_u32::<LittleEndian>()?;
                let mut value = vec![0; size as usize];
                cursor.read_exact(&mut value)?;
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::OctetString(value),
                };
                container.push(item);
            }
            TYPE_OCTET_STRING_L8 => {
                // octet string very very xtra large
                let size = cursor.read_u64::<LittleEndian>()?;
                let mut value = vec![0; size as usize];
                cursor.read_exact(&mut value)?;
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::OctetString(value),
                };
                container.push(item);
            }
            TYPE_STRUCT => {
                //list
                let mut c2 = Vec::new();
                decode(cursor, &mut c2)?;
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::List(c2),
                };
                container.push(item);
            }
            TYPE_ARRAY => {
                //list
                let mut c2 = Vec::new();
                decode(cursor, &mut c2)?;
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::List(c2),
                };
                container.push(item);
            }
            TYPE_LIST => {
                //list
                let mut c2 = Vec::new();
                decode(cursor, &mut c2)?;
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::List(c2),
                };
                container.push(item);
            }
            TYPE_END_CONTAINER => return Ok(()),
            0x14 => {
                let item = TlvItem {
                    tag,
                    value: TlvItemValue::Nil(),
                };
                container.push(item);
            }
            _ => {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    format!("unknown tlv type 0x{:x}", tp),
                ))
            }
        }
    }
    Ok(())
}

/// decode raw buffer with tlv data
pub fn decode_tlv(data: &[u8]) -> Result<TlvItem> {
    let mut container = Vec::new();
    let mut cursor = std::io::Cursor::new(data);
    decode(&mut cursor, &mut container)?;
    if container.len() == 1 {
        if let Some(i) = container.pop() {
            Ok(i)
        } else {
            Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "no data found",
            ))
        }
    } else {
        Ok(TlvItem {
            tag: 0,
            value: TlvItemValue::List(container),
        })
    }
}

#[derive(Debug)]
pub enum TlvItemValueEnc {
    Int8(i8),
    Int16(i16),
    Int32(i32),
    Int64(i64),
    UInt8(u8),
    UInt8Anonymous(u8),
    UInt16(u16),
    UInt32(u32),
    UInt64(u64),
    Bool(bool),
    String(String),
    OctetString(Vec<u8>),
    StructAnon(Vec<TlvItemEnc>),
    StructInvisible(Vec<TlvItemEnc>),
    Struct(Vec<TlvItemEnc>),
    List(Vec<TlvItemEnc>),
    Array(Vec<TlvItemEnc>),
    Invalid(),
}

/// Structure used for document style encoding.
///
/// ```
/// # use matc::tlv;
/// let t1 = tlv::TlvItemEnc {
///   tag: 0,
///   value: tlv::TlvItemValueEnc::StructAnon(vec![
///     tlv::TlvItemEnc { tag: 0, value: tlv::TlvItemValueEnc::UInt8(6) },
///     tlv::TlvItemEnc { tag: 1, value: tlv::TlvItemValueEnc::UInt8(7) }
///   ]),
/// };
/// let o = t1.encode().unwrap();
/// ```
#[derive(Debug)]
pub struct TlvItemEnc {
    pub tag: u8,
    pub value: TlvItemValueEnc,
}

impl From<(u8, TlvItemValueEnc)> for TlvItemEnc {
    fn from(item: (u8, TlvItemValueEnc)) -> Self {
        TlvItemEnc {
            tag: item.0,
            value: item.1,
        }
    }
}

impl TlvItemEnc {
    fn encode_internal(&self, buf: &mut TlvBuffer) -> Result<()> {
        match &self.value {
            TlvItemValueEnc::Int8(i) => {
                buf.write_int8(self.tag, *i)?;
            }
            TlvItemValueEnc::Int16(i) => {
                buf.write_int16(self.tag, *i)?;
            }
            TlvItemValueEnc::Int32(i) => {
                buf.write_int32(self.tag, *i)?;
            }
            TlvItemValueEnc::Int64(i) => {
                buf.write_int64(self.tag, *i)?;
            }
            TlvItemValueEnc::UInt8(i) => {
                buf.write_uint8(self.tag, *i)?;
            }
            TlvItemValueEnc::UInt8Anonymous(i) => {
                buf.write_uint8_notag(*i)?;
            }
            TlvItemValueEnc::UInt16(i) => {
                buf.write_uint16(self.tag, *i)?;
            }
            TlvItemValueEnc::UInt32(i) => {
                buf.write_uint32(self.tag, *i)?;
            }
            TlvItemValueEnc::UInt64(i) => {
                buf.write_uint64(self.tag, *i)?;
            }
            TlvItemValueEnc::Bool(v) => {
                buf.write_bool(self.tag, *v)?;
            }
            TlvItemValueEnc::String(s) => {
                buf.write_string(self.tag, s)?;
            }
            TlvItemValueEnc::OctetString(vec) => {
                buf.write_octetstring(self.tag, vec)?;
            }
            TlvItemValueEnc::StructAnon(vec) => {
                buf.write_anon_struct()?;
                for i in vec {
                    i.encode_internal(buf)?;
                }
                buf.write_struct_end()?;
            }
            TlvItemValueEnc::Struct(vec) => {
                buf.write_struct(self.tag)?;
                for i in vec {
                    i.encode_internal(buf)?;
                }
                buf.write_struct_end()?;
            }
            TlvItemValueEnc::List(vec) => {
                buf.write_list(self.tag)?;
                for i in vec {
                    i.encode_internal(buf)?;
                }
                buf.write_struct_end()?;
            }
            TlvItemValueEnc::Array(vec) => {
                buf.write_array(self.tag)?;
                for i in vec {
                    i.encode_internal(buf)?;
                }
                buf.write_struct_end()?;
            }
            TlvItemValueEnc::StructInvisible(vec) => {
                for i in vec {
                    i.encode_internal(buf)?;
                }
            }
            TlvItemValueEnc::Invalid() => todo!(),
        }
        Ok(())
    }

    pub fn encode(&self) -> Result<Vec<u8>> {
        let mut tlv = TlvBuffer::new();
        self.encode_internal(&mut tlv)?;
        Ok(tlv.data)
    }
}

#[cfg(test)]
mod tests {
    use super::{decode_tlv, TlvBuffer, TlvItemEnc, TlvItemValue, TlvItemValueEnc};

    #[test]
    fn test_1() {
        let t1 = TlvItemEnc {
            tag: 0,
            value: TlvItemValueEnc::StructAnon(vec![
                TlvItemEnc {
                    tag: 0,
                    value: TlvItemValueEnc::UInt8(6),
                },
                TlvItemEnc {
                    tag: 1,
                    value: TlvItemValueEnc::UInt8(7),
                },
            ]),
        };
        let o = t1.encode().unwrap();
        assert_eq!(hex::encode(o), "1524000624010718");

        let mut tlv = TlvBuffer::new();
        tlv.write_anon_struct().unwrap();
        tlv.write_octetstring(0x1, &[1, 2, 3]).unwrap();
        tlv.write_struct_end().unwrap();
        assert_eq!(hex::encode(tlv.data), "1530010301020318");

        let t1 = TlvItemEnc {
            tag: 0,
            value: TlvItemValueEnc::StructAnon(vec![TlvItemEnc {
                tag: 1,
                value: TlvItemValueEnc::OctetString(vec![1, 2, 3]),
            }]),
        }
        .encode()
        .unwrap();
        assert_eq!(hex::encode(t1), "1530010301020318");
    }

    #[test]
    fn test_decode_integers() {
        // Test uint8
        let mut tlv = TlvBuffer::new();
        tlv.write_uint8(1, 42).unwrap();
        let decoded = decode_tlv(&tlv.data).unwrap();
        assert_eq!(decoded.get_u8(&[]), Some(42));

        // Test uint16
        let mut tlv = TlvBuffer::new();
        tlv.write_uint16(2, 1000).unwrap();
        let decoded = decode_tlv(&tlv.data).unwrap();
        assert_eq!(decoded.get_u16(&[]), Some(1000));

        // Test uint32
        let mut tlv = TlvBuffer::new();
        tlv.write_uint32(3, 100000).unwrap();
        let decoded = decode_tlv(&tlv.data).unwrap();
        assert_eq!(decoded.get_u32(&[]), Some(100000));

        // Test uint64
        let mut tlv = TlvBuffer::new();
        tlv.write_uint64(4, 1000000000000).unwrap();
        let decoded = decode_tlv(&tlv.data).unwrap();
        assert_eq!(decoded.get_u64(&[]), Some(1000000000000));
    }

    #[test]
    fn test_decode_booleans() {
        // Test true
        let mut tlv = TlvBuffer::new();
        tlv.write_bool(1, true).unwrap();
        let decoded = decode_tlv(&tlv.data).unwrap();
        assert_eq!(decoded.get_bool(&[]), Some(true));

        // Test false
        let mut tlv = TlvBuffer::new();
        tlv.write_bool(2, false).unwrap();
        let decoded = decode_tlv(&tlv.data).unwrap();
        assert_eq!(decoded.get_bool(&[]), Some(false));
        assert_eq!(decoded.get_t(&[]), Some(false));
    }

    #[test]
    fn test_decode_strings() {
        let mut tlv = TlvBuffer::new();
        tlv.write_string(1, "hello world").unwrap();
        let decoded = decode_tlv(&tlv.data).unwrap();
        assert_eq!(
            decoded.get_string_owned(&[]),
            Some("hello world".to_string())
        );
        assert_eq!(decoded.get_t(&[]), Some("hello world".to_string()));
    }

    #[test]
    fn test_decode_octet_strings() {
        // Test small octet string (L1)
        let mut tlv = TlvBuffer::new();
        let data = vec![1, 2, 3, 4, 5];
        tlv.write_octetstring(1, &data).unwrap();
        let decoded = decode_tlv(&tlv.data).unwrap();
        assert_eq!(decoded.get_octet_string(&[]), Some(data.as_slice()));

        // Test large octet string (L2)
        let mut tlv = TlvBuffer::new();
        let large_data = vec![0; 300]; // Larger than 255 bytes
        tlv.write_octetstring(2, &large_data).unwrap();
        let decoded = decode_tlv(&tlv.data).unwrap();
        assert_eq!(decoded.get_octet_string(&[]), Some(large_data.as_slice()));
        assert_eq!(
            decoded.get_octet_string_owned(&[]),
            Some(large_data.clone())
        );
    }

    #[test]
    fn test_decode_structures() {
        let mut tlv = TlvBuffer::new();
        tlv.write_struct(1).unwrap();
        tlv.write_uint8(0, 100).unwrap();
        tlv.write_string(1, "test").unwrap();
        tlv.write_bool(2, true).unwrap();
        tlv.write_struct_end().unwrap();

        let decoded = decode_tlv(&tlv.data).unwrap();

        // Test nested access
        assert_eq!(decoded.get_u8(&[0]), Some(100));
        assert_eq!(decoded.get_string_owned(&[1]), Some("test".to_string()));
        assert_eq!(decoded.get_bool(&[2]), Some(true));

        // Verify it's a list structure
        if let TlvItemValue::List(items) = &decoded.value {
            assert_eq!(items.len(), 3);
        }
    }

    #[test]
    fn test_decode_anonymous_structures() {
        let mut tlv = TlvBuffer::new();
        tlv.write_anon_struct().unwrap();
        tlv.write_uint8_notag(42).unwrap();
        tlv.write_uint8_notag(84).unwrap();
        tlv.write_struct_end().unwrap();

        let decoded = decode_tlv(&tlv.data).unwrap();

        if let TlvItemValue::List(items) = &decoded.value {
            assert_eq!(items.len(), 2);
            assert_eq!(items[0].tag, 0);
            assert_eq!(items[1].tag, 0);
        }
    }

    #[test]
    fn test_decode_arrays_and_lists() {
        // Test array
        let mut tlv = TlvBuffer::new();
        tlv.write_array(1).unwrap();
        tlv.write_uint8(0, 1).unwrap();
        tlv.write_uint8(0, 2).unwrap();
        tlv.write_uint8(0, 3).unwrap();
        tlv.write_struct_end().unwrap();

        let decoded = decode_tlv(&tlv.data).unwrap();
        if let Some(TlvItemValue::List(items)) = decoded.get(&[]) {
            assert_eq!(items.len(), 3);
            assert_eq!(items[0].get_u8(&[]), Some(1));
            assert_eq!(items[1].get_u8(&[]), Some(2));
            assert_eq!(items[2].get_u8(&[]), Some(3));
        } else {
            panic!("Expected array structure");
        }

        // Test list
        let mut tlv = TlvBuffer::new();
        tlv.write_list(2).unwrap();
        tlv.write_string(0, "item1").unwrap();
        tlv.write_string(1, "item2").unwrap();
        tlv.write_struct_end().unwrap();

        let decoded = decode_tlv(&tlv.data).unwrap();
        if let Some(TlvItemValue::List(items)) = decoded.get(&[]) {
            assert_eq!(items.len(), 2);
            assert_eq!(items[0].get_string_owned(&[]), Some("item1".to_string()));
            assert_eq!(items[1].get_string_owned(&[]), Some("item2".to_string()));
        } else {
            panic!("Expected list structure");
        }
    }

    #[test]
    fn test_decode_mixed_container() {
        let mut tlv = TlvBuffer::new();
        tlv.write_uint8(0, 255).unwrap();
        tlv.write_string(1, "mixed").unwrap();
        tlv.write_bool(2, false).unwrap();

        let decoded = decode_tlv(&tlv.data).unwrap();

        // Should create a list with multiple items
        if let TlvItemValue::List(items) = &decoded.value {
            assert_eq!(items.len(), 3);
            assert_eq!(items[0].get_u8(&[]), Some(255));
            assert_eq!(items[1].get_string_owned(&[]), Some("mixed".to_string()));
            assert_eq!(items[2].get_bool(&[]), Some(false));
        } else {
            panic!("Expected list of items");
        }
    }

    #[test]
    fn test_decode_nested_structures() {
        let mut tlv = TlvBuffer::new();
        tlv.write_struct(1).unwrap();
        tlv.write_struct(2).unwrap();
        tlv.write_uint8(3, 42).unwrap();
        tlv.write_struct_end().unwrap(); // End inner struct
        tlv.write_string(4, "outer").unwrap();
        tlv.write_struct_end().unwrap(); // End outer struct

        let decoded = decode_tlv(&tlv.data).unwrap();

        // Test deep nested access
        assert_eq!(decoded.get_u8(&[2, 3]), Some(42));
        assert_eq!(decoded.get_string_owned(&[4]), Some("outer".to_string()));
    }

    #[test]
    fn test_decode_getter_methods() {
        let mut tlv = TlvBuffer::new();
        tlv.write_struct(0).unwrap();
        tlv.write_uint64(1, 0xFFFFFFFFFFFFFFFF).unwrap();
        tlv.write_uint32(2, 0xFFFFFFFF).unwrap();
        tlv.write_uint16(3, 0xFFFF).unwrap();
        tlv.write_uint8(4, 0xFF).unwrap();
        tlv.write_struct_end().unwrap();

        let decoded = decode_tlv(&tlv.data).unwrap();

        // Test type conversions
        assert_eq!(decoded.get_u64(&[1]), Some(0xFFFFFFFFFFFFFFFF));
        assert_eq!(decoded.get_u32(&[2]), Some(0xFFFFFFFF));
        assert_eq!(decoded.get_u16(&[3]), Some(0xFFFF));
        assert_eq!(decoded.get_u8(&[4]), Some(0xFF));

        // Test downcasting
        assert_eq!(decoded.get_u8(&[1]), Some(0xFF)); // u64 -> u8
        assert_eq!(decoded.get_u16(&[1]), Some(0xFFFF)); // u64 -> u16
    }

    #[test]
    fn test_decode_invalid_access() {
        let mut tlv = TlvBuffer::new();
        tlv.write_uint8(1, 42).unwrap();
        let decoded = decode_tlv(&tlv.data).unwrap();

        // Test accessing non-existent tags
        assert_eq!(decoded.get_u8(&[99]), None);
        assert_eq!(decoded.get_string_owned(&[1]), None); // Wrong type
        assert_eq!(decoded.get_bool(&[1]), None); // Wrong type
    }

    #[test]
    fn test_decode_empty_structure() {
        let mut tlv = TlvBuffer::new();
        tlv.write_anon_struct().unwrap();
        tlv.write_struct_end().unwrap();

        let decoded = decode_tlv(&tlv.data).unwrap();

        if let TlvItemValue::List(items) = &decoded.value {
            assert_eq!(items.len(), 0);
        } else {
            panic!("Expected empty list");
        }
    }

    #[test]
    fn test_get_item_method() {
        let mut tlv = TlvBuffer::new();
        tlv.write_struct(1).unwrap();
        tlv.write_uint8(2, 100).unwrap();
        tlv.write_string(3, "test").unwrap();
        tlv.write_bool(4, true).unwrap();
        tlv.write_struct(5).unwrap();
        tlv.write_string(1, "inner").unwrap();
        tlv.write_struct_end().unwrap();
        tlv.write_struct_end().unwrap();

        let decoded = decode_tlv(&tlv.data).unwrap();

        // Test get_item returns the actual item
        let item = decoded.get_item(&[2]).unwrap();
        assert_eq!(item.tag, 2);
        if let TlvItemValue::Int(val) = &item.value {
            assert_eq!(*val, 100);
        } else {
            panic!("Expected Int value");
        }
        let item = decoded.get_item(&[3]).unwrap();
        assert_eq!(item.tag, 3);
        if let TlvItemValue::String(val) = &item.value {
            assert_eq!(*val, "test");
        } else {
            panic!("Expected String value");
        }
        let item = decoded.get_item(&[4]).unwrap();
        assert_eq!(item.tag, 4);
        if let TlvItemValue::Bool(val) = &item.value {
            assert!(*val);
        } else {
            panic!("Expected Bool value");
        }
        let item = decoded.get_item(&[5]).unwrap();
        assert_eq!(item.tag, 5);
        if let TlvItemValue::List(items) = &item.value {
            assert_eq!(items.len(), 1);
            let inner_item = &items[0];
            assert_eq!(inner_item.tag, 1);
            if let TlvItemValue::String(val) = &inner_item.value {
                assert_eq!(*val, "inner");
            } else {
                panic!("Expected String value");
            }
        } else {
            panic!("Expected List value");
        }
        let item = decoded.get_item(&[99]);
        assert!(item.is_none());
    }
}